• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15module.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};
47