Blame view

node_modules/eslint/lib/rules/prefer-const.js 2.92 KB
c39994410   Ryan Glover   wip converting to...
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  /**
   * @fileoverview A rule to suggest using of const declaration for variables that are never modified after declared.
   * @author Toru Nagashima
   * @copyright 2015 Toru Nagashima. All rights reserved.
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      /**
       * Checks whether a reference is the initializer.
       * @param {Reference} reference - A reference to check.
       * @returns {boolean} Whether or not the reference is the initializer.
       */
      function isInitializer(reference) {
          return reference.init === true;
      }
  
      /**
       * Checks whether a reference is read-only or the initializer.
       * @param {Reference} reference - A reference to check.
       * @returns {boolean} Whether or not the reference is read-only or the initializer.
       */
      function isReadOnlyOrInitializer(reference) {
          return reference.isReadOnly() || reference.init === true;
      }
  
      /**
       * Searches and reports variables that are never modified after declared.
       * @param {Scope} scope - A scope of the search domain.
       * @returns {void}
       */
      function checkForVariables(scope) {
          // Skip the TDZ type.
          if (scope.type === "TDZ") {
              return;
          }
  
          var variables = scope.variables;
          for (var i = 0, end = variables.length; i < end; ++i) {
              var variable = variables[i];
              var def = variable.defs[0];
              var declaration = def && def.parent;
              var statement = declaration && declaration.parent;
              var references = variable.references;
              var identifier = variable.identifiers[0];
  
              if (statement &&
                  identifier &&
                  declaration.type === "VariableDeclaration" &&
                  declaration.kind === "let" &&
                  (statement.type !== "ForStatement" || statement.init !== declaration) &&
                  references.some(isInitializer) &&
                  references.every(isReadOnlyOrInitializer)
              ) {
                  context.report(
                      identifier,
                      "`{{name}}` is never modified, use `const` instead.",
                      {name: identifier.name});
              }
          }
      }
  
      /**
       * Adds multiple items to the tail of an array.
       * @param {any[]} array - A destination to add.
       * @param {any[]} values - Items to be added.
       * @returns {void}
       */
      var pushAll = Function.apply.bind(Array.prototype.push);
  
      return {
          "Program:exit": function() {
              var stack = [context.getScope()];
              while (stack.length) {
                  var scope = stack.pop();
                  pushAll(stack, scope.childScopes);
  
                  checkForVariables(scope);
              }
          }
      };
  
  };
  
  module.exports.schema = [];