Skip to content

Commit 6418943

Browse files
committed
update control flow
1 parent de81520 commit 6418943

File tree

10 files changed

+79
-8
lines changed

10 files changed

+79
-8
lines changed

src/compiler/binder.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,7 @@ namespace ts {
10091009
else {
10101010
return node.kind === SyntaxKind.BinaryExpression && (
10111011
(<BinaryExpression>node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken ||
1012-
(<BinaryExpression>node).operatorToken.kind === SyntaxKind.BarBarToken ||
1013-
(<BinaryExpression>node).operatorToken.kind === SyntaxKind.QuestionQuestionToken);
1012+
(<BinaryExpression>node).operatorToken.kind === SyntaxKind.BarBarToken);
10141013
}
10151014
}
10161015
}
@@ -1436,6 +1435,14 @@ namespace ts {
14361435
bindCondition(node.right, trueTarget, falseTarget);
14371436
}
14381437

1438+
function bindNullishCollasingExpression(node: BinaryExpression, trueTarget: FlowLabel, falseTarget: FlowLabel) {
1439+
const notNullLabel = createBranchLabel();
1440+
bindCondition(node.left, trueTarget, notNullLabel);
1441+
currentFlow = finishFlowLabel(notNullLabel);
1442+
bind(node.operatorToken);
1443+
bindCondition(node.right, trueTarget, falseTarget!);
1444+
}
1445+
14391446
function bindPrefixUnaryExpressionFlow(node: PrefixUnaryExpression) {
14401447
if (node.operator === SyntaxKind.ExclamationToken) {
14411448
const saveTrueTarget = currentTrueTarget;
@@ -1468,6 +1475,9 @@ namespace ts {
14681475
bindLogicalExpression(node, postExpressionLabel, postExpressionLabel);
14691476
currentFlow = finishFlowLabel(postExpressionLabel);
14701477
}
1478+
else if (operator === SyntaxKind.QuestionQuestionToken) {
1479+
bindNullishCollasingExpression(node, currentTrueTarget!, currentFalseTarget!);
1480+
}
14711481
else {
14721482
bindLogicalExpression(node, currentTrueTarget!, currentFalseTarget!);
14731483
}

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,6 @@ namespace ts {
15141514
export type LogicalOperator
15151515
= SyntaxKind.AmpersandAmpersandToken
15161516
| SyntaxKind.BarBarToken
1517-
| SyntaxKind.QuestionQuestionToken
15181517
;
15191518

15201519
// see: https://tc39.github.io/ecma262/#prod-LogicalANDExpression
@@ -1548,7 +1547,8 @@ namespace ts {
15481547

15491548
// see: https://tc39.github.io/ecma262/#prod-AssignmentExpression
15501549
export type AssignmentOperatorOrHigher
1551-
= LogicalOperatorOrHigher
1550+
= SyntaxKind.QuestionQuestionToken
1551+
| LogicalOperatorOrHigher
15521552
| AssignmentOperator
15531553
;
15541554

src/compiler/utilities.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,8 +4028,7 @@ namespace ts {
40284028
export function isLogicalOperator(token: SyntaxKind): boolean {
40294029
return token === SyntaxKind.BarBarToken
40304030
|| token === SyntaxKind.AmpersandAmpersandToken
4031-
|| token === SyntaxKind.ExclamationToken
4032-
|| token === SyntaxKind.QuestionQuestionToken;
4031+
|| token === SyntaxKind.ExclamationToken;
40334032
}
40344033

40354034
export function isAssignmentOperator(token: SyntaxKind): boolean {

src/services/codefixes/inferFromUsage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ namespace ts.codefix {
719719
}
720720
break;
721721

722-
// LogicalOperator
722+
// LogicalOperator Or NullishCoalescing
723723
case SyntaxKind.BarBarToken:
724724
case SyntaxKind.QuestionQuestionToken:
725725
if (node === parent.left &&
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator11.ts(3,18): error TS2533: Object is possibly 'null' or 'undefined'.
2+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator11.ts(3,22): error TS2339: Property 'toFixed' does not exist on type '"" | 0'.
3+
Property 'toFixed' does not exist on type '""'.
4+
5+
6+
==== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator11.ts (2 errors) ====
7+
declare const f11: 1 | 0 | '' | null | undefined;
8+
9+
let g11 = f11 ?? f11.toFixed()
10+
~~~
11+
!!! error TS2533: Object is possibly 'null' or 'undefined'.
12+
~~~~~~~
13+
!!! error TS2339: Property 'toFixed' does not exist on type '"" | 0'.
14+
!!! error TS2339: Property 'toFixed' does not exist on type '""'.
15+
16+
17+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [nullishCoalescingOperator11.ts]
2+
declare const f11: 1 | 0 | '' | null | undefined;
3+
4+
let g11 = f11 ?? f11.toFixed()
5+
6+
7+
8+
9+
//// [nullishCoalescingOperator11.js]
10+
"use strict";
11+
var g11 = (f11 !== null && f11 !== void 0 ? f11 : f11.toFixed());
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator11.ts ===
2+
declare const f11: 1 | 0 | '' | null | undefined;
3+
>f11 : Symbol(f11, Decl(nullishCoalescingOperator11.ts, 0, 13))
4+
5+
let g11 = f11 ?? f11.toFixed()
6+
>g11 : Symbol(g11, Decl(nullishCoalescingOperator11.ts, 2, 3))
7+
>f11 : Symbol(f11, Decl(nullishCoalescingOperator11.ts, 0, 13))
8+
>f11 : Symbol(f11, Decl(nullishCoalescingOperator11.ts, 0, 13))
9+
10+
11+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator11.ts ===
2+
declare const f11: 1 | 0 | '' | null | undefined;
3+
>f11 : "" | 0 | 1 | null | undefined
4+
>null : null
5+
6+
let g11 = f11 ?? f11.toFixed()
7+
>g11 : any
8+
>f11 ?? f11.toFixed() : any
9+
>f11 : "" | 0 | 1 | null | undefined
10+
>f11.toFixed() : any
11+
>f11.toFixed : any
12+
>f11 : "" | 0 | null | undefined
13+
>toFixed : any
14+
15+
16+

tests/baselines/reference/nullishCoalescingOperator5.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ a ?? b || c;
2020
a || b ?? c;
2121
>a || b ?? c : string | undefined
2222
>a || b : string | undefined
23-
>a : string | undefined
23+
>a : string
2424
>b : string | undefined
2525
>c : string | undefined
2626

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @strict: true
2+
3+
declare const f11: 1 | 0 | '' | null | undefined;
4+
5+
let g11 = f11 ?? f11.toFixed()
6+
7+

0 commit comments

Comments
 (0)