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: avoid let in for loops #9049

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
1 change: 1 addition & 0 deletions lib/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rules:
# Custom rules in tools/eslint-rules
require-buffer: 2
buffer-constructor: 2
no-let-in-for-declaration: 2
46 changes: 46 additions & 0 deletions tools/eslint-rules/no-let-in-for-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @fileoverview Prohibit the use of `let` as the loop variable
* in the initialization of for, and the left-hand
* iterator in forIn and forOf loops.
*
* @author Jessica Quynh Tran
*/

'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
create(context) {

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

/**
* Report function to test if the for-loop is declared using `let`.
*/
function testForLoop(node) {
if (node.init && node.init.kind === 'let') {
context.report(node.init, msg);
}
}

/**
* Report function to test if the for-in or for-of loop
* is declared using `let`.
*/
function testForInOfLoop(node) {
if (node.left && node.left.kind === 'let') {
context.report(node.left, msg);
}
}

return {
'ForStatement': testForLoop,
'ForInStatement': testForInOfLoop,
'ForOfStatement': testForInOfLoop
};
}
};