minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-undefined.js
1 /**
2  * @fileoverview Rule to flag references to the undefined variable.
3  * @author Michael Ficarra
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = {
12     meta: {
13         type: "suggestion",
14
15         docs: {
16             description: "disallow the use of `undefined` as an identifier",
17             category: "Variables",
18             recommended: false,
19             url: "https://eslint.org/docs/rules/no-undefined"
20         },
21
22         schema: []
23     },
24
25     create(context) {
26
27         /**
28          * Report an invalid "undefined" identifier node.
29          * @param {ASTNode} node The node to report.
30          * @returns {void}
31          */
32         function report(node) {
33             context.report({
34                 node,
35                 message: "Unexpected use of undefined."
36             });
37         }
38
39         /**
40          * Checks the given scope for references to `undefined` and reports
41          * all references found.
42          * @param {eslint-scope.Scope} scope The scope to check.
43          * @returns {void}
44          */
45         function checkScope(scope) {
46             const undefinedVar = scope.set.get("undefined");
47
48             if (!undefinedVar) {
49                 return;
50             }
51
52             const references = undefinedVar.references;
53
54             const defs = undefinedVar.defs;
55
56             // Report non-initializing references (those are covered in defs below)
57             references
58                 .filter(ref => !ref.init)
59                 .forEach(ref => report(ref.identifier));
60
61             defs.forEach(def => report(def.name));
62         }
63
64         return {
65             "Program:exit"() {
66                 const globalScope = context.getScope();
67
68                 const stack = [globalScope];
69
70                 while (stack.length) {
71                     const scope = stack.pop();
72
73                     stack.push(...scope.childScopes);
74                     checkScope(scope);
75                 }
76             }
77         };
78
79     }
80 };