Skip to content

Commit ede6214

Browse files
committed
Add more check
1 parent 82fe658 commit ede6214

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
@@ -905,6 +905,9 @@ namespace ts {
905905
function isNarrowingBinaryExpression(expr: BinaryExpression) {
906906
switch (expr.operatorToken.kind) {
907907
case SyntaxKind.EqualsToken:
908+
case SyntaxKind.BarBarEqualsToken:
909+
case SyntaxKind.AmpersandAmpersandEqualsToken:
910+
case SyntaxKind.QuestionQuestionEqualsToken:
908911
return containsNarrowableReference(expr.left);
909912
case SyntaxKind.EqualsEqualsToken:
910913
case SyntaxKind.ExclamationEqualsToken:
@@ -1052,6 +1055,15 @@ namespace ts {
10521055
}
10531056
}
10541057

1058+
function isTopLevelLogicalAssignmentExpression(node: Node): boolean {
1059+
while (isParenthesizedExpression(node.parent)) {
1060+
node = node.parent;
1061+
}
1062+
return !isStatementCondition(node) &&
1063+
!isLogicalAssignmentExpressioin(node.parent) &&
1064+
!(isOptionalChain(node.parent) && node.parent.expression === node);
1065+
}
1066+
10551067
function isTopLevelLogicalExpression(node: Node): boolean {
10561068
while (isParenthesizedExpression(node.parent) ||
10571069
isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.ExclamationToken) {
@@ -1174,23 +1186,19 @@ namespace ts {
11741186
currentFlow = finishFlowLabel(postIfLabel);
11751187
}
11761188

1177-
function bindLogicalAssignmentExpression(node: BinaryExpression) {
1189+
function bindLogicalAssignmentExpression(node: BinaryExpression, trueTarget: FlowLabel, falseTarget: FlowLabel) {
11781190
const preRightLabel = createBranchLabel();
1179-
const postExpressionLabel = createBranchLabel();
1180-
11811191
if (node.operatorToken.kind === SyntaxKind.AmpersandAmpersandEqualsToken) {
1182-
bindCondition(node.left, preRightLabel, postExpressionLabel);
1192+
bindCondition(node.left, preRightLabel, falseTarget);
11831193
}
11841194
else {
1185-
bindCondition(node.left, postExpressionLabel, preRightLabel);
1195+
bindCondition(node.left, trueTarget, preRightLabel);
11861196
}
11871197

11881198
currentFlow = finishFlowLabel(preRightLabel);
11891199
bind(node.operatorToken);
1190-
bind(node.right);
1200+
doWithConditionalBranches(bind, node.right, trueTarget, falseTarget);
11911201
bindAssignmentTargetFlow(node.left);
1192-
1193-
currentFlow = finishFlowLabel(postExpressionLabel);
11941202
}
11951203

11961204
function bindReturnOrThrow(node: ReturnStatement | ThrowStatement): void {
@@ -1546,7 +1554,14 @@ namespace ts {
15461554
completeNode();
15471555
}
15481556
else if(isLogicalAssignmentOperator(operator)) {
1549-
bindLogicalAssignmentExpression(node);
1557+
if (isTopLevelLogicalAssignmentExpression(node)) {
1558+
const postExpressionLabel = createBranchLabel();
1559+
bindLogicalAssignmentExpression(node, postExpressionLabel, postExpressionLabel);
1560+
currentFlow = finishFlowLabel(postExpressionLabel);
1561+
}
1562+
else {
1563+
bindLogicalAssignmentExpression(node, currentTrueTarget!, currentFalseTarget!);
1564+
}
15501565
completeNode();
15511566
}
15521567
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)