Skip to content

Commit 5b44de1

Browse files
author
Andy Hanson
committed
Fix for ConditionalExpression
1 parent 7d56deb commit 5b44de1

10 files changed

+152
-101
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/baselines/reference/sourceMapValidationStatements.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationStatements.sourcemap.txt

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,15 +1251,19 @@ sourceFile:sourceMapValidationStatements.ts
12511251
7 > ^^^^
12521252
8 > ^
12531253
9 > ^
1254-
10> ^^^
1255-
11> ^
1256-
12> ^^^
1257-
13> ^
1258-
14> ^^^
1259-
15> ^
1260-
16> ^^^
1261-
17> ^
1262-
18> ^
1254+
10> ^
1255+
11> ^
1256+
12> ^
1257+
13> ^
1258+
14> ^^^
1259+
15> ^
1260+
16> ^
1261+
17> ^
1262+
18> ^
1263+
19> ^
1264+
20> ^^^
1265+
21> ^
1266+
22> ^
12631267
1->
12641268
>
12651269
2 > var
@@ -1270,15 +1274,19 @@ sourceFile:sourceMapValidationStatements.ts
12701274
7 > ==
12711275
8 > 1
12721276
9 > )
1273-
10> ?
1274-
11> x
1275-
12> +
1276-
13> 1
1277-
14> :
1278-
15> x
1279-
16> -
1280-
17> 1
1281-
18> ;
1277+
10>
1278+
11> ?
1279+
12>
1280+
13> x
1281+
14> +
1282+
15> 1
1283+
16>
1284+
17> :
1285+
18>
1286+
19> x
1287+
20> -
1288+
21> 1
1289+
22> ;
12821290
1->Emitted(74, 5) Source(72, 5) + SourceIndex(0)
12831291
2 >Emitted(74, 9) Source(72, 9) + SourceIndex(0)
12841292
3 >Emitted(74, 10) Source(72, 10) + SourceIndex(0)
@@ -1288,15 +1296,19 @@ sourceFile:sourceMapValidationStatements.ts
12881296
7 >Emitted(74, 19) Source(72, 19) + SourceIndex(0)
12891297
8 >Emitted(74, 20) Source(72, 20) + SourceIndex(0)
12901298
9 >Emitted(74, 21) Source(72, 21) + SourceIndex(0)
1291-
10>Emitted(74, 24) Source(72, 24) + SourceIndex(0)
1292-
11>Emitted(74, 25) Source(72, 25) + SourceIndex(0)
1293-
12>Emitted(74, 28) Source(72, 28) + SourceIndex(0)
1294-
13>Emitted(74, 29) Source(72, 29) + SourceIndex(0)
1295-
14>Emitted(74, 32) Source(72, 32) + SourceIndex(0)
1296-
15>Emitted(74, 33) Source(72, 33) + SourceIndex(0)
1297-
16>Emitted(74, 36) Source(72, 36) + SourceIndex(0)
1298-
17>Emitted(74, 37) Source(72, 37) + SourceIndex(0)
1299-
18>Emitted(74, 38) Source(72, 38) + SourceIndex(0)
1299+
10>Emitted(74, 22) Source(72, 22) + SourceIndex(0)
1300+
11>Emitted(74, 23) Source(72, 23) + SourceIndex(0)
1301+
12>Emitted(74, 24) Source(72, 24) + SourceIndex(0)
1302+
13>Emitted(74, 25) Source(72, 25) + SourceIndex(0)
1303+
14>Emitted(74, 28) Source(72, 28) + SourceIndex(0)
1304+
15>Emitted(74, 29) Source(72, 29) + SourceIndex(0)
1305+
16>Emitted(74, 30) Source(72, 30) + SourceIndex(0)
1306+
17>Emitted(74, 31) Source(72, 31) + SourceIndex(0)
1307+
18>Emitted(74, 32) Source(72, 32) + SourceIndex(0)
1308+
19>Emitted(74, 33) Source(72, 33) + SourceIndex(0)
1309+
20>Emitted(74, 36) Source(72, 36) + SourceIndex(0)
1310+
21>Emitted(74, 37) Source(72, 37) + SourceIndex(0)
1311+
22>Emitted(74, 38) Source(72, 38) + SourceIndex(0)
13001312
---
13011313
>>> (x == 1) ? x + 1 : x - 1;
13021314
1 >^^^^
@@ -1305,46 +1317,58 @@ sourceFile:sourceMapValidationStatements.ts
13051317
4 > ^^^^
13061318
5 > ^
13071319
6 > ^
1308-
7 > ^^^
1309-
8 > ^
1310-
9 > ^^^
1311-
10> ^
1312-
11> ^^^
1313-
12> ^
1314-
13> ^^^
1315-
14> ^
1316-
15> ^
1320+
7 > ^
1321+
8 > ^
1322+
9 > ^
1323+
10> ^
1324+
11> ^^^
1325+
12> ^
1326+
13> ^
1327+
14> ^
1328+
15> ^
1329+
16> ^
1330+
17> ^^^
1331+
18> ^
1332+
19> ^
13171333
1 >
13181334
>
13191335
2 > (
13201336
3 > x
13211337
4 > ==
13221338
5 > 1
13231339
6 > )
1324-
7 > ?
1325-
8 > x
1326-
9 > +
1327-
10> 1
1328-
11> :
1329-
12> x
1330-
13> -
1331-
14> 1
1332-
15> ;
1340+
7 >
1341+
8 > ?
1342+
9 >
1343+
10> x
1344+
11> +
1345+
12> 1
1346+
13>
1347+
14> :
1348+
15>
1349+
16> x
1350+
17> -
1351+
18> 1
1352+
19> ;
13331353
1 >Emitted(75, 5) Source(73, 5) + SourceIndex(0)
13341354
2 >Emitted(75, 6) Source(73, 6) + SourceIndex(0)
13351355
3 >Emitted(75, 7) Source(73, 7) + SourceIndex(0)
13361356
4 >Emitted(75, 11) Source(73, 11) + SourceIndex(0)
13371357
5 >Emitted(75, 12) Source(73, 12) + SourceIndex(0)
13381358
6 >Emitted(75, 13) Source(73, 13) + SourceIndex(0)
1339-
7 >Emitted(75, 16) Source(73, 16) + SourceIndex(0)
1340-
8 >Emitted(75, 17) Source(73, 17) + SourceIndex(0)
1341-
9 >Emitted(75, 20) Source(73, 20) + SourceIndex(0)
1342-
10>Emitted(75, 21) Source(73, 21) + SourceIndex(0)
1343-
11>Emitted(75, 24) Source(73, 24) + SourceIndex(0)
1344-
12>Emitted(75, 25) Source(73, 25) + SourceIndex(0)
1345-
13>Emitted(75, 28) Source(73, 28) + SourceIndex(0)
1346-
14>Emitted(75, 29) Source(73, 29) + SourceIndex(0)
1347-
15>Emitted(75, 30) Source(73, 30) + SourceIndex(0)
1359+
7 >Emitted(75, 14) Source(73, 14) + SourceIndex(0)
1360+
8 >Emitted(75, 15) Source(73, 15) + SourceIndex(0)
1361+
9 >Emitted(75, 16) Source(73, 16) + SourceIndex(0)
1362+
10>Emitted(75, 17) Source(73, 17) + SourceIndex(0)
1363+
11>Emitted(75, 20) Source(73, 20) + SourceIndex(0)
1364+
12>Emitted(75, 21) Source(73, 21) + SourceIndex(0)
1365+
13>Emitted(75, 22) Source(73, 22) + SourceIndex(0)
1366+
14>Emitted(75, 23) Source(73, 23) + SourceIndex(0)
1367+
15>Emitted(75, 24) Source(73, 24) + SourceIndex(0)
1368+
16>Emitted(75, 25) Source(73, 25) + SourceIndex(0)
1369+
17>Emitted(75, 28) Source(73, 28) + SourceIndex(0)
1370+
18>Emitted(75, 29) Source(73, 29) + SourceIndex(0)
1371+
19>Emitted(75, 30) Source(73, 30) + SourceIndex(0)
13481372
---
13491373
>>> x === 1;
13501374
1 >^^^^

