no-obj-calls.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const { CALL, CONSTRUCT, ReferenceTracker } = require("eslint-utils");
  10. const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];
  15. /**
  16. * Returns the name of the node to report
  17. * @param {ASTNode} node A node to report
  18. * @returns {string} name to report
  19. */
  20. function getReportNodeName(node) {
  21. if (node.type === "ChainExpression") {
  22. return getReportNodeName(node.expression);
  23. }
  24. if (node.type === "MemberExpression") {
  25. return getPropertyName(node);
  26. }
  27. return node.name;
  28. }
  29. //------------------------------------------------------------------------------
  30. // Rule Definition
  31. //------------------------------------------------------------------------------
  32. module.exports = {
  33. meta: {
  34. type: "problem",
  35. docs: {
  36. description: "disallow calling global object properties as functions",
  37. category: "Possible Errors",
  38. recommended: true,
  39. url: "https://eslint.org/docs/rules/no-obj-calls"
  40. },
  41. schema: [],
  42. messages: {
  43. unexpectedCall: "'{{name}}' is not a function.",
  44. unexpectedRefCall: "'{{name}}' is reference to '{{ref}}', which is not a function."
  45. }
  46. },
  47. create(context) {
  48. return {
  49. Program() {
  50. const scope = context.getScope();
  51. const tracker = new ReferenceTracker(scope);
  52. const traceMap = {};
  53. for (const g of nonCallableGlobals) {
  54. traceMap[g] = {
  55. [CALL]: true,
  56. [CONSTRUCT]: true
  57. };
  58. }
  59. for (const { node, path } of tracker.iterateGlobalReferences(traceMap)) {
  60. const name = getReportNodeName(node.callee);
  61. const ref = path[0];
  62. const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall";
  63. context.report({ node, messageId, data: { name, ref } });
  64. }
  65. }
  66. };
  67. }
  68. };