From 38c828690738fb8eb43acd112f7f57941ee25640 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sun, 10 Dec 2017 05:32:17 -0600 Subject: [PATCH] Fix closing parens on multi-line intersection/union type (#3436) --- src/printer.js | 21 +++++++++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 20 ++++++++++++++++++ tests/flow_intersection/intersection.js | 7 +++++++ tests/flow_intersection/jsfmt.spec.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 15 +++++++++++++ tests/typescript_union/union-parens.ts | 8 +++++++ 6 files changed, 72 insertions(+) create mode 100644 tests/flow_intersection/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/flow_intersection/intersection.js create mode 100644 tests/flow_intersection/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index d886fe95..7539cde3 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2291,6 +2291,7 @@ function genericPrintNoParens(path, options, print, args) { // | C const parent = path.getParentNode(); + // If there's a leading comment, the parent is doing the indentation const shouldIndent = parent.type !== "TypeParameterInstantiation" && @@ -2330,6 +2331,26 @@ function genericPrintNoParens(path, options, print, args) { join(concat([line, "| "]), printed) ]); + let hasParens; + + if (n.type === "TSUnionType") { + const greatGrandParent = path.getParentNode(2); + const greatGreatGrandParent = path.getParentNode(3); + + hasParens = + greatGrandParent && + greatGrandParent.type === "TSParenthesizedType" && + greatGreatGrandParent && + (greatGreatGrandParent.type === "TSUnionType" || + greatGreatGrandParent.type === "TSIntersectionType"); + } else { + hasParens = path.needsParens(options); + } + + if (hasParens) { + return group(concat([indent(code), softline])); + } + return group(shouldIndent ? indent(code) : code); } case "NullableTypeAnnotation": diff --git a/tests/flow_intersection/__snapshots__/jsfmt.spec.js.snap b/tests/flow_intersection/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..afd19f19 --- /dev/null +++ b/tests/flow_intersection/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,20 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`intersection.js 1`] = ` +type State = { + sharedProperty: any; +} & ( + | { discriminant: "FOO"; foo: any } + | { discriminant: "BAR"; bar: any } + | { discriminant: "BAZ"; baz: any } +); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +type State = { + sharedProperty: any +} & ( + | { discriminant: "FOO", foo: any } + | { discriminant: "BAR", bar: any } + | { discriminant: "BAZ", baz: any } +); + +`; diff --git a/tests/flow_intersection/intersection.js b/tests/flow_intersection/intersection.js new file mode 100644 index 00000000..057b8ff9 --- /dev/null +++ b/tests/flow_intersection/intersection.js @@ -0,0 +1,7 @@ +type State = { + sharedProperty: any; +} & ( + | { discriminant: "FOO"; foo: any } + | { discriminant: "BAR"; bar: any } + | { discriminant: "BAZ"; baz: any } +); diff --git a/tests/flow_intersection/jsfmt.spec.js b/tests/flow_intersection/jsfmt.spec.js new file mode 100644 index 00000000..c1ba82f4 --- /dev/null +++ b/tests/flow_intersection/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["flow", "babylon"]); diff --git a/tests/typescript_union/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_union/__snapshots__/jsfmt.spec.js.snap index 20640ac2..560dc4de 100644 --- a/tests/typescript_union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_union/__snapshots__/jsfmt.spec.js.snap @@ -102,6 +102,14 @@ interface Interface { i: (X | Y) & Z; j: Partial<(X | Y)>; } + +type State = { + sharedProperty: any; +} & ( + | { discriminant: "FOO"; foo: any } + | { discriminant: "BAR"; bar: any } + | { discriminant: "BAZ"; baz: any } +); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ export type A = | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa @@ -133,6 +141,13 @@ interface Interface { j: Partial; } +type State = { + sharedProperty: any; +} & ( + | { discriminant: "FOO"; foo: any } + | { discriminant: "BAR"; bar: any } + | { discriminant: "BAZ"; baz: any }); + `; exports[`with-type-params.ts 1`] = ` diff --git a/tests/typescript_union/union-parens.ts b/tests/typescript_union/union-parens.ts index 2b1896fe..76b5a8a2 100644 --- a/tests/typescript_union/union-parens.ts +++ b/tests/typescript_union/union-parens.ts @@ -30,3 +30,11 @@ interface Interface { i: (X | Y) & Z; j: Partial<(X | Y)>; } + +type State = { + sharedProperty: any; +} & ( + | { discriminant: "FOO"; foo: any } + | { discriminant: "BAR"; bar: any } + | { discriminant: "BAZ"; baz: any } +);