Skip to content

Commit e30bfad

Browse files
committed
feature: @putout/plugin-split-assignment-expressions: array destructuring after variable assignment
1 parent 4c7f1d7 commit e30bfad

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/plugin-split-assignment-expressions/lib/split-assignment-expressions.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ const {replaceWithMultiple} = operator;
55
const {
66
isAssignmentExpression,
77
AssignmentExpression,
8+
ArrayPattern,
89
isMemberExpression,
910
isSequenceExpression,
1011
} = types;
1112

1213
module.exports.report = () => `Split assignment expressions`;
1314

14-
module.exports.fix = ({path, lefts, right}) => {
15+
module.exports.fix = ({path, lefts, right, merged}) => {
16+
if (merged) {
17+
const rightPath = path.get('right');
18+
const {right, left} = rightPath.node;
19+
const {object, property} = left;
20+
21+
const assignments = [
22+
AssignmentExpression(lefts[0][0], lefts[0][1], object),
23+
AssignmentExpression(lefts[1][0], convertToArray(property), right),
24+
];
25+
26+
replaceWithMultiple(path, assignments);
27+
28+
return;
29+
}
30+
1531
const [[operator, firstLeft], ...otherLefts] = lefts;
1632
const assignments = [
1733
AssignmentExpression(operator, firstLeft, right),
@@ -37,8 +53,7 @@ module.exports.traverse = ({push}) => ({
3753
if (!isAssignmentExpression(right))
3854
return;
3955

40-
if (isMemberExpression(right.left) && isSequenceExpression(right.left.property))
41-
return;
56+
const merged = isMemberExpression(right.left) && isSequenceExpression(right.left.property);
4257

4358
const lefts = [
4459
[operator, left],
@@ -57,6 +72,9 @@ module.exports.traverse = ({push}) => ({
5772
path,
5873
lefts,
5974
right,
75+
merged,
6076
});
6177
},
6278
});
79+
80+
const convertToArray = ({expressions}) => ArrayPattern(expressions);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
currentDir = dirname(currentDir);
2+
[currentDir, currentType] = getParentPath(currentDir, filesystem);

packages/plugin-split-assignment-expressions/test/split-assignment-expressions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('putout: split-assignment-expressions: transform', (t) => {
2929
t.end();
3030
});
3131

32-
test('putout: split-assignment-expressions: no report: separate', (t) => {
33-
t.noReport('separate');
32+
test('putout: split-assignment-expressions: transform: separate', (t) => {
33+
t.transform('separate');
3434
t.end();
3535
});

0 commit comments

Comments
 (0)