From ccf509abb6123997fa249136f99e9dd148a65d3a Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Thu, 4 May 2017 21:06:30 -0500 Subject: [PATCH] Fix empty line with flow union (#1511) --- src/printer.js | 11 +++++--- .../__snapshots__/jsfmt.spec.js.snap | 26 +++++++++++++++++++ tests/flow_generic/union.js | 4 +++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/flow_generic/union.js diff --git a/src/printer.js b/src/printer.js index cbbd1168b..4f0b5d4f1 100644 --- a/src/printer.js +++ b/src/printer.js @@ -748,9 +748,9 @@ function genericPrintNoParens(path, options, print, args) { case "TSInterfaceDeclaration": case "TSTypeLiteral": { var isTypeAnnotation = n.type === "ObjectTypeAnnotation"; - var isTypeScriptTypeAnnotaion = n.type === "TSTypeLiteral"; + var isTypeScriptTypeAnnotation = n.type === "TSTypeLiteral"; var isTypeScriptInterfaceDeclaration = n.type === "TSInterfaceDeclaration"; - var isTypeScriptType = isTypeScriptTypeAnnotaion || isTypeScriptInterfaceDeclaration; + var isTypeScriptType = isTypeScriptTypeAnnotation || isTypeScriptInterfaceDeclaration; // Leave this here because we *might* want to make this // configurable later -- flow accepts ";" for type separators, // typescript accepts ";" and newlines @@ -1903,8 +1903,11 @@ function genericPrintNoParens(path, options, print, args) { const parent = path.getParentNode(); // If there's a leading comment, the parent is doing the indentation - const shouldIndent = !(parent.type === "TypeAlias" && - hasLeadingOwnLineComment(options.originalText, n)); + const shouldIndent = ( + parent.type !== "TypeParameterInstantiation" && + !(parent.type === "TypeAlias" && + hasLeadingOwnLineComment(options.originalText, n)) + ); //const token = isIntersection ? "&" : "|"; const code = concat([ diff --git a/tests/flow_generic/__snapshots__/jsfmt.spec.js.snap b/tests/flow_generic/__snapshots__/jsfmt.spec.js.snap index e5981fbbd..464cea15c 100644 --- a/tests/flow_generic/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_generic/__snapshots__/jsfmt.spec.js.snap @@ -137,3 +137,29 @@ type State = { }; `; + +exports[`union.js 1`] = ` +type Foo = Promise< + { ok: true, bar: string, baz: SomeOtherLongType } | + { ok: false, bar: SomeOtherLongType } +>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +type Foo = Promise< + | { ok: true, bar: string, baz: SomeOtherLongType } + | { ok: false, bar: SomeOtherLongType } +>; + +`; + +exports[`union.js 2`] = ` +type Foo = Promise< + { ok: true, bar: string, baz: SomeOtherLongType } | + { ok: false, bar: SomeOtherLongType } +>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +type Foo = Promise< + | { ok: true, bar: string, baz: SomeOtherLongType } + | { ok: false, bar: SomeOtherLongType }, +>; + +`; diff --git a/tests/flow_generic/union.js b/tests/flow_generic/union.js new file mode 100644 index 000000000..0bb92580b --- /dev/null +++ b/tests/flow_generic/union.js @@ -0,0 +1,4 @@ +type Foo = Promise< + { ok: true, bar: string, baz: SomeOtherLongType } | + { ok: false, bar: SomeOtherLongType } +>;