File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 11rules:
22 # Custom rules in tools/eslint-rules
33 require-buffer: 2
4+ no-let-in-for-declaration: 2
Original file line number Diff line number Diff line change 1+ /**
2+ * @fileoverview Prohibit the use of `let` as the loop variable
3+ * in the initialization of for, and the left-hand
4+ * iterator in forIn and forOf loops.
5+ *
6+ * @author Jessica Quynh Tran
7+ */
8+
9+ 'use strict' ;
10+
11+ //------------------------------------------------------------------------------
12+ // Rule Definition
13+ //------------------------------------------------------------------------------
14+
15+ module . exports = {
16+ create ( context ) {
17+
18+ const msg = 'Use of `let` as the loop variable in a for-loop is ' +
19+ 'not recommended. Please use `var` instead.' ;
20+
21+ /**
22+ * Report function to test if the for-loop is declared using `let`.
23+ */
24+ function testForLoop ( node ) {
25+ if ( node . init && node . init . kind === 'let' ) {
26+ context . report ( node . init , msg ) ;
27+ }
28+ }
29+
30+ /**
31+ * Report function to test if the for-in or for-of loop
32+ * is declared using `let`.
33+ */
34+ function testForInOfLoop ( node ) {
35+ if ( node . left && node . left . kind === 'let' ) {
36+ context . report ( node . left , msg ) ;
37+ }
38+ }
39+
40+ return {
41+ 'ForStatement' : testForLoop ,
42+ 'ForInStatement' : testForInOfLoop ,
43+ 'ForOfStatement' : testForInOfLoop
44+ } ;
45+ }
46+ } ;
You can’t perform that action at this time.
0 commit comments