From 4b47c0cb825bcd70a65a33ed24744d67c9b06f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Tue, 22 May 2018 14:01:04 +0800 Subject: [PATCH] fix wrong formatting with multiline type literals with IntersectionType and UnionType --- src/services/formatting/smartIndenter.ts | 6 +++ ...matLiteralTypeInUnionOrIntersectionType.ts | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/cases/fourslash/formatLiteralTypeInUnionOrIntersectionType.ts diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index a75781b8da0e4..4c93d7816e75d 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -569,6 +569,12 @@ namespace ts.formatting { return childKind !== SyntaxKind.JsxClosingElement; case SyntaxKind.JsxFragment: return childKind !== SyntaxKind.JsxClosingFragment; + case SyntaxKind.IntersectionType: + case SyntaxKind.UnionType: + if (childKind === SyntaxKind.TypeLiteral) { + return false; + } + // falls through } // No explicit rule for given nodes so the result will follow the default value argument return indentByDefault; diff --git a/tests/cases/fourslash/formatLiteralTypeInUnionOrIntersectionType.ts b/tests/cases/fourslash/formatLiteralTypeInUnionOrIntersectionType.ts new file mode 100644 index 0000000000000..81bba56adf20f --- /dev/null +++ b/tests/cases/fourslash/formatLiteralTypeInUnionOrIntersectionType.ts @@ -0,0 +1,37 @@ +/// + +//// type NumberAndString = { +//// a: number +//// } & { +//// b: string +//// }; +//// +//// type NumberOrString = { +//// a: number +//// } | { +//// b: string +//// }; +//// +//// type Complexed = +//// Foo & +//// Bar | +//// Baz; + + +format.document(); +verify.currentFileContentIs(`type NumberAndString = { + a: number +} & { + b: string +}; + +type NumberOrString = { + a: number +} | { + b: string +}; + +type Complexed = + Foo & + Bar | + Baz;`);