Skip to content

Commit 6a76d2a

Browse files
committed
Remove makeTypeAnnotationBreakable(), introduce canBreak()
1 parent 89e92bd commit 6a76d2a

12 files changed

Lines changed: 158 additions & 130 deletions

File tree

src/document/doc-utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,20 @@ function replaceEndOfLineWith(text, replacement) {
401401
return join(replacement, text.split("\n")).parts;
402402
}
403403

404+
function canBreakFn(doc) {
405+
if (doc.type === "line") {
406+
return true;
407+
}
408+
}
409+
410+
function canBreak(doc) {
411+
return findInDoc(doc, canBreakFn, false);
412+
}
413+
404414
module.exports = {
405415
isConcat,
406416
getDocParts,
417+
canBreak,
407418
willBreak,
408419
traverseDoc,
409420
findInDoc,

src/language-js/print/assignment.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { isNonEmptyArray, getStringWidth } = require("../../common/util");
44
const {
5-
builders: { group, ifBreak, indent, indentIfBreak, line, softline },
5+
builders: { line, group, indent, indentIfBreak },
66
utils: { cleanDoc, willBreak },
77
} = require("../../document");
88
const {
@@ -56,12 +56,7 @@ function printAssignment(
5656
}
5757

5858
case "break-lhs": {
59-
return group([
60-
makeTypeAnnotationBreakable(leftDoc),
61-
operator,
62-
" ",
63-
group(rightDoc),
64-
]);
59+
return group([leftDoc, operator, " ", group(rightDoc)]);
6560
}
6661

6762
// Parts of assignment chains aren't wrapped in groups.
@@ -467,32 +462,6 @@ function getTypeArgumentsFromCallExpression(node) {
467462
);
468463
}
469464

470-
function makeTypeAnnotationBreakable(leftDoc) {
471-
if (!Array.isArray(leftDoc)) {
472-
return leftDoc;
473-
}
474-
475-
const [variableName, space, typeAnnotationDoc] = leftDoc;
476-
477-
if (!typeAnnotationDoc || !Array.isArray(typeAnnotationDoc)) {
478-
return leftDoc;
479-
}
480-
481-
return [
482-
variableName,
483-
space,
484-
[
485-
typeAnnotationDoc[0],
486-
group([
487-
ifBreak("("),
488-
indent([softline, typeAnnotationDoc[1]]),
489-
softline,
490-
ifBreak(")"),
491-
]),
492-
],
493-
];
494-
}
495-
496465
module.exports = {
497466
printVariableDeclarator,
498467
printAssignmentExpression,

src/language-js/print/typescript.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
conditionalGroup,
1414
ifBreak,
1515
},
16+
utils: { canBreak },
1617
} = require("../../document");
1718
const {
1819
isLiteral,
@@ -505,8 +506,17 @@ function printTypescript(path, options, print) {
505506
print("typeName"),
506507
printTypeParameters(path, options, print, "typeParameters"),
507508
];
508-
case "TSTypeAnnotation":
509-
return print("typeAnnotation");
509+
case "TSTypeAnnotation": {
510+
const typeAnnotationDoc = print("typeAnnotation");
511+
return canBreak(typeAnnotationDoc)
512+
? typeAnnotationDoc
513+
: group([
514+
ifBreak("("),
515+
indent([softline, typeAnnotationDoc]),
516+
softline,
517+
ifBreak(")"),
518+
]);
519+
}
510520
case "TSEmptyBodyFunctionExpression":
511521
return printMethodInternal(path, options, print);
512522

tests/format/flow/assignments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
9898
const map: Map<Function, Foo<S>> = new Map();
9999
100100
=====================================output=====================================
101-
const map: (
102-
Map<Function, Map<string | void, { value: UnloadedDescriptor }>>
103-
) = new Map();
101+
const map: Map<
102+
Function,
103+
Map<string | void, { value: UnloadedDescriptor }>
104+
> = new Map();
104105
105106
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
106107
new Map();

tests/format/flow/function-type-param/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export const testFunctionOnOptionsAsArgument: <T1,a>(?a, ((?a) => T1)) => T1 = f
1717
=====================================output=====================================
1818
let f: <A>(((?A) => B)) => B;
1919
20-
export const testFunctionOnOptionsAsArgument: (
21-
<T1, a>(?a, ((?a) => T1)) => T1
22-
) = function _(Arg1, Arg2) {
20+
export const testFunctionOnOptionsAsArgument: <T1, a>(
21+
?a,
22+
((?a) => T1)
23+
) => T1 = function _(Arg1, Arg2) {
2324
const result = TypesBS.testFunctionOnOptionsAsArgument(
2425
Arg1 == null ? undefined : Arg1,
2526
Arg2

tests/format/typescript/assignment/__snapshots__/jsfmt.spec.js.snap

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ printWidth: 80
99
export const listAuthorizedSitesForDefaultHandler: ListAuthorizedSitesForHandler = aListAuthorizedSitesForResponse;
1010
1111
=====================================output=====================================
12-
export const listAuthorizedSitesForDefaultHandler: ListAuthorizedSitesForHandler =
13-
aListAuthorizedSitesForResponse;
12+
export const listAuthorizedSitesForDefaultHandler: (
13+
ListAuthorizedSitesForHandler
14+
) = aListAuthorizedSitesForResponse;
1415
1516
================================================================================
1617
`;
@@ -385,9 +386,9 @@ interface MyGenericComponentProps<T> {
385386
y: T;
386387
}
387388
388-
const MyGenericComponent: (
389-
React.VoidFunctionComponent<MyGenericComponentProps<number>>
390-
) = ({ x, y }) => {
389+
const MyGenericComponent: React.VoidFunctionComponent<
390+
MyGenericComponentProps<number>
391+
> = ({ x, y }) => {
391392
const a = useA();
392393
return (
393394
<div>
@@ -417,13 +418,15 @@ const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
417418
const map: Map<Function, Foo<S>> = new Map();
418419
419420
=====================================output=====================================
420-
const map: (
421-
Map<Function, Map<string | void, { value: UnloadedDescriptor }>>
422-
) = new Map();
423-
424-
const map: (
425-
Map<Function, Condition extends Foo ? FooFooFoo : BarBarBar>
426-
) = new Map();
421+
const map: Map<
422+
Function,
423+
Map<string | void, { value: UnloadedDescriptor }>
424+
> = new Map();
425+
426+
const map: Map<
427+
Function,
428+
Condition extends Foo ? FooFooFoo : BarBarBar
429+
> = new Map();
427430
428431
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
429432
new Map();

tests/format/typescript/declare/trailing-comma/__snapshots__/jsfmt.spec.js.snap

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ declare function foo(...long_long_long_long_long_long_long_long_long_long_long_l
1313
=====================================output=====================================
1414
declare function foo(...args: any[]);
1515
declare function foo(
16-
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: any[]
16+
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: (
17+
any[]
18+
)
1719
);
1820
1921
================================================================================
@@ -32,7 +34,9 @@ declare function foo(...long_long_long_long_long_long_long_long_long_long_long_l
3234
=====================================output=====================================
3335
declare function foo(...args: any[]);
3436
declare function foo(
35-
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: any[]
37+
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: (
38+
any[]
39+
)
3640
);
3741
3842
================================================================================
@@ -51,7 +55,9 @@ declare function foo(...long_long_long_long_long_long_long_long_long_long_long_l
5155
=====================================output=====================================
5256
declare function foo(...args: any[]);
5357
declare function foo(
54-
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: any[]
58+
...long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_args: (
59+
any[]
60+
)
5561
);
5662
5763
================================================================================

tests/format/typescript/decorators/__snapshots__/jsfmt.spec.js.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ class Bar {
413413
}
414414
415415
class MyContainerComponent {
416-
@ContentChildren(MyComponent)
417-
components: QueryListSomeBigName<MyComponentThat>;
416+
@ContentChildren(MyComponent) components: (
417+
QueryListSomeBigName<MyComponentThat>
418+
);
418419
}
419420
420421
================================================================================

tests/format/typescript/error-recovery/__snapshots__/jsfmt.spec.js.snap

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ type TooLong = {
218218
]: string;
219219
};
220220
type TooLong81 = {
221-
[
222-
loooooooooooooooooooooooooong: string,
223-
loooooooooooooooooong: string,
224-
]: string;
221+
[loooooooooooooooooooooooooong: string, loooooooooooooooooong: string]: (
222+
string
223+
);
225224
};
226225
type TooLong80 = {
227226
[loooooooooooooooooooooooooong: string, looooooooooooooooong: string]: string;
@@ -230,7 +229,9 @@ type TooLong80 = {
230229
// note lack of trailing comma in the index signature
231230
type TooLongSingleParam = {
232231
[
233-
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
232+
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: (
233+
string
234+
)
234235
]: string;
235236
};
236237
@@ -292,10 +293,9 @@ type TooLong = {
292293
]: string;
293294
};
294295
type TooLong81 = {
295-
[
296-
loooooooooooooooooooooooooong: string,
297-
loooooooooooooooooong: string,
298-
]: string;
296+
[loooooooooooooooooooooooooong: string, loooooooooooooooooong: string]: (
297+
string
298+
);
299299
};
300300
type TooLong80 = {
301301
[loooooooooooooooooooooooooong: string, looooooooooooooooong: string]: string;
@@ -304,7 +304,9 @@ type TooLong80 = {
304304
// note lack of trailing comma in the index signature
305305
type TooLongSingleParam = {
306306
[
307-
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
307+
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: (
308+
string
309+
)
308310
]: string;
309311
};
310312
@@ -354,10 +356,9 @@ type TooLong = {
354356
]: string;
355357
};
356358
type TooLong81 = {
357-
[
358-
loooooooooooooooooooooooooong: string,
359-
loooooooooooooooooong: string,
360-
]: string;
359+
[loooooooooooooooooooooooooong: string, loooooooooooooooooong: string]: (
360+
string
361+
);
361362
};
362363
type TooLong80 = {
363364
[loooooooooooooooooooooooooong: string, looooooooooooooooong: string]: string;
@@ -366,7 +367,9 @@ type TooLong80 = {
366367
// note lack of trailing comma in the index signature
367368
type TooLongSingleParam = {
368369
[
369-
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
370+
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: (
371+
string
372+
)
370373
]: string;
371374
};
372375

tests/format/typescript/generic/__snapshots__/jsfmt.spec.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export const getVehicleDescriptor = async (
7373
7474
export const getVehicleDescriptor = async (
7575
vehicleId: string
76-
): Promise<Collections.Parts.PrintedCircuitBoardAssemblyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy> => {};
76+
): (
77+
Promise<Collections.Parts.PrintedCircuitBoardAssemblyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy>
78+
) => {};
7779
7880
export const getVehicleDescriptor = async (
7981
vehicleId: string

0 commit comments

Comments
 (0)