Skip to content

Commit 4f0e164

Browse files
committed
feature: @putout/opereate: hasParens: add
1 parent 3d83697 commit 4f0e164

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

packages/operate/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ Remove parens around expression depending on used `printer`:
276276
removeParens(path);
277277
```
278278

279+
### `hasParens(path: Path)`
280+
281+
Check if `path` has parens around expression depending on used `printer`:
282+
283+
- ✅ checks `node.extra.parenthesized` when `@putout/printer` used;
284+
- ✅ check if parent node type is `ParenthesizedExpression` or `TSParenthesizedType` when `babel` used;
285+
286+
```js
287+
hasParens(path);
288+
```
289+
279290
## License
280291

281292
MIT

packages/operate/lib/operate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ const {rename} = require('./rename');
1212
const {renameProperty} = require('./rename-property');
1313
const {setLiteralValue} = require('./set-literal-value');
1414
const {getPathAfterRequires} = require('./get-path-after-requires');
15-
const {addParens, removeParens} = require('./parens');
15+
const {
16+
addParens,
17+
removeParens,
18+
hasParens,
19+
} = require('./parens');
1620

1721
const {
1822
getProperty,
@@ -50,6 +54,7 @@ module.exports.renameProperty = renameProperty;
5054
module.exports.setLiteralValue = setLiteralValue;
5155

5256
module.exports.addParens = addParens;
57+
module.exports.hasParens = hasParens;
5358
module.exports.removeParens = removeParens;
5459

5560
module.exports.getProperty = getProperty;

packages/operate/lib/parens.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ 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+
};
19+
920
module.exports.addParens = (path) => {
1021
const printer = getPrinter(path);
1122

packages/operate/lib/parens.spec.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ const {
88
traverse,
99
} = require('putout');
1010

11-
const {addParens, removeParens} = require('./parens');
11+
const {
12+
addParens,
13+
removeParens,
14+
hasParens,
15+
} = require('./parens');
1216

1317
test('putout: operate: parens: removeParens: putout', (t) => {
1418
const source = '(b = 3)';
@@ -112,3 +116,56 @@ test('putout: operate: parens: addParens: babel: ts', (t) => {
112116
t.equal(result, expected);
113117
t.end();
114118
});
119+
120+
test('putout: operate: parens: hasParens: babel: ts', (t) => {
121+
let result = false;
122+
const source = 'const a: (boolean) = true;';
123+
const ast = parse(source, {
124+
printer: 'babel',
125+
isTS: true,
126+
});
127+
128+
traverse(ast, {
129+
TSBooleanKeyword(path) {
130+
result = hasParens(path);
131+
path.stop();
132+
},
133+
});
134+
135+
t.ok(result);
136+
t.end();
137+
});
138+
139+
test('putout: operate: parens: hasParens: babel', (t) => {
140+
let result = false;
141+
const source = '(b = 3)';
142+
const ast = parse(source, {
143+
printer: 'babel',
144+
});
145+
146+
traverse(ast, {
147+
AssignmentExpression(path) {
148+
result = hasParens(path);
149+
path.stop();
150+
},
151+
});
152+
153+
t.ok(result);
154+
t.end();
155+
});
156+
157+
test('putout: operate: parens: hasParens', (t) => {
158+
let result = false;
159+
const source = '(b = 3)';
160+
const ast = parse(source);
161+
162+
traverse(ast, {
163+
AssignmentExpression(path) {
164+
result = hasParens(path);
165+
path.stop();
166+
},
167+
});
168+
169+
t.ok(result);
170+
t.end();
171+
});

packages/operate/test/operate.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,3 +1092,18 @@ test('putout: operate: removeParens', (t) => {
10921092
t.equal(result, expected);
10931093
t.end();
10941094
});
1095+
1096+
test('putout: operate: hasParens', (t) => {
1097+
let result;
1098+
const source = 'b = 3';
1099+
const ast = parse(source);
1100+
1101+
traverse(ast, {
1102+
AssignmentExpression: (path) => {
1103+
result = operate.hasParens(path);
1104+
},
1105+
});
1106+
1107+
t.notOk(result);
1108+
t.end();
1109+
});

0 commit comments

Comments
 (0)