Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: Implement automatic fix for no-let-in-for-declaration #16642

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
tools: Implement automatic fix for no-let-in-for-declaration
Refs: #16636
  • Loading branch information
starkwang committed Nov 1, 2017
commit ae9b2c675046c224d0efa1eb7127cc00ca5b9895
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