prefer-destructuring.js
10.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @fileoverview Prefer destructuring from arrays and objects
* @author Alex LaFroscia
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ type: "AssignmentExpression" });
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Require destructuring from arrays and/or objects",
recommended: false,
url: "https://eslint.org/docs/rules/prefer-destructuring"
},
fixable: "code",
schema: [
{
/*
* old support {array: Boolean, object: Boolean}
* new support {VariableDeclarator: {}, AssignmentExpression: {}}
*/
oneOf: [
{
type: "object",
properties: {
VariableDeclarator: {
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
},
AssignmentExpression: {
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
}
},
additionalProperties: false
},
{
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
}
]
},
{
type: "object",
properties: {
enforceForRenamedProperties: {
type: "boolean"
}
},
additionalProperties: false
}
],
messages: {
preferDestructuring: "Use {{type}} destructuring."
}
},
create(context) {
const enabledTypes = context.options[0];
const enforceForRenamedProperties = context.options[1] && context.options[1].enforceForRenamedProperties;
let normalizedOptions = {
VariableDeclarator: { array: true, object: true },
AssignmentExpression: { array: true, object: true }
};
if (enabledTypes) {
normalizedOptions = typeof enabledTypes.array !== "undefined" || typeof enabledTypes.object !== "undefined"
? { VariableDeclarator: enabledTypes, AssignmentExpression: enabledTypes }
: enabledTypes;
}
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Checks if destructuring type should be checked.
* @param {string} nodeType "AssignmentExpression" or "VariableDeclarator"
* @param {string} destructuringType "array" or "object"
* @returns {boolean} `true` if the destructuring type should be checked for the given node
*/
function shouldCheck(nodeType, destructuringType) {
return normalizedOptions &&
normalizedOptions[nodeType] &&
normalizedOptions[nodeType][destructuringType];
}
/**
* Determines if the given node is accessing an array index
*
* This is used to differentiate array index access from object property
* access.
* @param {ASTNode} node the node to evaluate
* @returns {boolean} whether or not the node is an integer
*/
function isArrayIndexAccess(node) {
return Number.isInteger(node.property.value);
}
/**
* Report that the given node should use destructuring
* @param {ASTNode} reportNode the node to report
* @param {string} type the type of destructuring that should have been done
* @param {Function|null} fix the fix function or null to pass to context.report
* @returns {void}
*/
function report(reportNode, type, fix) {
context.report({
node: reportNode,
messageId: "preferDestructuring",
data: { type },
fix
});
}
/**
* Determines if a node should be fixed into object destructuring
*
* The fixer only fixes the simplest case of object destructuring,
* like: `let x = a.x`;
*
* Assignment expression is not fixed.
* Array destructuring is not fixed.
* Renamed property is not fixed.
* @param {ASTNode} node the node to evaluate
* @returns {boolean} whether or not the node should be fixed
*/
function shouldFix(node) {
return node.type === "VariableDeclarator" &&
node.id.type === "Identifier" &&
node.init.type === "MemberExpression" &&
!node.init.computed &&
node.init.property.type === "Identifier" &&
node.id.name === node.init.property.name;
}
/**
* Fix a node into object destructuring.
* This function only handles the simplest case of object destructuring,
* see {@link shouldFix}.
* @param {SourceCodeFixer} fixer the fixer object
* @param {ASTNode} node the node to be fixed.
* @returns {Object} a fix for the node
*/
function fixIntoObjectDestructuring(fixer, node) {
const rightNode = node.init;
const sourceCode = context.getSourceCode();
// Don't fix if that would remove any comments. Only comments inside `rightNode.object` can be preserved.
if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(rightNode.object).length) {
return null;
}
let objectText = sourceCode.getText(rightNode.object);
if (astUtils.getPrecedence(rightNode.object) < PRECEDENCE_OF_ASSIGNMENT_EXPR) {
objectText = `(${objectText})`;
}
return fixer.replaceText(
node,
`{${rightNode.property.name}} = ${objectText}`
);
}
/**
* Check that the `prefer-destructuring` rules are followed based on the
* given left- and right-hand side of the assignment.
*
* Pulled out into a separate method so that VariableDeclarators and
* AssignmentExpressions can share the same verification logic.
* @param {ASTNode} leftNode the left-hand side of the assignment
* @param {ASTNode} rightNode the right-hand side of the assignment
* @param {ASTNode} reportNode the node to report the error on
* @returns {void}
*/
function performCheck(leftNode, rightNode, reportNode) {
if (
rightNode.type !== "MemberExpression" ||
rightNode.object.type === "Super" ||
rightNode.property.type === "PrivateIdentifier"
) {
return;
}
if (isArrayIndexAccess(rightNode)) {
if (shouldCheck(reportNode.type, "array")) {
report(reportNode, "array", null);
}
return;
}
const fix = shouldFix(reportNode)
? fixer => fixIntoObjectDestructuring(fixer, reportNode)
: null;
if (shouldCheck(reportNode.type, "object") && enforceForRenamedProperties) {
report(reportNode, "object", fix);
return;
}
if (shouldCheck(reportNode.type, "object")) {
const property = rightNode.property;
if (
(property.type === "Literal" && leftNode.name === property.value) ||
(property.type === "Identifier" && leftNode.name === property.name && !rightNode.computed)
) {
report(reportNode, "object", fix);
}
}
}
/**
* Check if a given variable declarator is coming from an property access
* that should be using destructuring instead
* @param {ASTNode} node the variable declarator to check
* @returns {void}
*/
function checkVariableDeclarator(node) {
// Skip if variable is declared without assignment
if (!node.init) {
return;
}
// We only care about member expressions past this point
if (node.init.type !== "MemberExpression") {
return;
}
performCheck(node.id, node.init, node);
}
/**
* Run the `prefer-destructuring` check on an AssignmentExpression
* @param {ASTNode} node the AssignmentExpression node
* @returns {void}
*/
function checkAssignmentExpression(node) {
if (node.operator === "=") {
performCheck(node.left, node.right, node);
}
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
VariableDeclarator: checkVariableDeclarator,
AssignmentExpression: checkAssignmentExpression
};
}
};