summaryrefslogtreecommitdiff
path: root/deps/node-inspect/tools/eslint-rules/no-let-in-for-declaration.js
blob: 8b1a6783e0773d328d7974118e42f7c1dfb30030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
    };
  }
};