2 * @fileoverview Rule to flag references to undeclared variables.
3 * @author Mark Macdonald
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
12 * Checks if the given node is the argument of a typeof operator.
13 * @param {ASTNode} node The AST node being checked.
14 * @returns {boolean} Whether or not the node is the argument of a typeof operator.
16 function hasTypeOfOperator(node) {
17 const parent = node.parent;
19 return parent.type === "UnaryExpression" && parent.operator === "typeof";
22 //------------------------------------------------------------------------------
24 //------------------------------------------------------------------------------
31 description: "disallow the use of undeclared variables unless mentioned in `/*global */` comments",
32 category: "Variables",
34 url: "https://eslint.org/docs/rules/no-undef"
46 additionalProperties: false
50 undef: "'{{name}}' is not defined."
55 const options = context.options[0];
56 const considerTypeOf = options && options.typeof === true || false;
59 "Program:exit"(/* node */) {
60 const globalScope = context.getScope();
62 globalScope.through.forEach(ref => {
63 const identifier = ref.identifier;
65 if (!considerTypeOf && hasTypeOfOperator(identifier)) {