Skip to content

Commit 53b4b91

Browse files
committed
feature: @putout/operator-parens: addParens/removeParens: return path
1 parent be567e3 commit 53b4b91

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

packages/operator-parens/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ npm i putout @putout/operator-parens
1313

1414
## API
1515

16-
### `addParens(path: Path)`
16+
### `addParens(path: Path): Path`
1717

1818
Add parens around expression depending on used `printer`:
1919

@@ -28,7 +28,7 @@ const {addParens} = operator;
2828
addParens(path);
2929
```
3030

31-
### `removeParens(path: Path)`
31+
### `removeParens(path: Path): Path`
3232

3333
Remove parens around expression depending on used `printer`:
3434

@@ -43,7 +43,7 @@ const {removeParens} = operator;
4343
removeParens(path);
4444
```
4545

46-
### `hasParens(path: Path)`
46+
### `hasParens(path: Path): Boolean`
4747

4848
Check if `path` has parens around expression depending on used `printer`:
4949

packages/operator-parens/lib/parens.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports.addParens = (path) => {
2626
extra.parenthesized = true;
2727
path.node.extra = extra;
2828

29-
return;
29+
return path;
3030
}
3131

3232
const {node} = path;
@@ -35,16 +35,22 @@ module.exports.addParens = (path) => {
3535
return path.replaceWith(TSParenthesizedType(node));
3636

3737
path.replaceWith(ParenthesizedExpression(node));
38+
39+
return path;
3840
};
3941

4042
module.exports.removeParens = (path) => {
4143
const printer = getPrinter(path);
4244

43-
if (printer !== 'babel')
44-
return path.node.extra.parenthesized = false;
45+
if (printer !== 'babel') {
46+
path.node.extra.parenthesized = false;
47+
return path;
48+
}
4549

4650
const {node} = path;
4751
path.parentPath.replaceWith(node);
52+
53+
return path;
4854
};
4955

5056
function getPrinter(path) {

packages/operator-parens/lib/parens.spec.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ test('putout: operator: parens: removeParens: putout', (t) => {
1919
const ast = parse(source);
2020

2121
traverse(ast, {
22-
AssignmentExpression: removeParens,
22+
AssignmentExpression: (path) => {
23+
removeParens(path);
24+
},
2325
});
2426

2527
const result = print(ast);
@@ -29,6 +31,21 @@ test('putout: operator: parens: removeParens: putout', (t) => {
2931
t.end();
3032
});
3133

34+
test('putout: operator: parens: removeParens: putout: return', (t) => {
35+
const source = '(b = 3)';
36+
const ast = parse(source);
37+
let result;
38+
39+
traverse(ast, {
40+
AssignmentExpression: (path) => {
41+
result = removeParens(path);
42+
},
43+
});
44+
45+
t.equal(result.type, 'AssignmentExpression');
46+
t.end();
47+
});
48+
3249
test('putout: operator: parens: removeParens: babel', (t) => {
3350
const source = '(b = 3)';
3451
const ast = parse(source, {
@@ -52,6 +69,25 @@ test('putout: operator: parens: removeParens: babel', (t) => {
5269
t.end();
5370
});
5471

72+
test('putout: operator: parens: removeParens: babel: return', (t) => {
73+
const source = '(b = 3)';
74+
const ast = parse(source, {
75+
printer: 'babel',
76+
});
77+
78+
let result;
79+
80+
traverse(ast, {
81+
AssignmentExpression(path) {
82+
result = removeParens(path);
83+
path.stop();
84+
},
85+
});
86+
87+
t.equal(result.type, 'AssignmentExpression');
88+
t.end();
89+
});
90+
5591
test('putout: operator: parens: addParens', (t) => {
5692
const source = 'const b = a';
5793
const ast = parse(source);
@@ -70,6 +106,22 @@ test('putout: operator: parens: addParens', (t) => {
70106
t.end();
71107
});
72108

109+
test('putout: operator: parens: addParens: return', (t) => {
110+
const source = 'const b = a';
111+
const ast = parse(source);
112+
let result;
113+
114+
traverse(ast, {
115+
VariableDeclarator(path) {
116+
result = addParens(path.get('init'));
117+
path.stop();
118+
},
119+
});
120+
121+
t.equal(result.type, 'Identifier');
122+
t.end();
123+
});
124+
73125
test('putout: operator: parens: addParens: babel', (t) => {
74126
const source = 'b = 3';
75127
const ast = parse(source, {
@@ -93,6 +145,25 @@ test('putout: operator: parens: addParens: babel', (t) => {
93145
t.end();
94146
});
95147

148+
test('putout: operator: parens: addParens: babel: return', (t) => {
149+
const source = 'b = 3';
150+
const ast = parse(source, {
151+
printer: 'babel',
152+
});
153+
154+
let result;
155+
156+
traverse(ast, {
157+
AssignmentExpression(path) {
158+
result = addParens(path);
159+
path.stop();
160+
},
161+
});
162+
163+
t.equal(result.type, 'ParenthesizedExpression');
164+
t.end();
165+
});
166+
96167
test('putout: operator: parens: addParens: babel: ts', (t) => {
97168
const source = 'const a: boolean = true;';
98169
const ast = parse(source, {

0 commit comments

Comments
 (0)