Skip to content

Commit 49e21b1

Browse files
author
Andy Hanson
committed
Fix for ConditionalExpression
1 parent 7d56deb commit 49e21b1

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,13 +1363,13 @@ namespace ts {
13631363

13641364
emitExpression(node.condition);
13651365
increaseIndentIf(indentBeforeQuestion, " ");
1366-
write("?");
1366+
emit(node.questionToken);
13671367
increaseIndentIf(indentAfterQuestion, " ");
13681368
emitExpression(node.whenTrue);
13691369
decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion);
13701370

13711371
increaseIndentIf(indentBeforeColon, " ");
1372-
write(":");
1372+
emit(node.colonToken);
13731373
increaseIndentIf(indentAfterColon, " ");
13741374
emitExpression(node.whenFalse);
13751375
decreaseIndentIf(indentBeforeColon, indentAfterColon);

src/compiler/factory.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,11 +1139,20 @@ namespace ts {
11391139
return node;
11401140
}
11411141

1142-
export function updateConditional(node: ConditionalExpression, condition: Expression, whenTrue: Expression, whenFalse: Expression) {
1142+
export function updateConditional(
1143+
node: ConditionalExpression,
1144+
condition: Expression,
1145+
whenTrue: Expression,
1146+
whenFalse: Expression,
1147+
questionToken: Token<SyntaxKind.QuestionToken> = node.questionToken,
1148+
colonToken: Token<SyntaxKind.ColonToken> = node.colonToken,
1149+
) {
11431150
return node.condition !== condition
1151+
|| node.questionToken !== questionToken
11441152
|| node.whenTrue !== whenTrue
1153+
|| node.colonToken !== colonToken
11451154
|| node.whenFalse !== whenFalse
1146-
? updateNode(createConditional(condition, node.questionToken, whenTrue, node.colonToken, whenFalse), node)
1155+
? updateNode(createConditional(condition, questionToken, whenTrue, colonToken, whenFalse), node)
11471156
: node;
11481157
}
11491158

src/compiler/visitor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ namespace ts {
525525
return updateConditional(<ConditionalExpression>node,
526526
visitNode((<ConditionalExpression>node).condition, visitor, isExpression),
527527
visitNode((<ConditionalExpression>node).whenTrue, visitor, isExpression),
528-
visitNode((<ConditionalExpression>node).whenFalse, visitor, isExpression));
528+
visitNode((<ConditionalExpression>node).whenFalse, visitor, isExpression),
529+
visitNode((<ConditionalExpression>node).questionToken, visitor, n => n.kind === SyntaxKind.QuestionToken),
530+
visitNode((<ConditionalExpression>node).colonToken, visitor, n => n.kind === SyntaxKind.ColonToken));
529531

530532
case SyntaxKind.TemplateExpression:
531533
return updateTemplateExpression(<TemplateExpression>node,

tests/cases/fourslash/extract-method5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ verify.currentFileContentIs(
2020
var x: 1 | 2 | 3 = newFunction();
2121
2222
function newFunction(): 1 | 2 | 3 {
23-
return 1 + 1 === 2?1: 2;
23+
return 1 + 1 === 2 ? 1: 2;
2424
}
2525
}`);

0 commit comments

Comments
 (0)