Skip to content

Commit

Permalink
New: add rule no-plusplus (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Feb 11, 2019
1 parent 410c676 commit 3dd99a1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/rules/no-plusplus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @fileoverview add fixer to rule no-plusplus
* @author 唯然<weiran.zsd@outlook.com>
*/
"use strict";

const ruleComposer = require("eslint-rule-composer");
const utils = require("../utils");

const rule = utils.getFixableRule("no-plusplus");
const fixableTypes = new Set(["ExpressionStatement", "ForStatement"]);

module.exports = ruleComposer.mapReports(
rule,
problem => {
const node = problem.node;
const op = node.operator === "++" ? "+=1" : "-=1";

problem.fix = function(fixer) {
return fixableTypes.has(node.parent.type)
? fixer.replaceText(node, `${node.argument.name}${op}`)
: null;
};

return problem;
}
);
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Name | 🛠 | Description
[no-alert](https://eslint.org/docs/rules/no-alert) | 🛠 | disallow the use of `alert`, `confirm`, and `prompt`
[no-console](https://eslint.org/docs/rules/no-console) | 🛠 | disallow the use of `console`
[no-debugger](https://eslint.org/docs/rules/no-debugger) | 🛠 | disallow the use of `debugger`
[no-plusplus](https://eslint.org/docs/rules/no-plusplus) | 🛠 | disallow the unary operators `++` and `--`
[prefer-spread](https://eslint.org/docs/rules/prefer-spread) | 🛠 | require spread operators instead of `.apply()`
[valid-typeof](https:/eslint.org/docs/rules/valid-typeof) | 🛠 | enforce comparing `typeof` expressions against valid strings
<!-- __END AUTOGENERATED TABLE__ -->
Expand Down
66 changes: 66 additions & 0 deletions tests/lib/rules/no-plusplus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @fileoverview Tests for no-plusplus.
* @author 唯然<weiran.zsd@outlook.com>
*/

"use strict";

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

const rule = require("../../../lib/rules/no-plusplus"),
RuleTester = require("eslint").RuleTester;

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

const ruleTester = new RuleTester();

ruleTester.run("no-plusplus", rule, {
valid: [
"var foo = 0; foo=+1;",

// With "allowForLoopAfterthoughts" allowed
{ code: "var foo = 0; foo=+1;", options: [{ allowForLoopAfterthoughts: true }] },
{ code: "for (i = 0; i < l; i++) { console.log(i); }", options: [{ allowForLoopAfterthoughts: true }] }
],

invalid: [
{
code: "var foo = 0; foo++;",
output: "var foo = 0; foo+=1;",
errors: [{ message: "Unary operator '++' used.", type: "UpdateExpression" }]
},
{
code: "var foo = 0; foo--;",
output: "var foo = 0; foo-=1;",
errors: [{ message: "Unary operator '--' used.", type: "UpdateExpression" }]
},
{
code: "for (i = 0; i < l; i++) { console.log(i); }",
output: "for (i = 0; i < l; i+=1) { console.log(i); }",
errors: [{ message: "Unary operator '++' used.", type: "UpdateExpression" }]
},

// With "allowForLoopAfterthoughts" allowed
{
code: "var foo = 0; foo++;",
output: "var foo = 0; foo+=1;",
options: [{ allowForLoopAfterthoughts: true }],
errors: [{ message: "Unary operator '++' used.", type: "UpdateExpression" }]
},
{
code: "for (i = 0; i < l; i++) { v++; }",
output: "for (i = 0; i < l; i++) { v+=1; }",
options: [{ allowForLoopAfterthoughts: true }],
errors: [{ message: "Unary operator '++' used.", type: "UpdateExpression" }]
},
{
code: "var bar = foo++;",
output: null,
errors: [{ message: "Unary operator '++' used.", type: "UpdateExpression" }]
}
]
});

0 comments on commit 3dd99a1

Please sign in to comment.