]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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 | ||
34eeec05 | 12 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
13 | module.exports = { |
14 | meta: { | |
15 | type: "suggestion", | |
16 | ||
17 | docs: { | |
8f9d1d4d | 18 | description: "Disallow `Array` constructors", |
eb39fafa DC |
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 | }; |