mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
tools: avoid let in for loops
This adds a new ESLint tool to check for let declarations within the for, forIn, forOf expressions. Fixes: https://github.com/nodejs/node/issues/9045 Ref: https://github.com/nodejs/node/pull/8873 PR-URL: https://github.com/nodejs/node/pull/9049 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
835fbeee00
commit
b16a97e042
2 files changed
+47
No files matched your search
@@ -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
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user