| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d9c3364 commit c8dccf2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | 1 | rules: | |
| 2 | 2 | # Custom rules in tools/eslint-rules | |
| 3 | 3 | require-buffer: 2 | |
| 4 | + no-let-in-for-declaration: 2 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 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 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments