Skip to content

Commit f8bf4e1

Browse files
committed
Add more check
1 parent f8812c9 commit f8bf4e1

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/compiler/binder.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,9 @@ namespace ts {
915915
function isNarrowingBinaryExpression(expr: BinaryExpression) {
916916
switch (expr.operatorToken.kind) {
917917
case SyntaxKind.EqualsToken:
918+
case SyntaxKind.BarBarEqualsToken:
919+
case SyntaxKind.AmpersandAmpersandEqualsToken:
920+
case SyntaxKind.QuestionQuestionEqualsToken:
918921
return containsNarrowableReference(expr.left);
919922
case SyntaxKind.EqualsEqualsToken:
920923
case SyntaxKind.ExclamationEqualsToken:
@@ -1062,6 +1065,15 @@ namespace ts {
10621065
}
10631066
}
10641067

1068+
function isTopLevelLogicalAssignmentExpression(node: Node): boolean {
1069+
while (isParenthesizedExpression(node.parent)) {
1070+
node = node.parent;
1071+
}
1072+
return !isStatementCondition(node) &&
1073+
!isLogicalAssignmentExpressioin(node.parent) &&
1074+
!(isOptionalChain(node.parent) && node.parent.expression === node);
1075+
}
1076+
10651077
function isTopLevelLogicalExpression(node: Node): boolean {
10661078
while (isParenthesizedExpression(node.parent) ||
10671079
isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.ExclamationToken) {
@@ -1184,23 +1196,19 @@ namespace ts {
11841196
currentFlow = finishFlowLabel(postIfLabel);
11851197
}
11861198

1187-
function bindLogicalAssignmentExpression(node: BinaryExpression) {
1199+
function bindLogicalAssignmentExpression(node: BinaryExpression, trueTarget: FlowLabel, falseTarget: FlowLabel) {
11881200
const preRightLabel = createBranchLabel();
1189-
const postExpressionLabel = createBranchLabel();
1190-
11911201
if (node.operatorToken.kind === SyntaxKind.AmpersandAmpersandEqualsToken) {
1192-
bindCondition(node.left, preRightLabel, postExpressionLabel);
1202+
bindCondition(node.left, preRightLabel, falseTarget);
11931203
}
11941204
else {
1195-
bindCondition(node.left, postExpressionLabel, preRightLabel);
1205+
bindCondition(node.left, trueTarget, preRightLabel);
11961206
}
11971207

11981208
currentFlow = finishFlowLabel(preRightLabel);
11991209
bind(node.operatorToken);
1200-
bind(node.right);
1210+
doWithConditionalBranches(bind, node.right, trueTarget, falseTarget);
12011211
bindAssignmentTargetFlow(node.left);
1202-
1203-
currentFlow = finishFlowLabel(postExpressionLabel);
12041212
}
12051213

12061214
function bindReturnOrThrow(node: ReturnStatement | ThrowStatement): void {
@@ -1556,7 +1564,14 @@ namespace ts {
15561564
completeNode();
15571565
}
15581566
else if(isLogicalAssignmentOperator(operator)) {
1559-
bindLogicalAssignmentExpression(node);
1567+
if (isTopLevelLogicalAssignmentExpression(node)) {
1568+
const postExpressionLabel = createBranchLabel();
1569+
bindLogicalAssignmentExpression(node, postExpressionLabel, postExpressionLabel);
1570+
currentFlow = finishFlowLabel(postExpressionLabel);
1571+
}
1572+
else {
1573+
bindLogicalAssignmentExpression(node, currentTrueTarget!, currentFalseTarget!);
1574+
}
15601575
completeNode();
15611576
}
15621577
else {

tests/cases/conformance/esnext/logicalAssignment/logicalAssignment4.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @strict: true
22
// @target: esnext, es2020, es2015
3+
// @allowUnreachableCode: false
34

45
function foo1(results: number[] | undefined) {
56
(results ||= []).push(100);

0 commit comments

Comments
 (0)