[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to flag use of an object property of the global object (Math and JSON) as a function
|
---|
| 3 | * @author James Allardice
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const { CALL, CONSTRUCT, ReferenceTracker } = require("@eslint-community/eslint-utils");
|
---|
| 13 | const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
|
---|
| 14 |
|
---|
| 15 | //------------------------------------------------------------------------------
|
---|
| 16 | // Helpers
|
---|
| 17 | //------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
| 19 | const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect", "Intl"];
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Returns the name of the node to report
|
---|
| 23 | * @param {ASTNode} node A node to report
|
---|
| 24 | * @returns {string} name to report
|
---|
| 25 | */
|
---|
| 26 | function getReportNodeName(node) {
|
---|
| 27 | if (node.type === "ChainExpression") {
|
---|
| 28 | return getReportNodeName(node.expression);
|
---|
| 29 | }
|
---|
| 30 | if (node.type === "MemberExpression") {
|
---|
| 31 | return getPropertyName(node);
|
---|
| 32 | }
|
---|
| 33 | return node.name;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | //------------------------------------------------------------------------------
|
---|
| 37 | // Rule Definition
|
---|
| 38 | //------------------------------------------------------------------------------
|
---|
| 39 |
|
---|
| 40 | /** @type {import('../shared/types').Rule} */
|
---|
| 41 | module.exports = {
|
---|
| 42 | meta: {
|
---|
| 43 | type: "problem",
|
---|
| 44 |
|
---|
| 45 | docs: {
|
---|
| 46 | description: "Disallow calling global object properties as functions",
|
---|
| 47 | recommended: true,
|
---|
| 48 | url: "https://eslint.org/docs/latest/rules/no-obj-calls"
|
---|
| 49 | },
|
---|
| 50 |
|
---|
| 51 | schema: [],
|
---|
| 52 |
|
---|
| 53 | messages: {
|
---|
| 54 | unexpectedCall: "'{{name}}' is not a function.",
|
---|
| 55 | unexpectedRefCall: "'{{name}}' is reference to '{{ref}}', which is not a function."
|
---|
| 56 | }
|
---|
| 57 | },
|
---|
| 58 |
|
---|
| 59 | create(context) {
|
---|
| 60 |
|
---|
| 61 | const sourceCode = context.sourceCode;
|
---|
| 62 |
|
---|
| 63 | return {
|
---|
| 64 | Program(node) {
|
---|
| 65 | const scope = sourceCode.getScope(node);
|
---|
| 66 | const tracker = new ReferenceTracker(scope);
|
---|
| 67 | const traceMap = {};
|
---|
| 68 |
|
---|
| 69 | for (const g of nonCallableGlobals) {
|
---|
| 70 | traceMap[g] = {
|
---|
| 71 | [CALL]: true,
|
---|
| 72 | [CONSTRUCT]: true
|
---|
| 73 | };
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | for (const { node: refNode, path } of tracker.iterateGlobalReferences(traceMap)) {
|
---|
| 77 | const name = getReportNodeName(refNode.callee);
|
---|
| 78 | const ref = path[0];
|
---|
| 79 | const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall";
|
---|
| 80 |
|
---|
| 81 | context.report({ node: refNode, messageId, data: { name, ref } });
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | };
|
---|
| 85 | }
|
---|
| 86 | };
|
---|