Skip to content

Commit

Permalink
[flow] Do not break on empty type parameters (#3281)
Browse files Browse the repository at this point in the history
Not only does breaking look bad but when you have trailing comma enabled, it puts a comma there which is not valid.
  • Loading branch information
vjeux committed Nov 16, 2017
1 parent 258de9d commit 4b070c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3517,13 +3517,14 @@ function printTypeParameters(path, options, print, paramsKey) {
}

const shouldInline =
n[paramsKey].length === 1 &&
(shouldHugType(n[paramsKey][0]) ||
(n[paramsKey][0].type === "GenericTypeAnnotation" &&
shouldHugType(n[paramsKey][0].id)) ||
(n[paramsKey][0].type === "TSTypeReference" &&
shouldHugType(n[paramsKey][0].typeName)) ||
n[paramsKey][0].type === "NullableTypeAnnotation");
n[paramsKey].length === 0 ||
(n[paramsKey].length === 1 &&
(shouldHugType(n[paramsKey][0]) ||
(n[paramsKey][0].type === "GenericTypeAnnotation" &&
shouldHugType(n[paramsKey][0].id)) ||
(n[paramsKey][0].type === "TSTypeReference" &&
shouldHugType(n[paramsKey][0].typeName)) ||
n[paramsKey][0].type === "NullableTypeAnnotation"));

if (shouldInline) {
return concat(["<", join(", ", path.map(print, paramsKey)), ">"]);
Expand Down
12 changes: 12 additions & 0 deletions tests/flow_type_parameters/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`empty_generic_break.js 1`] = `
class X {
a: B<> = SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class X {
a: B<> = SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong;
}
`;
3 changes: 3 additions & 0 deletions tests/flow_type_parameters/empty_generic_break.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class X {
a: B<> = SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong;
}
1 change: 1 addition & 0 deletions tests/flow_type_parameters/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, null, ["babylon"]);

0 comments on commit 4b070c3

Please sign in to comment.