google_forms/node_modules/eslint/lib/rules/no-undef.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to flag references to undeclared variables.
* @author Mark Macdonald
*/
"use strict";
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Checks if the given node is the argument of a typeof operator.
* @param {ASTNode} node The AST node being checked.
* @returns {boolean} Whether or not the node is the argument of a typeof operator.
*/
function hasTypeOfOperator(node) {
const parent = node.parent;
return parent.type === "UnaryExpression" && parent.operator === "typeof";
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "problem",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow the use of undeclared variables unless mentioned in `/*global */` comments",
2024-08-09 12:04:48 +00:00
recommended: true,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-undef"
2024-08-09 12:04:48 +00:00
},
schema: [
{
type: "object",
properties: {
typeof: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],
messages: {
undef: "'{{name}}' is not defined."
}
},
create(context) {
const options = context.options[0];
const considerTypeOf = options && options.typeof === true || false;
2024-08-21 06:34:30 +00:00
const sourceCode = context.sourceCode;
2024-08-09 12:04:48 +00:00
return {
2024-08-21 06:34:30 +00:00
"Program:exit"(node) {
const globalScope = sourceCode.getScope(node);
2024-08-09 12:04:48 +00:00
globalScope.through.forEach(ref => {
const identifier = ref.identifier;
if (!considerTypeOf && hasTypeOfOperator(identifier)) {
return;
}
context.report({
node: identifier,
messageId: "undef",
data: identifier
});
});
}
};
}
};