Skip to content

Commit

Permalink
tools: add fixer for no-let-in-for-declaration
Browse files Browse the repository at this point in the history
PR-URL: #16642
Refs: #16636
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
starkwang authored and gibfahn committed Dec 20, 2017
1 parent 5a1a567 commit a91fb54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions test/parallel/test-eslint-no-let-in-for-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ ruleTester.run('no-let-in-for-declaration', rule, {
invalid: [
{
code: 'for (let foo = 1;;);',
output: 'for (var foo = 1;;);',
errors: [{ message }]
},
{
code: 'for (let foo in bar);',
output: 'for (var foo in bar);',
errors: [{ message }]
},
{
code: 'for (let foo of bar);',
output: 'for (var foo of bar);',
errors: [{ message }]
}
]
Expand Down
16 changes: 13 additions & 3 deletions tools/eslint-rules/no-let-in-for-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module.exports = {
create(context) {

const sourceCode = context.getSourceCode();
const msg = 'Use of `let` as the loop variable in a for-loop is ' +
'not recommended. Please use `var` instead.';

Expand All @@ -23,7 +23,12 @@ module.exports = {
*/
function testForLoop(node) {
if (node.init && node.init.kind === 'let') {
context.report(node.init, msg);
context.report({
node: node.init,
message: msg,
fix: (fixer) =>
fixer.replaceText(sourceCode.getFirstToken(node.init), 'var')
});
}
}

Expand All @@ -33,7 +38,12 @@ module.exports = {
*/
function testForInOfLoop(node) {
if (node.left && node.left.kind === 'let') {
context.report(node.left, msg);
context.report({
node: node.left,
message: msg,
fix: (fixer) =>
fixer.replaceText(sourceCode.getFirstToken(node.left), 'var')
});
}
}

Expand Down

0 comments on commit a91fb54

Please sign in to comment.