forked from e18e/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefer-array-fill.ts
More file actions
215 lines (201 loc) · 6.58 KB
/
Copy pathprefer-array-fill.ts
File metadata and controls
215 lines (201 loc) · 6.58 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import type {Rule, SourceCode} from 'eslint';
import type {
CallExpression,
ArrowFunctionExpression,
FunctionExpression,
Expression
} from 'estree';
function isConstantExpression(node: Expression): boolean {
switch (node.type) {
case 'Literal':
case 'Identifier':
return true;
case 'CallExpression':
case 'NewExpression':
case 'ObjectExpression':
case 'ArrayExpression':
return false;
case 'MemberExpression':
return (
node.object.type !== 'Super' &&
isConstantExpression(node.object) &&
(!node.computed || isConstantExpression(node.property as Expression))
);
case 'UnaryExpression':
return isConstantExpression(node.argument);
case 'BinaryExpression':
case 'LogicalExpression':
return (
node.left.type !== 'PrivateIdentifier' &&
isConstantExpression(node.left) &&
isConstantExpression(node.right)
);
case 'ConditionalExpression':
return (
isConstantExpression(node.test) &&
isConstantExpression(node.consequent) &&
isConstantExpression(node.alternate)
);
case 'TemplateLiteral':
return node.expressions.every((expr) => isConstantExpression(expr));
default:
return false;
}
}
function getCallbackValueNode(
func: ArrowFunctionExpression | FunctionExpression
): Expression | undefined {
if (func.body.type === 'BlockStatement') {
if (func.body.body.length !== 1) return undefined;
const returnStmt = func.body.body[0];
if (returnStmt?.type === 'ReturnStatement' && returnStmt.argument) {
return returnStmt.argument;
}
return undefined;
}
return func.body;
}
function isConstantCallback(
func: ArrowFunctionExpression | FunctionExpression
): boolean {
if (func.params.length !== 0) {
return false;
}
const valueNode = getCallbackValueNode(func);
if (!valueNode) {
return false;
}
return isConstantExpression(valueNode);
}
function getCallbackValueText(
func: ArrowFunctionExpression | FunctionExpression,
sourceCode: SourceCode
): string | undefined {
const valueNode = getCallbackValueNode(func);
return valueNode ? sourceCode.getText(valueNode) : undefined;
}
export const preferArrayFill: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description:
'Prefer Array.prototype.fill() over Array.from or map with constant values',
recommended: true
},
fixable: 'code',
schema: [],
messages: {
preferFillArrayFrom:
'Use Array.from({length: {{length}}}).fill({{value}}) instead of Array.from with a constant callback',
preferFillSpreadMap:
'Use Array({{length}}).fill({{value}}) instead of spread Array with map'
}
},
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression(node: CallExpression) {
// Check for Array.from({length: n}, () => value)
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'Array' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'from' &&
node.arguments.length === 2
) {
const firstArg = node.arguments[0];
const secondArg = node.arguments[1];
// Check if first arg is {length: n}
if (
firstArg?.type === 'ObjectExpression' &&
firstArg.properties.length === 1 &&
firstArg.properties[0]?.type === 'Property' &&
firstArg.properties[0].key.type === 'Identifier' &&
firstArg.properties[0].key.name === 'length'
) {
// Check if second arg is a constant callback
if (
secondArg &&
(secondArg.type === 'ArrowFunctionExpression' ||
secondArg.type === 'FunctionExpression') &&
isConstantCallback(secondArg)
) {
const lengthValue = firstArg.properties[0].value;
const lengthText = sourceCode.getText(lengthValue);
const valueText = getCallbackValueText(secondArg, sourceCode);
if (!valueText) {
return;
}
context.report({
node,
messageId: 'preferFillArrayFrom',
data: {
length: lengthText,
value: valueText
},
fix(fixer) {
return fixer.replaceText(
node,
`Array.from({length: ${lengthText}}).fill(${valueText})`
);
}
});
}
}
}
// Check for [...Array(n)].map(() => value)
if (
node.callee.type === 'MemberExpression' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'map' &&
node.callee.object.type === 'ArrayExpression' &&
node.callee.object.elements.length === 1 &&
node.arguments.length === 1
) {
const spreadElement = node.callee.object.elements[0];
const callback = node.arguments[0];
if (
spreadElement?.type === 'SpreadElement' &&
spreadElement.argument.type === 'CallExpression' &&
spreadElement.argument.callee.type === 'Identifier' &&
spreadElement.argument.callee.name === 'Array' &&
spreadElement.argument.arguments.length === 1
) {
const arrayArg = spreadElement.argument.arguments[0];
// Check if callback is a constant function
if (
callback &&
(callback.type === 'ArrowFunctionExpression' ||
callback.type === 'FunctionExpression') &&
isConstantCallback(callback)
) {
if (!arrayArg) {
return;
}
const lengthText = sourceCode.getText(arrayArg);
const valueText = getCallbackValueText(callback, sourceCode);
if (!valueText) {
return;
}
context.report({
node,
messageId: 'preferFillSpreadMap',
data: {
length: lengthText,
value: valueText
},
fix(fixer) {
return fixer.replaceText(
node,
`Array(${lengthText}).fill(${valueText})`
);
}
});
}
}
}
}
};
}
};