-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from iancmyers/rule-no-plusplus
Rule: no-plusplus
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |