no-new-symbol.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Rule to disallow use of the new operator with the `Symbol` object
  3. * @author Alberto Rodríguez
  4. * @deprecated in ESLint v9.0.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../types').Rule.RuleModule} */
  11. module.exports = {
  12. meta: {
  13. type: "problem",
  14. docs: {
  15. description: "Disallow `new` operators with the `Symbol` object",
  16. recommended: false,
  17. url: "https://eslint.org/docs/latest/rules/no-new-symbol",
  18. },
  19. deprecated: {
  20. message: "The rule was replaced with a more general rule.",
  21. url: "https://eslint.org/docs/latest/use/migrate-to-9.0.0#eslint-recommended",
  22. deprecatedSince: "9.0.0",
  23. availableUntil: "11.0.0",
  24. replacedBy: [
  25. {
  26. rule: {
  27. name: "no-new-native-nonconstructor",
  28. url: "https://eslint.org/docs/latest/rules/no-new-native-nonconstructor",
  29. },
  30. },
  31. ],
  32. },
  33. schema: [],
  34. messages: {
  35. noNewSymbol: "`Symbol` cannot be called as a constructor.",
  36. },
  37. },
  38. create(context) {
  39. const sourceCode = context.sourceCode;
  40. return {
  41. "Program:exit"(node) {
  42. const globalScope = sourceCode.getScope(node);
  43. const variable = globalScope.set.get("Symbol");
  44. if (variable && variable.defs.length === 0) {
  45. variable.references.forEach(ref => {
  46. const idNode = ref.identifier;
  47. const parent = idNode.parent;
  48. if (
  49. parent &&
  50. parent.type === "NewExpression" &&
  51. parent.callee === idNode
  52. ) {
  53. context.report({
  54. node: idNode,
  55. messageId: "noNewSymbol",
  56. });
  57. }
  58. });
  59. }
  60. },
  61. };
  62. },
  63. };