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