/
consistent-destructuring.js
169 lines (141 loc) · 3.84 KB
/
consistent-destructuring.js
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
'use strict';
const avoidCapture = require('./utils/avoid-capture.js');
const {not, notLeftHandSideSelector} = require('./selectors/index.js');
const MESSAGE_ID = 'consistentDestructuring';
const MESSAGE_ID_SUGGEST = 'consistentDestructuringSuggest';
const declaratorSelector = [
'VariableDeclarator',
'[id.type="ObjectPattern"]',
'[init]',
'[init.type!="Literal"]',
].join('');
const memberSelector = [
'MemberExpression',
'[computed!=true]',
notLeftHandSideSelector(),
not([
'CallExpression > .callee',
'NewExpression> .callee',
]),
].join('');
const isSimpleExpression = expression => {
while (expression) {
if (expression.computed) {
return false;
}
if (expression.type !== 'MemberExpression') {
break;
}
expression = expression.object;
}
return expression.type === 'Identifier'
|| expression.type === 'ThisExpression';
};
const isChildInParentScope = (child, parent) => {
while (child) {
if (child === parent) {
return true;
}
child = child.upper;
}
return false;
};
const create = context => {
const source = context.getSourceCode();
const declarations = new Map();
return {
[declaratorSelector]: node => {
// Ignore any complex expressions (e.g. arrays, functions)
if (!isSimpleExpression(node.init)) {
return;
}
declarations.set(source.getText(node.init), {
scope: context.getScope(),
variables: context.getDeclaredVariables(node),
objectPattern: node.id,
});
},
[memberSelector]: node => {
const declaration = declarations.get(source.getText(node.object));
if (!declaration) {
return;
}
const {scope, objectPattern} = declaration;
const memberScope = context.getScope();
// Property is destructured outside the current scope
if (!isChildInParentScope(memberScope, scope)) {
return;
}
const destructurings = objectPattern.properties.filter(property =>
property.type === 'Property'
&& property.key.type === 'Identifier'
&& property.value.type === 'Identifier',
);
const lastProperty = objectPattern.properties[objectPattern.properties.length - 1];
const hasRest = lastProperty && lastProperty.type === 'RestElement';
const expression = source.getText(node);
const member = source.getText(node.property);
// Member might already be destructured
const destructuredMember = destructurings.find(property =>
property.key.name === member,
);
if (!destructuredMember) {
// Don't destructure additional members when rest is used
if (hasRest) {
return;
}
// Destructured member collides with an existing identifier
if (avoidCapture(member, [memberScope]) !== member) {
return;
}
}
// Don't try to fix nested member expressions
if (node.parent.type === 'MemberExpression') {
return {
node,
messageId: MESSAGE_ID,
};
}
const newMember = destructuredMember ? destructuredMember.value.name : member;
return {
node,
messageId: MESSAGE_ID,
suggest: [{
messageId: MESSAGE_ID_SUGGEST,
data: {
expression,
property: newMember,
},
* fix(fixer) {
const {properties} = objectPattern;
const lastProperty = properties[properties.length - 1];
yield fixer.replaceText(node, newMember);
if (!destructuredMember) {
yield lastProperty
? fixer.insertTextAfter(lastProperty, `, ${newMember}`)
: fixer.replaceText(objectPattern, `{${newMember}}`);
}
},
}],
};
},
};
};
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Use destructured variables over properties.',
},
fixable: 'code',
hasSuggestions: true,
messages: {
[MESSAGE_ID]: 'Use destructured variables over properties.',
[MESSAGE_ID_SUGGEST]: 'Replace `{{expression}}` with destructured property `{{property}}`.',
},
},
};