no-new-wrappers.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @fileoverview Rule to flag when using constructor for wrapper objects
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Disallow `new` operators with the `String`, `Number`, and `Boolean` objects",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-new-wrappers"
  17. },
  18. schema: [],
  19. messages: {
  20. noConstructor: "Do not use {{fn}} as a constructor."
  21. }
  22. },
  23. create(context) {
  24. return {
  25. NewExpression(node) {
  26. const wrapperObjects = ["String", "Number", "Boolean"];
  27. if (wrapperObjects.includes(node.callee.name)) {
  28. context.report({
  29. node,
  30. messageId: "noConstructor",
  31. data: { fn: node.callee.name }
  32. });
  33. }
  34. }
  35. };
  36. }
  37. };