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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow `Array` constructors",
  14. category: "Stylistic Issues",
  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. };