Skip to content

Commit a4c9990

Browse files
committed
feature: @putout/plugin-merge-destructuring-properties: AssignmentExpression
1 parent 3078ca3 commit a4c9990

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

packages/plugin-merge-destructuring-properties/lib/merge-destructuring-properties.js

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,25 @@ const {compare, remove} = operator;
55
const {
66
isObjectPattern,
77
isRestElement,
8+
isAssignmentExpression,
89
} = types;
910

1011
const notEmptyPlaces = (a) => a.places.length;
1112

13+
const LEFT = {
14+
VariableDeclarator: 'id',
15+
AssignmentExpression: 'left',
16+
};
17+
18+
const RIGHT = {
19+
VariableDeclarator: 'init',
20+
AssignmentExpression: 'right',
21+
};
22+
1223
export const report = () => 'Merge object properties when destructuring';
1324

1425
export const fix = ({path, places}) => {
15-
const {node} = path;
16-
17-
if (path.isVariableDeclarator()) {
18-
for (const place of places) {
19-
node.id.properties = [
20-
...node.id.properties,
21-
...place.node.id.properties,
22-
];
23-
24-
remove(place);
25-
}
26-
27-
return;
28-
}
29-
30-
for (const place of places) {
31-
node.left.properties = [
32-
...node.left.properties,
33-
...place.node.left.properties,
34-
];
35-
36-
remove(place);
37-
}
26+
merge(path, places);
3827
};
3928

4029
export const traverse = ({push, store}) => {
@@ -43,8 +32,11 @@ export const traverse = ({push, store}) => {
4332
});
4433

4534
return {
46-
AssignmentExpression(path) {
47-
const {left, right} = path.node;
35+
'VariableDeclarator|AssignmentExpression'(path) {
36+
const {left, right} = split(path);
37+
38+
if (!right)
39+
return;
4840

4941
if (!isObjectPattern(left))
5042
return;
@@ -56,22 +48,6 @@ export const traverse = ({push, store}) => {
5648

5749
add(path, right);
5850
},
59-
VariableDeclarator(path) {
60-
const {id, init} = path.node;
61-
62-
if (!init)
63-
return;
64-
65-
if (!isObjectPattern(id))
66-
return;
67-
68-
for (const property of id.properties) {
69-
if (isRestElement(property))
70-
return;
71-
}
72-
73-
add(path, init);
74-
},
7551
Program: {
7652
exit() {
7753
store()
@@ -84,7 +60,8 @@ export const traverse = ({push, store}) => {
8460

8561
const createUID = (path) => {
8662
const {uid} = path.scope;
87-
const name = path.isVariableDeclarator() ? 'init' : 'right';
63+
const {type} = path;
64+
const name = RIGHT[type];
8865

8966
const str = `${uid}-${path.get(name).toString()}`;
9067

@@ -112,17 +89,14 @@ const addVariable = ({store}) => (path, node) => {
11289
if (currentPath.parentPath.removed)
11390
return;
11491

115-
let is;
92+
const {type} = currentPath;
93+
const name = RIGHT[type];
11694

117-
if (currentPath.isVariableDeclarator()) {
118-
is = compare(currentPath.node.init, node);
119-
is = is && sameKind(path, currentPath);
120-
} else {
121-
is = compare(currentPath.node.right, node);
122-
}
95+
if (isAssignmentExpression(currentPath) && compare(currentPath.node[name], node))
96+
return currentVar.places.push(path);
12397

124-
if (is)
125-
currentVar.places.push(path);
98+
if (sameKind(path, currentPath))
99+
return currentVar.places.push(path);
126100
};
127101

128102
function sameKind(path1, path2) {
@@ -131,3 +105,31 @@ function sameKind(path1, path2) {
131105

132106
return kind1 === kind2;
133107
}
108+
109+
function split(path) {
110+
const {type} = path;
111+
const leftName = LEFT[type];
112+
const rightName = RIGHT[type];
113+
const left = path.node[leftName];
114+
const right = path.node[rightName];
115+
116+
return {
117+
left,
118+
right,
119+
};
120+
}
121+
122+
function merge(path, places) {
123+
const {node, type} = path;
124+
125+
const name = LEFT[type];
126+
127+
for (const place of places) {
128+
node[name].properties = [
129+
...node[name].properties,
130+
...place.node[name].properties,
131+
];
132+
133+
remove(place);
134+
}
135+
}

0 commit comments

Comments
 (0)