Skip to content

Commit

Permalink
Merge pull request #126 from iancmyers/rule-no-plusplus
Browse files Browse the repository at this point in the history
Rule: no-plusplus
  • Loading branch information
nzakas committed Jul 25, 2013
2 parents 48ba0b3 + c264f28 commit 08355ff
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"no-new": 1,
"no-new-func": 1,
"no-native-reassign": 1,
"no-plusplus": 0,
"no-delete-var": 1,
"no-return-assign": 1,

Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ These rules are purely matters of style and are quite subjective.

The following rules are included for compatibility with [JSHint](http://jshint.com/) and [JSLint](http://jslint.com/). While the names of the rules may not match up with the JSHint/JSLint counterpart, the functionality is the same.

* [no-plusplus] - disallow use of unary operators, `++` and `--` (off by default)
* [no-bitwise] - disallow use of bitwise operators (off by default)
* [guard-for-in] - make sure `for-in` loops have an `if` statement (off by default)
32 changes: 32 additions & 0 deletions docs/rules/no-plusplus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# no plusplus

The `no-plusplus` rule flags the use of unary operators, `++` and `--`.

```js
var foo = 0;
foo++;
```

## Rule Details

This rule is aimed at flagging the use of `++` and `--`. Some believe that the use of these unary operators reduces code quality and clarity. There are some programming languages that completely exclude these operators.

The following patterns are considered warnings:

```js
var foo = 0;
foo++;
var bar = 42;
bar--;
```

The following patterns are not considered warnings:

```js
var foo = 0;
foo += 1;
var bar = 42;
bar -= 1;
```
20 changes: 20 additions & 0 deletions lib/rules/no-plusplus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @fileoverview Rule to flag use of unary increment and decrement operators.
* @author Ian Christian Myers
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

return {

"UpdateExpression": function(node) {
context.report(node, "Unary operator '" + node.operator + "' used.");
}

};

};
60 changes: 60 additions & 0 deletions tests/lib/rules/no-plusplus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @fileoverview Tests for no-plusplus.
* @author Ian Christian Myers
*/

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var vows = require("vows"),
assert = require("assert"),
eslint = require("../../../lib/eslint");

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var RULE_ID = "no-plusplus";

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

vows.describe(RULE_ID).addBatch({

"when evaluating unary increment operator": {

topic: "var foo = 0; foo++;",

"should report a violation": function(topic) {

var config = { rules: {} };
config.rules[RULE_ID] = 1;

var messages = eslint.verify(topic, config);
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, RULE_ID);
assert.equal(messages[0].message, "Unary operator '++' used.");
assert.include(messages[0].node.type, "UpdateExpression");
}
},

"when evaluating unary decrement operator": {

topic: "var foo = 0; foo--;",

"should not report a violation": function(topic) {

var config = { rules: {} };
config.rules[RULE_ID] = 1;

var messages = eslint.verify(topic, config);
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, RULE_ID);
assert.equal(messages[0].message, "Unary operator '--' used.");
assert.include(messages[0].node.type, "UpdateExpression");
}
}

}).export(module);

0 comments on commit 08355ff

Please sign in to comment.