forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-array-constructor.js
More file actions
195 lines (165 loc) · 4.88 KB
/
Copy pathno-array-constructor.js
File metadata and controls
195 lines (165 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @fileoverview Disallow construction of dense arrays using the Array constructor
* @author Matt DuVall <http://www.mattduvall.com/>
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const {
getVariableByName,
isClosingParenToken,
isOpeningParenToken,
isStartOfExpressionStatement,
needsPrecedingSemicolon,
} = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../types').Rule.RuleModule} */
module.exports = {
meta: {
dialects: ["javascript", "typescript"],
language: "javascript",
type: "suggestion",
docs: {
description: "Disallow `Array` constructors",
recommended: false,
url: "https://eslint.org/docs/latest/rules/no-array-constructor",
},
fixable: "code",
hasSuggestions: true,
schema: [],
messages: {
preferLiteral: "The array literal notation [] is preferable.",
useLiteral: "Replace with an array literal.",
useLiteralAfterSemicolon:
"Replace with an array literal, add preceding semicolon.",
},
},
create(context) {
const sourceCode = context.sourceCode;
/**
* Checks if there are comments in Array constructor expressions.
* @param {ASTNode} node A CallExpression or NewExpression node.
* @returns {boolean} True if there are comments, false otherwise.
*/
function hasCommentsInArrayConstructor(node) {
const firstToken = sourceCode.getFirstToken(node);
const lastToken = sourceCode.getLastToken(node);
let lastRelevantToken = sourceCode.getLastToken(node.callee);
while (
lastRelevantToken !== lastToken &&
!isOpeningParenToken(lastRelevantToken)
) {
lastRelevantToken = sourceCode.getTokenAfter(lastRelevantToken);
}
return sourceCode.commentsExistBetween(
firstToken,
lastRelevantToken,
);
}
/**
* Gets the text between the calling parentheses of a CallExpression or NewExpression.
* @param {ASTNode} node A CallExpression or NewExpression node.
* @returns {string} The text between the calling parentheses, or an empty string if there are none.
*/
function getArgumentsText(node) {
const lastToken = sourceCode.getLastToken(node);
if (!isClosingParenToken(lastToken)) {
return "";
}
let firstToken = node.callee;
do {
firstToken = sourceCode.getTokenAfter(firstToken);
if (!firstToken || firstToken === lastToken) {
return "";
}
} while (!isOpeningParenToken(firstToken));
return sourceCode.text.slice(
firstToken.range[1],
lastToken.range[0],
);
}
/**
* Disallow construction of dense arrays using the Array constructor
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function check(node) {
if (
node.callee.type !== "Identifier" ||
node.callee.name !== "Array" ||
node.typeArguments ||
(node.arguments.length === 1 &&
node.arguments[0].type !== "SpreadElement")
) {
return;
}
const variable = getVariableByName(
sourceCode.getScope(node),
"Array",
);
/*
* Check if `Array` is a predefined global variable: predefined globals have no declarations,
* meaning that the `identifiers` list of the variable object is empty.
*/
if (variable && variable.identifiers.length === 0) {
const argsText = getArgumentsText(node);
let fixText;
let messageId;
const nonSpreadCount = node.arguments.reduce(
(count, arg) =>
arg.type !== "SpreadElement" ? count + 1 : count,
0,
);
const shouldSuggest =
node.optional ||
(node.arguments.length > 0 && nonSpreadCount < 2) ||
hasCommentsInArrayConstructor(node);
/*
* Check if the suggested change should include a preceding semicolon or not.
* Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically
* before an expression like `Array()` or `new Array()`, but not when the expression
* is changed into an array literal like `[]`.
*/
if (
isStartOfExpressionStatement(node) &&
needsPrecedingSemicolon(sourceCode, node)
) {
fixText = `;[${argsText}]`;
messageId = "useLiteralAfterSemicolon";
} else {
fixText = `[${argsText}]`;
messageId = "useLiteral";
}
context.report({
node,
messageId: "preferLiteral",
fix(fixer) {
if (shouldSuggest) {
return null;
}
return fixer.replaceText(node, fixText);
},
suggest: [
{
messageId,
fix(fixer) {
if (shouldSuggest) {
return fixer.replaceText(node, fixText);
}
return null;
},
},
],
});
}
}
return {
CallExpression: check,
NewExpression: check,
};
},
};