google_forms/node_modules/eslint/lib/rules/no-obj-calls.js

87 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to flag use of an object property of the global object (Math and JSON) as a function
* @author James Allardice
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
const { CALL, CONSTRUCT, ReferenceTracker } = require("@eslint-community/eslint-utils");
2024-08-09 12:04:48 +00:00
const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect", "Intl"];
2024-08-09 12:04:48 +00:00
/**
* Returns the name of the node to report
* @param {ASTNode} node A node to report
* @returns {string} name to report
*/
function getReportNodeName(node) {
if (node.type === "ChainExpression") {
return getReportNodeName(node.expression);
}
if (node.type === "MemberExpression") {
return getPropertyName(node);
}
return node.name;
}
//------------------------------------------------------------------------------
// 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 calling global object properties as functions",
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-obj-calls"
2024-08-09 12:04:48 +00:00
},
schema: [],
messages: {
unexpectedCall: "'{{name}}' is not a function.",
unexpectedRefCall: "'{{name}}' is reference to '{{ref}}', which is not a function."
}
},
create(context) {
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(node) {
const scope = sourceCode.getScope(node);
2024-08-09 12:04:48 +00:00
const tracker = new ReferenceTracker(scope);
const traceMap = {};
for (const g of nonCallableGlobals) {
traceMap[g] = {
[CALL]: true,
[CONSTRUCT]: true
};
}
2024-08-21 06:34:30 +00:00
for (const { node: refNode, path } of tracker.iterateGlobalReferences(traceMap)) {
const name = getReportNodeName(refNode.callee);
2024-08-09 12:04:48 +00:00
const ref = path[0];
const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall";
2024-08-21 06:34:30 +00:00
context.report({ node: refNode, messageId, data: { name, ref } });
2024-08-09 12:04:48 +00:00
}
}
};
}
};