Skip to content

Commit e0ef81c

Browse files
committed
feature: @putout/plugin-parens: add-missing-for-assign: add
1 parent 6d1e778 commit e0ef81c

File tree

16 files changed

+152
-3
lines changed

16 files changed

+152
-3
lines changed

docs/syntax-errors.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,12 @@ const a = {
264264
```
265265

266266
</details>
267+
268+
<details><summary>wrong brace</summary>
269+
270+
```diff
271+
a && b = a;
272+
a && (b = a);
273+
```
274+
275+
</details>

packages/plugin-parens/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ npm i @putout/plugin-parens
2525
}
2626
```
2727

28+
## assign
29+
30+
To disable use:
31+
32+
```json
33+
{
34+
"rules": {
35+
"parens/add-missing-for-assign": "off"
36+
}
37+
}
38+
```
39+
40+
## ❌ Example of incorrect code
41+
42+
```diff
43+
-a && b = a;
44+
+a && (b = a);
45+
```
46+
2847
## await
2948

3049
To disable use:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a && (b = a);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a && b = a;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a === (b = c);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a === b = c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!(b = c);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!b = c
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
const {types, operator} = require('putout');
4+
const {
5+
BinaryExpression,
6+
LogicalExpression,
7+
UnaryExpression,
8+
AssignmentExpression,
9+
isBinaryExpression,
10+
isUnaryExpression,
11+
isLogicalExpression,
12+
} = types;
13+
14+
const {replaceWith} = operator;
15+
16+
module.exports.report = () => `SyntaxError: Invalid left-hand side in assignment expression`;
17+
18+
module.exports.fix = (path) => {
19+
const {
20+
left,
21+
right,
22+
operator,
23+
} = path.node;
24+
25+
const logicalOperator = left.operator;
26+
const logicalLeft = left.left;
27+
28+
if (isLogicalExpression(left)) {
29+
const logicalRight = AssignmentExpression(operator, left.right, right);
30+
31+
replaceWith(path, LogicalExpression(
32+
logicalOperator,
33+
logicalLeft,
34+
logicalRight,
35+
));
36+
} else if (isBinaryExpression(left)) {
37+
const logicalRight = AssignmentExpression(operator, left.right, right);
38+
39+
replaceWith(path, BinaryExpression(
40+
logicalOperator,
41+
logicalLeft,
42+
logicalRight,
43+
));
44+
} else if (isUnaryExpression(left)) {
45+
const logicalRight = AssignmentExpression(operator, left.argument, right);
46+
47+
replaceWith(path, UnaryExpression(
48+
logicalOperator,
49+
logicalRight,
50+
));
51+
}
52+
53+
path.node.extra = {
54+
parenthesized: true,
55+
};
56+
};
57+
58+
module.exports.traverse = ({push}) => ({
59+
AssignmentExpression(path) {
60+
if (isLogicalExpression(path.node.left))
61+
push(path);
62+
63+
if (isBinaryExpression(path.node.left))
64+
push(path);
65+
66+
if (isUnaryExpression(path.node.left))
67+
push(path);
68+
},
69+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['add-missing-for-assign', plugin],
9+
],
10+
});
11+
12+
test('putout: plugin-parens: add-missing-for-assign: report', (t) => {
13+
t.report('add-missing-for-assign', `SyntaxError: Invalid left-hand side in assignment expression`);
14+
t.end();
15+
});
16+
17+
test('putout: plugin-parens: add-missing-for-assign: transform', (t) => {
18+
t.transform('add-missing-for-assign');
19+
t.end();
20+
});
21+
22+
test('putout: plugin-parens: add-missing-for-assign: transform: binary', (t) => {
23+
t.transform('binary');
24+
t.end();
25+
});
26+
27+
test('putout: plugin-parens: add-missing-for-assign: transform: unary', (t) => {
28+
t.transform('unary');
29+
t.end();
30+
});

0 commit comments

Comments
 (0)