forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new ESLint tool to check for let declarations within the for, forIn, forOf expressions. Fixes: nodejs#9045 Ref: nodejs#8873
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ rules: | |
# Custom rules in tools/eslint-rules | ||
require-buffer: 2 | ||
buffer-constructor: 2 | ||
no-let-in-for-declaration: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; |