no-array-constructor.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @fileoverview Disallow construction of dense arrays using the Array constructor
  3. * @author Matt DuVall <http://www.mattduvall.com/>
  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 `Array` constructors",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-array-constructor"
  17. },
  18. schema: [],
  19. messages: {
  20. preferLiteral: "The array literal notation [] is preferable."
  21. }
  22. },
  23. create(context) {
  24. /**
  25. * Disallow construction of dense arrays using the Array constructor
  26. * @param {ASTNode} node node to evaluate
  27. * @returns {void}
  28. * @private
  29. */
  30. function check(node) {
  31. if (
  32. node.arguments.length !== 1 &&
  33. node.callee.type === "Identifier" &&
  34. node.callee.name === "Array"
  35. ) {
  36. context.report({ node, messageId: "preferLiteral" });
  37. }
  38. }
  39. return {
  40. CallExpression: check,
  41. NewExpression: check
  42. };
  43. }
  44. };