no-obj-calls.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, ReferenceTracker } = require("eslint-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];
  14. //------------------------------------------------------------------------------
  15. // Rule Definition
  16. //------------------------------------------------------------------------------
  17. module.exports = {
  18. meta: {
  19. type: "problem",
  20. docs: {
  21. description: "disallow calling global object properties as functions",
  22. category: "Possible Errors",
  23. recommended: true,
  24. url: "https://eslint.org/docs/rules/no-obj-calls"
  25. },
  26. schema: [],
  27. messages: {
  28. unexpectedCall: "'{{name}}' is not a function."
  29. }
  30. },
  31. create(context) {
  32. return {
  33. Program() {
  34. const scope = context.getScope();
  35. const tracker = new ReferenceTracker(scope);
  36. const traceMap = {};
  37. for (const global of nonCallableGlobals) {
  38. traceMap[global] = {
  39. [CALL]: true
  40. };
  41. }
  42. for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
  43. context.report({ node, messageId: "unexpectedCall", data: { name: node.callee.name } });
  44. }
  45. }
  46. };
  47. }
  48. };