Skip to content

Commit 6102ae4

Browse files
committed
feature: @putout/operator-parens: addParens/removeParens: do nothingh when parens exists/absent
1 parent 3489df0 commit 6102ae4

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

packages/operator-parens/lib/parens.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ const {
66
TSParenthesizedType,
77
} = types;
88

9-
module.exports.hasParens = (path) => {
10-
const printer = getPrinter(path);
11-
12-
if (printer !== 'babel')
13-
return path.node.extra?.parenthesized;
14-
15-
const {type} = path.parentPath;
16-
17-
return /^(TS)?Parenthesized(Expression|Type)?$/.test(type);
18-
};
9+
module.exports.hasParens = hasParens;
1910

2011
module.exports.addParens = (path) => {
2112
const printer = getPrinter(path);
2213

14+
if (hasParens(path, printer))
15+
return path;
16+
2317
if (printer !== 'babel') {
2418
const {extra = {}} = path.node;
2519

@@ -42,6 +36,9 @@ module.exports.addParens = (path) => {
4236
module.exports.removeParens = (path) => {
4337
const printer = getPrinter(path);
4438

39+
if (!hasParens(path, printer))
40+
return path;
41+
4542
if (printer !== 'babel') {
4643
path.node.extra.parenthesized = false;
4744
return path;
@@ -59,3 +56,12 @@ function getPrinter(path) {
5956

6057
return programPath.node.extra.__putout_printer;
6158
}
59+
60+
function hasParens(path, printer = getPrinter(path)) {
61+
if (printer !== 'babel')
62+
return path.node.extra?.parenthesized;
63+
64+
const {type} = path.parentPath;
65+
66+
return /^(TS)?Parenthesized(Expression|Type)?$/.test(type);
67+
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ test('putout: operator: parens: removeParens: babel', (t) => {
6969
t.end();
7070
});
7171

72+
test('putout: operator: parens: removeParens: babel: no parens', (t) => {
73+
const source = 'b = 3';
74+
const ast = parse(source, {
75+
printer: 'babel',
76+
});
77+
78+
traverse(ast, {
79+
NumericLiteral(path) {
80+
removeParens(path);
81+
path.stop();
82+
},
83+
});
84+
85+
const result = print(ast, {
86+
printer: 'babel',
87+
});
88+
89+
const expected = 'b = 3;\n';
90+
91+
t.equal(result, expected);
92+
t.end();
93+
});
94+
7295
test('putout: operator: parens: removeParens: babel: return', (t) => {
7396
const source = '(b = 3)';
7497
const ast = parse(source, {
@@ -145,6 +168,29 @@ test('putout: operator: parens: addParens: babel', (t) => {
145168
t.end();
146169
});
147170

171+
test('putout: operator: parens: addParens: babel: has parens', (t) => {
172+
const source = '(b = 3)';
173+
const ast = parse(source, {
174+
printer: 'babel',
175+
});
176+
177+
traverse(ast, {
178+
AssignmentExpression(path) {
179+
addParens(path);
180+
path.stop();
181+
},
182+
});
183+
184+
const result = print(ast, {
185+
printer: 'babel',
186+
});
187+
188+
const expected = '(b = 3);\n';
189+
190+
t.equal(result, expected);
191+
t.end();
192+
});
193+
148194
test('putout: operator: parens: addParens: babel: return', (t) => {
149195
const source = 'b = 3';
150196
const ast = parse(source, {

0 commit comments

Comments
 (0)