From 79dde93068a7ab5bee72af2685448209023c7146 Mon Sep 17 00:00:00 2001
From: Priyantha Lankapura <403912+lankaapura@users.noreply.github.com>
Date: Thu, 8 Feb 2018 22:58:38 +0530
Subject: [PATCH] Fix formatting of conditional types (#21762)
* Add test for formatting of conditional types
* Fix formatting of conditional types
---
src/services/formatting/rules.ts | 4 +++-
.../cases/fourslash/formattingConditionalOperator.ts | 2 +-
tests/cases/fourslash/formattingConditionalTypes.ts | 12 ++++++++++++
3 files changed, 16 insertions(+), 2 deletions(-)
create mode 100644 tests/cases/fourslash/formattingConditionalTypes.ts
diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts
index d8c94e8c772..0371fff09c4 100644
--- a/src/services/formatting/rules.ts
+++ b/src/services/formatting/rules.ts
@@ -409,6 +409,7 @@ namespace ts.formatting {
switch (context.contextNode.kind) {
case SyntaxKind.BinaryExpression:
case SyntaxKind.ConditionalExpression:
+ case SyntaxKind.ConditionalType:
case SyntaxKind.AsExpression:
case SyntaxKind.ExportSpecifier:
case SyntaxKind.ImportSpecifier:
@@ -461,7 +462,8 @@ namespace ts.formatting {
}
function isConditionalOperatorContext(context: FormattingContext): boolean {
- return context.contextNode.kind === SyntaxKind.ConditionalExpression;
+ return context.contextNode.kind === SyntaxKind.ConditionalExpression ||
+ context.contextNode.kind === SyntaxKind.ConditionalType;
}
function isSameLineTokenOrBeforeBlockContext(context: FormattingContext): boolean {
diff --git a/tests/cases/fourslash/formattingConditionalOperator.ts b/tests/cases/fourslash/formattingConditionalOperator.ts
index 545adb572b1..308f39315a3 100644
--- a/tests/cases/fourslash/formattingConditionalOperator.ts
+++ b/tests/cases/fourslash/formattingConditionalOperator.ts
@@ -3,4 +3,4 @@
////var x=true?1:2
format.document();
goTo.bof();
-verify.currentLineContentIs("var x = true ? 1 : 2");;
\ No newline at end of file
+verify.currentLineContentIs("var x = true ? 1 : 2");
\ No newline at end of file
diff --git a/tests/cases/fourslash/formattingConditionalTypes.ts b/tests/cases/fourslash/formattingConditionalTypes.ts
new file mode 100644
index 00000000000..1d9526ef7bc
--- /dev/null
+++ b/tests/cases/fourslash/formattingConditionalTypes.ts
@@ -0,0 +1,12 @@
+///
+
+/////*L1*/type Diff1 = T extends U?never:T;
+/////*L2*/type Diff2 = T extends U ? never : T;
+
+format.document();
+
+goTo.marker("L1");
+verify.currentLineContentIs("type Diff1 = T extends U ? never : T;");
+
+goTo.marker("L2");
+verify.currentLineContentIs("type Diff2 = T extends U ? never : T;");
\ No newline at end of file