Skip to content

Commit 99c7207

Browse files
committed
feature: @putout/printer: TSUnionType: always add parens when inside TSArrayType (benjamn/recast#1416)
1 parent 7fb5435 commit 99c7207

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

lib/tokenize/statements/if-statement/if-statement.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const {types} = require('@putout/babel');
44

55
const {markAfter} = require('../../mark');
66
const {exists, isNext} = require('../../is');
7+
78
const {
89
isBlockStatement,
910
isFunctionDeclaration,
1011
} = types;
12+
1113
const isInside = ({parentPath}) => !parentPath.parentPath.isProgram();
1214
const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
1315

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type A = (number | string)[];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type A = (number | string)[];

lib/tokenize/typescript/ts-union-type/ts-union-type.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
const {maybeParens} = require('../../maybe/maybe-parens');
44
const insideTypeDeclaration = ({parentPath}) => parentPath.isTSTypeAliasDeclaration();
55

6-
module.exports.TSUnionType = maybeParens((path, printer, {maxTypesInOneLine}) => {
7-
const types = path.get('types');
8-
const {length} = types;
9-
10-
if (!insideTypeDeclaration(path) || length <= maxTypesInOneLine)
11-
printInOneLine(types, printer);
12-
else
13-
printInCoupleLines(types, printer);
6+
module.exports.TSUnionType = maybeParens({
7+
condition: (path) => {
8+
return path.parentPath.isTSArrayType();
9+
},
10+
print: (path, printer, {maxTypesInOneLine}) => {
11+
const types = path.get('types');
12+
const {length} = types;
13+
14+
if (!insideTypeDeclaration(path) || length <= maxTypesInOneLine)
15+
printInOneLine(types, printer);
16+
else
17+
printInCoupleLines(types, printer);
18+
},
1419
});
1520

1621
function printInOneLine(types, {traverse, write}) {

lib/tokenize/typescript/ts-union-type/ts-union-type.spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {extend} = require('supertape');
44

5-
const {parse} = require('putout');
5+
const {parse, transform} = require('putout');
66
const {printExtension} = require('../../../../test/printer');
77
const {readFixtures} = require('../../../../test/fixture');
88

@@ -43,3 +43,27 @@ test('printer: tokenizer: ts-union-type: override', (t) => {
4343
t.equal(result, fixture.tsUnionTypeOverrideFix);
4444
t.end();
4545
});
46+
47+
test('printer: tokenizer: typescript: ts-union-type: parens', (t) => {
48+
const source = fixture.tsUnionTypeParens;
49+
const ast = parse(source, {
50+
isTS: true,
51+
});
52+
53+
transform(ast, source, {
54+
plugins: [
55+
['remove-parens', {
56+
report: () => '',
57+
include: () => ['TSUnionType'],
58+
fix: (path) => {
59+
delete path.node.extra;
60+
},
61+
}],
62+
],
63+
});
64+
65+
const result = print(ast);
66+
67+
t.equal(result, fixture.tsUnionTypeParensFix);
68+
t.end();
69+
});

0 commit comments

Comments
 (0)