Skip to content

Commit 4896496

Browse files
committed
feature: @putout/plugin-for-of: remove-useless-variables: exclude nested ObjectPatterns
1 parent bfdfb94 commit 4896496

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
for (const place of places) {
2+
const {
3+
rule,
4+
message,
5+
position: {
6+
line,
7+
column,
8+
},
9+
} = place;
10+
}

packages/plugin-for-of/lib/remove-useless-variables/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
const {types, operator} = require('putout');
44

55
const {replaceWith, remove} = operator;
6+
const {
7+
isAssignmentPattern,
8+
isObjectPattern,
9+
} = types;
610

7-
const {isAssignmentPattern} = types;
811
const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
912
const isAssignment = (a) => isAssignmentPattern(a.value);
1013

@@ -24,9 +27,7 @@ module.exports.traverse = ({push, options}) => ({
2427
return;
2528

2629
const {scope, node} = varPath;
27-
2830
const {name} = node;
29-
3031
const {references, referencePaths} = scope.bindings[name];
3132

3233
if (references !== 1)
@@ -48,6 +49,9 @@ module.exports.traverse = ({push, options}) => ({
4849
if (isToManyProperties(idPath, {maxProperties}))
4950
return;
5051

52+
if (isNested(idPath))
53+
return;
54+
5155
const {properties} = idPath.node;
5256

5357
if (idPath.isObjectPattern() && properties.find(isAssignment))
@@ -59,3 +63,15 @@ module.exports.traverse = ({push, options}) => ({
5963
});
6064
},
6165
});
66+
67+
function isNested(path) {
68+
if (!path.isObjectPattern())
69+
return false;
70+
71+
for (const {value} of path.node.properties) {
72+
if (isObjectPattern(value))
73+
return true;
74+
}
75+
76+
return false;
77+
}

packages/plugin-for-of/lib/remove-useless-variables/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ test('putout: plugin-for-of: remove-useless-variables no transform: assignment',
5050
t.end();
5151
});
5252

53+
test('putout: plugin-for-of: remove-useless-variables no transform: nested', (t) => {
54+
t.noReport('nested');
55+
t.end();
56+
});
57+
5358
test('putout: plugin-for-of: remove-useless-variables transform with: array-from', (t) => {
5459
t.transform('array-from', {
5560
'remove-useless-array-from': removeUselessArrayFrom,

0 commit comments

Comments
 (0)