-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
410c676
commit 3dd99a1
Showing
3 changed files
with
94 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
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; | ||
} | ||
); |
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,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" }] | ||
} | ||
] | ||
}); |