Skip to content

Commit 0acc4ff

Browse files
committed
feature: @putout/plugin-variables: remove-unused: ArrayPattern: improve support
1 parent 47db7aa commit 0acc4ff

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

packages/plugin-variables/lib/remove-unused/get-vars.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ test('remove-unused-variables: get-vars: arguments: array destructuring', (t) =>
215215
merge: 'd_',
216216
}, {
217217
raw: du,
218-
source: _u,
218+
source: du,
219219
}];
220220

221221
t.deepEqual(result, expected);

packages/plugin-variables/lib/remove-unused/get-vars/get-vars.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const {
2323
isFunctionDeclaration,
2424
isArrayExpression,
2525
isRestElement,
26+
isArrayPattern,
2627
} = types;
2728

2829
export default ({use, declare, addParams}) => {
2930
const traverseObj = traverseObjectExpression(use);
30-
31-
const processObj = processObjectPattern({
31+
const declareObject = processObjectPattern({
3232
use,
3333
declare,
3434
});
@@ -64,7 +64,7 @@ export default ({use, declare, addParams}) => {
6464
const param = path.get('param');
6565

6666
if (param.isObjectPattern())
67-
return processObj(param.get('properties'));
67+
return declareObject(param.get('properties'));
6868

6969
if (!param.isIdentifier())
7070
return;
@@ -141,7 +141,7 @@ export default ({use, declare, addParams}) => {
141141

142142
for (const elPath of elements) {
143143
if (elPath.isObjectPattern()) {
144-
processObj(elPath.get('properties'));
144+
declareObject(elPath.get('properties'));
145145
continue;
146146
}
147147

@@ -213,7 +213,7 @@ export default ({use, declare, addParams}) => {
213213
use(rightPath, rightPath.node.name);
214214
},
215215

216-
'ArrayExpression'(path) {
216+
ArrayExpression(path) {
217217
const {elements} = path.node;
218218

219219
for (const el of elements) {
@@ -557,7 +557,12 @@ export default ({use, declare, addParams}) => {
557557

558558
/* istanbul ignore else */
559559
if (isObjectPattern(node)) {
560-
processObj(paramPath.get('properties'));
560+
declareObject(paramPath.get('properties'));
561+
continue;
562+
}
563+
564+
if (isArrayPattern(node)) {
565+
declareArray(paramPath.get('elements'));
561566
continue;
562567
}
563568
}

packages/plugin-variables/lib/remove-unused/get-vars/traverse.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export const traverseArrayExpression = (use) => {
125125
for (const elementPath of elementsPaths) {
126126
const {node} = elementPath;
127127

128+
if (isIdentifier(node))
129+
use(elementPath, node.name);
130+
128131
if (node.properties)
129132
traverseObjExpression(elementPath.get('properties'));
130133
}

0 commit comments

Comments
 (0)