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 <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
starkwang authored and cjihrig committed Nov 6, 2017
1 parent a87f846 commit 326a048
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 326a048

Please sign in to comment.