no-new-symbol.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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('../shared/types').Rule} */
  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: true,
  20. replacedBy: [
  21. "no-new-native-nonconstructor"
  22. ],
  23. schema: [],
  24. messages: {
  25. noNewSymbol: "`Symbol` cannot be called as a constructor."
  26. }
  27. },
  28. create(context) {
  29. const sourceCode = context.sourceCode;
  30. return {
  31. "Program:exit"(node) {
  32. const globalScope = sourceCode.getScope(node);
  33. const variable = globalScope.set.get("Symbol");
  34. if (variable && variable.defs.length === 0) {
  35. variable.references.forEach(ref => {
  36. const idNode = ref.identifier;
  37. const parent = idNode.parent;
  38. if (parent && parent.type === "NewExpression" && parent.callee === idNode) {
  39. context.report({
  40. node: idNode,
  41. messageId: "noNewSymbol"
  42. });
  43. }
  44. });
  45. }
  46. }
  47. };
  48. }
  49. };