2 * @fileoverview Rule to flag use of an object property of the global object (Math and JSON) as a function
3 * @author James Allardice
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const { CALL, CONSTRUCT, ReferenceTracker } = require("eslint-utils");
13 const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];
22 * Returns the name of the node to report
23 * @param {ASTNode} node A node to report
24 * @returns {string} name to report
26 function getReportNodeName(node) {
27 if (node.type === "ChainExpression") {
28 return getReportNodeName(node.expression);
30 if (node.type === "MemberExpression") {
31 return getPropertyName(node);
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
45 description: "disallow calling global object properties as functions",
46 category: "Possible Errors",
48 url: "https://eslint.org/docs/rules/no-obj-calls"
54 unexpectedCall: "'{{name}}' is not a function.",
55 unexpectedRefCall: "'{{name}}' is reference to '{{ref}}', which is not a function."
63 const scope = context.getScope();
64 const tracker = new ReferenceTracker(scope);
67 for (const g of nonCallableGlobals) {
74 for (const { node, path } of tracker.iterateGlobalReferences(traceMap)) {
75 const name = getReportNodeName(node.callee);
77 const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall";
79 context.report({ node, messageId, data: { name, ref } });