UNPKG

1.37 kBJavaScriptView Raw
1/**
2 * @fileoverview Disallow construction of dense arrays using the Array constructor
3 * @author Matt DuVall <http://www.mattduvall.com/>
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = {
13 meta: {
14 docs: {
15 description: "disallow `Array` constructors",
16 category: "Stylistic Issues",
17 recommended: false,
18 url: "https://eslint.org/docs/rules/no-array-constructor"
19 },
20
21 schema: [],
22
23 messages: {
24 preferLiteral: "The array literal notation [] is preferable."
25 }
26 },
27
28 create(context) {
29
30 /**
31 * Disallow construction of dense arrays using the Array constructor
32 * @param {ASTNode} node node to evaluate
33 * @returns {void}
34 * @private
35 */
36 function check(node) {
37 if (
38 node.arguments.length !== 1 &&
39 node.callee.type === "Identifier" &&
40 node.callee.name === "Array"
41 ) {
42 context.report({ node, messageId: "preferLiteral" });
43 }
44 }
45
46 return {
47 CallExpression: check,
48 NewExpression: check
49 };
50
51 }
52};