2 * @fileoverview Rule to flag references to the undefined variable.
3 * @author Michael Ficarra
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "disallow the use of `undefined` as an identifier",
17 category: "Variables",
19 url: "https://eslint.org/docs/rules/no-undefined"
25 unexpectedUndefined: "Unexpected use of undefined."
32 * Report an invalid "undefined" identifier node.
33 * @param {ASTNode} node The node to report.
36 function report(node) {
39 messageId: "unexpectedUndefined"
44 * Checks the given scope for references to `undefined` and reports
45 * all references found.
46 * @param {eslint-scope.Scope} scope The scope to check.
49 function checkScope(scope) {
50 const undefinedVar = scope.set.get("undefined");
56 const references = undefinedVar.references;
58 const defs = undefinedVar.defs;
60 // Report non-initializing references (those are covered in defs below)
62 .filter(ref => !ref.init)
63 .forEach(ref => report(ref.identifier));
65 defs.forEach(def => report(def.name));
70 const globalScope = context.getScope();
72 const stack = [globalScope];
74 while (stack.length) {
75 const scope = stack.pop();
77 stack.push(...scope.childScopes);