Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-var.js
1 /**
2  * @fileoverview Rule to check for the usage of var.
3  * @author Jamund Ferguson
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Helpers
16 //------------------------------------------------------------------------------
17
18 /**
19  * Check whether a given variable is a global variable or not.
20  * @param {eslint-scope.Variable} variable The variable to check.
21  * @returns {boolean} `true` if the variable is a global variable.
22  */
23 function isGlobal(variable) {
24     return Boolean(variable.scope) && variable.scope.type === "global";
25 }
26
27 /**
28  * Finds the nearest function scope or global scope walking up the scope
29  * hierarchy.
30  * @param {eslint-scope.Scope} scope The scope to traverse.
31  * @returns {eslint-scope.Scope} a function scope or global scope containing the given
32  *      scope.
33  */
34 function getEnclosingFunctionScope(scope) {
35     let currentScope = scope;
36
37     while (currentScope.type !== "function" && currentScope.type !== "global") {
38         currentScope = currentScope.upper;
39     }
40     return currentScope;
41 }
42
43 /**
44  * Checks whether the given variable has any references from a more specific
45  * function expression (i.e. a closure).
46  * @param {eslint-scope.Variable} variable A variable to check.
47  * @returns {boolean} `true` if the variable is used from a closure.
48  */
49 function isReferencedInClosure(variable) {
50     const enclosingFunctionScope = getEnclosingFunctionScope(variable.scope);
51
52     return variable.references.some(reference =>
53         getEnclosingFunctionScope(reference.from) !== enclosingFunctionScope);
54 }
55
56 /**
57  * Checks whether the given node is the assignee of a loop.
58  * @param {ASTNode} node A VariableDeclaration node to check.
59  * @returns {boolean} `true` if the declaration is assigned as part of loop
60  *      iteration.
61  */
62 function isLoopAssignee(node) {
63     return (node.parent.type === "ForOfStatement" || node.parent.type === "ForInStatement") &&
64         node === node.parent.left;
65 }
66
67 /**
68  * Checks whether the given variable declaration is immediately initialized.
69  * @param {ASTNode} node A VariableDeclaration node to check.
70  * @returns {boolean} `true` if the declaration has an initializer.
71  */
72 function isDeclarationInitialized(node) {
73     return node.declarations.every(declarator => declarator.init !== null);
74 }
75
76 const SCOPE_NODE_TYPE = /^(?:Program|BlockStatement|SwitchStatement|ForStatement|ForInStatement|ForOfStatement)$/u;
77
78 /**
79  * Gets the scope node which directly contains a given node.
80  * @param {ASTNode} node A node to get. This is a `VariableDeclaration` or
81  *      an `Identifier`.
82  * @returns {ASTNode} A scope node. This is one of `Program`, `BlockStatement`,
83  *      `SwitchStatement`, `ForStatement`, `ForInStatement`, and
84  *      `ForOfStatement`.
85  */
86 function getScopeNode(node) {
87     for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
88         if (SCOPE_NODE_TYPE.test(currentNode.type)) {
89             return currentNode;
90         }
91     }
92
93     /* istanbul ignore next : unreachable */
94     return null;
95 }
96
97 /**
98  * Checks whether a given variable is redeclared or not.
99  * @param {eslint-scope.Variable} variable A variable to check.
100  * @returns {boolean} `true` if the variable is redeclared.
101  */
102 function isRedeclared(variable) {
103     return variable.defs.length >= 2;
104 }
105
106 /**
107  * Checks whether a given variable is used from outside of the specified scope.
108  * @param {ASTNode} scopeNode A scope node to check.
109  * @returns {Function} The predicate function which checks whether a given
110  *      variable is used from outside of the specified scope.
111  */
112 function isUsedFromOutsideOf(scopeNode) {
113
114     /**
115      * Checks whether a given reference is inside of the specified scope or not.
116      * @param {eslint-scope.Reference} reference A reference to check.
117      * @returns {boolean} `true` if the reference is inside of the specified
118      *      scope.
119      */
120     function isOutsideOfScope(reference) {
121         const scope = scopeNode.range;
122         const id = reference.identifier.range;
123
124         return id[0] < scope[0] || id[1] > scope[1];
125     }
126
127     return function(variable) {
128         return variable.references.some(isOutsideOfScope);
129     };
130 }
131
132 /**
133  * Creates the predicate function which checks whether a variable has their references in TDZ.
134  *
135  * The predicate function would return `true`:
136  *
137  * - if a reference is before the declarator. E.g. (var a = b, b = 1;)(var {a = b, b} = {};)
138  * - if a reference is in the expression of their default value.  E.g. (var {a = a} = {};)
139  * - if a reference is in the expression of their initializer.  E.g. (var a = a;)
140  * @param {ASTNode} node The initializer node of VariableDeclarator.
141  * @returns {Function} The predicate function.
142  * @private
143  */
144 function hasReferenceInTDZ(node) {
145     const initStart = node.range[0];
146     const initEnd = node.range[1];
147
148     return variable => {
149         const id = variable.defs[0].name;
150         const idStart = id.range[0];
151         const defaultValue = (id.parent.type === "AssignmentPattern" ? id.parent.right : null);
152         const defaultStart = defaultValue && defaultValue.range[0];
153         const defaultEnd = defaultValue && defaultValue.range[1];
154
155         return variable.references.some(reference => {
156             const start = reference.identifier.range[0];
157             const end = reference.identifier.range[1];
158
159             return !reference.init && (
160                 start < idStart ||
161                 (defaultValue !== null && start >= defaultStart && end <= defaultEnd) ||
162                 (start >= initStart && end <= initEnd)
163             );
164         });
165     };
166 }
167
168 /**
169  * Checks whether a given variable has name that is allowed for 'var' declarations,
170  * but disallowed for `let` declarations.
171  * @param {eslint-scope.Variable} variable The variable to check.
172  * @returns {boolean} `true` if the variable has a disallowed name.
173  */
174 function hasNameDisallowedForLetDeclarations(variable) {
175     return variable.name === "let";
176 }
177
178 //------------------------------------------------------------------------------
179 // Rule Definition
180 //------------------------------------------------------------------------------
181
182 module.exports = {
183     meta: {
184         type: "suggestion",
185
186         docs: {
187             description: "require `let` or `const` instead of `var`",
188             category: "ECMAScript 6",
189             recommended: false,
190             url: "https://eslint.org/docs/rules/no-var"
191         },
192
193         schema: [],
194         fixable: "code"
195     },
196
197     create(context) {
198         const sourceCode = context.getSourceCode();
199
200         /**
201          * Checks whether the variables which are defined by the given declarator node have their references in TDZ.
202          * @param {ASTNode} declarator The VariableDeclarator node to check.
203          * @returns {boolean} `true` if one of the variables which are defined by the given declarator node have their references in TDZ.
204          */
205         function hasSelfReferenceInTDZ(declarator) {
206             if (!declarator.init) {
207                 return false;
208             }
209             const variables = context.getDeclaredVariables(declarator);
210
211             return variables.some(hasReferenceInTDZ(declarator.init));
212         }
213
214         /**
215          * Checks whether it can fix a given variable declaration or not.
216          * It cannot fix if the following cases:
217          *
218          * - A variable is a global variable.
219          * - A variable is declared on a SwitchCase node.
220          * - A variable is redeclared.
221          * - A variable is used from outside the scope.
222          * - A variable is used from a closure within a loop.
223          * - A variable might be used before it is assigned within a loop.
224          * - A variable might be used in TDZ.
225          * - A variable is declared in statement position (e.g. a single-line `IfStatement`)
226          * - A variable has name that is disallowed for `let` declarations.
227          *
228          * ## A variable is declared on a SwitchCase node.
229          *
230          * If this rule modifies 'var' declarations on a SwitchCase node, it
231          * would generate the warnings of 'no-case-declarations' rule. And the
232          * 'eslint:recommended' preset includes 'no-case-declarations' rule, so
233          * this rule doesn't modify those declarations.
234          *
235          * ## A variable is redeclared.
236          *
237          * The language spec disallows redeclarations of `let` declarations.
238          * Those variables would cause syntax errors.
239          *
240          * ## A variable is used from outside the scope.
241          *
242          * The language spec disallows accesses from outside of the scope for
243          * `let` declarations. Those variables would cause reference errors.
244          *
245          * ## A variable is used from a closure within a loop.
246          *
247          * A `var` declaration within a loop shares the same variable instance
248          * across all loop iterations, while a `let` declaration creates a new
249          * instance for each iteration. This means if a variable in a loop is
250          * referenced by any closure, changing it from `var` to `let` would
251          * change the behavior in a way that is generally unsafe.
252          *
253          * ## A variable might be used before it is assigned within a loop.
254          *
255          * Within a loop, a `let` declaration without an initializer will be
256          * initialized to null, while a `var` declaration will retain its value
257          * from the previous iteration, so it is only safe to change `var` to
258          * `let` if we can statically determine that the variable is always
259          * assigned a value before its first access in the loop body. To keep
260          * the implementation simple, we only convert `var` to `let` within
261          * loops when the variable is a loop assignee or the declaration has an
262          * initializer.
263          * @param {ASTNode} node A variable declaration node to check.
264          * @returns {boolean} `true` if it can fix the node.
265          */
266         function canFix(node) {
267             const variables = context.getDeclaredVariables(node);
268             const scopeNode = getScopeNode(node);
269
270             if (node.parent.type === "SwitchCase" ||
271                 node.declarations.some(hasSelfReferenceInTDZ) ||
272                 variables.some(isGlobal) ||
273                 variables.some(isRedeclared) ||
274                 variables.some(isUsedFromOutsideOf(scopeNode)) ||
275                 variables.some(hasNameDisallowedForLetDeclarations)
276             ) {
277                 return false;
278             }
279
280             if (astUtils.isInLoop(node)) {
281                 if (variables.some(isReferencedInClosure)) {
282                     return false;
283                 }
284                 if (!isLoopAssignee(node) && !isDeclarationInitialized(node)) {
285                     return false;
286                 }
287             }
288
289             if (
290                 !isLoopAssignee(node) &&
291                 !(node.parent.type === "ForStatement" && node.parent.init === node) &&
292                 !astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)
293             ) {
294
295                 // If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed.
296                 return false;
297             }
298
299             return true;
300         }
301
302         /**
303          * Reports a given variable declaration node.
304          * @param {ASTNode} node A variable declaration node to report.
305          * @returns {void}
306          */
307         function report(node) {
308             context.report({
309                 node,
310                 message: "Unexpected var, use let or const instead.",
311
312                 fix(fixer) {
313                     const varToken = sourceCode.getFirstToken(node, { filter: t => t.value === "var" });
314
315                     return canFix(node)
316                         ? fixer.replaceText(varToken, "let")
317                         : null;
318                 }
319             });
320         }
321
322         return {
323             "VariableDeclaration:exit"(node) {
324                 if (node.kind === "var") {
325                     report(node);
326                 }
327             }
328         };
329     }
330 };