租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

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