tests/baselines/reference/ternaryExpressionSourceMap.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/ternaryExpressionSourceMap.sourcemap.txt

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,55 +35,67 @@ sourceFile:ternaryExpressionSourceMap.ts
3535
3 > ^^^
3636
4 > ^^^
3737
5 > ^
38-
6 > ^^^
39-
7 > ^^^^^^^^^^^^^^
40-
8 > ^^^^^^^
41-
9 > ^
42-
10> ^^
43-
11> ^
44-
12> ^^^
45-
13> ^^^^^^^^^^^^^^
46-
14> ^^^^^^^
47-
15> ^
48-
16> ^^
49-
17> ^
50-
18> ^
38+
6 > ^
39+
7 > ^
40+
8 > ^
41+
9 > ^^^^^^^^^^^^^^
42+
10> ^^^^^^^
43+
11> ^
44+
12> ^^
45+
13> ^
46+
14> ^
47+
15> ^
48+
16> ^
49+
17> ^^^^^^^^^^^^^^
50+
18> ^^^^^^^
51+
19> ^
52+
20> ^^
53+
21> ^
54+
22> ^
5155
1->
5256
>
5357
2 >var
5458
3 > foo
5559
4 > =
5660
5 > x
57-
6 > ?
58-
7 > () =>
59-
8 >
60-
9 > 0
61-
10>
62-
11> 0
63-
12> :
64-
13> () =>
65-
14>
66-
15> 0
67-
16>
68-
17> 0
69-
18> ;
61+
6 >
62+
7 > ?
63+
8 >
64+
9 > () =>
65+
10>
66+
11> 0
67+
12>
68+
13> 0
69+
14>
70+
15> :
71+
16>
72+
17> () =>
73+
18>
74+
19> 0
75+
20>
76+
21> 0
77+
22> ;
7078
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
7179
2 >Emitted(2, 5) Source(2, 5) + SourceIndex(0)
7280
3 >Emitted(2, 8) Source(2, 8) + SourceIndex(0)
7381
4 >Emitted(2, 11) Source(2, 11) + SourceIndex(0)
7482
5 >Emitted(2, 12) Source(2, 12) + SourceIndex(0)
75-
6 >Emitted(2, 15) Source(2, 15) + SourceIndex(0)
76-
7 >Emitted(2, 29) Source(2, 21) + SourceIndex(0)
77-
8 >Emitted(2, 36) Source(2, 21) + SourceIndex(0)
78-
9 >Emitted(2, 37) Source(2, 22) + SourceIndex(0)
79-
10>Emitted(2, 39) Source(2, 21) + SourceIndex(0)
80-
11>Emitted(2, 40) Source(2, 22) + SourceIndex(0)
81-
12>Emitted(2, 43) Source(2, 25) + SourceIndex(0)
82-
13>Emitted(2, 57) Source(2, 31) + SourceIndex(0)
83-
14>Emitted(2, 64) Source(2, 31) + SourceIndex(0)
84-
15>Emitted(2, 65) Source(2, 32) + SourceIndex(0)
85-
16>Emitted(2, 67) Source(2, 31) + SourceIndex(0)
86-
17>Emitted(2, 68) Source(2, 32) + SourceIndex(0)
87-
18>Emitted(2, 69) Source(2, 33) + SourceIndex(0)
83+
6 >Emitted(2, 13) Source(2, 13) + SourceIndex(0)
84+
7 >Emitted(2, 14) Source(2, 14) + SourceIndex(0)
85+
8 >Emitted(2, 15) Source(2, 15) + SourceIndex(0)
86+
9 >Emitted(2, 29) Source(2, 21) + SourceIndex(0)
87+
10>Emitted(2, 36) Source(2, 21) + SourceIndex(0)
88+
11>Emitted(2, 37) Source(2, 22) + SourceIndex(0)
89+
12>Emitted(2, 39) Source(2, 21) + SourceIndex(0)
90+
13>Emitted(2, 40) Source(2, 22) + SourceIndex(0)
91+
14>Emitted(2, 41) Source(2, 23) + SourceIndex(0)
92+
15>Emitted(2, 42) Source(2, 24) + SourceIndex(0)
93+
16>Emitted(2, 43) Source(2, 25) + SourceIndex(0)
94+
17>Emitted(2, 57) Source(2, 31) + SourceIndex(0)
95+
18>Emitted(2, 64) Source(2, 31) + SourceIndex(0)
96+
19>Emitted(2, 65) Source(2, 32) + SourceIndex(0)
97+
20>Emitted(2, 67) Source(2, 31) + SourceIndex(0)
98+
21>Emitted(2, 68) Source(2, 32) + SourceIndex(0)
99+
22>Emitted(2, 69) Source(2, 33) + SourceIndex(0)
88100
---
89101
>>>//# sourceMappingURL=ternaryExpressionSourceMap.js.map

tests/baselines/reference/typeGuardsInRightOperandOfAndAndOperator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function foo7(x) {
8585
return typeof x !== "string"
8686
&& ((z = x) // number | boolean
8787
&& (typeof x === "number"
88+
// change value of x
8889
? ((x = 10) && x.toString()) // x is number
90+
// do not change value
8991
: ((y = x) && x.toString()))); // x is boolean
9092
}

tests/baselines/reference/typeGuardsInRightOperandOfOrOrOperator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ function foo7(x) {
8787
return typeof x === "string"
8888
|| ((z = x) // number | boolean
8989
|| (typeof x === "number"
90+
// change value of x
9091
? ((x = 10) && x.toString()) // number | boolean | string
92+
// do not change value
9193
: ((y = x) && x.toString()))); // number | boolean | string
9294
}

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)