no-array-constructor.js 1.3 KB

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