From ede6214157990584c678bbdb8c55f1b397a8f627 Mon Sep 17 00:00:00 2001 From: kingwl Date: Thu, 2 Apr 2020 03:01:19 +0800 Subject: [PATCH] Add more check --- src/compiler/binder.ts | 33 ++++++++++++++----- .../logicalAssignment/logicalAssignment4.ts | 1 + 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 5e570020941..a7272d8a7e0 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -905,6 +905,9 @@ namespace ts { function isNarrowingBinaryExpression(expr: BinaryExpression) { switch (expr.operatorToken.kind) { case SyntaxKind.EqualsToken: + case SyntaxKind.BarBarEqualsToken: + case SyntaxKind.AmpersandAmpersandEqualsToken: + case SyntaxKind.QuestionQuestionEqualsToken: return containsNarrowableReference(expr.left); case SyntaxKind.EqualsEqualsToken: case SyntaxKind.ExclamationEqualsToken: @@ -1052,6 +1055,15 @@ namespace ts { } } + function isTopLevelLogicalAssignmentExpression(node: Node): boolean { + while (isParenthesizedExpression(node.parent)) { + node = node.parent; + } + return !isStatementCondition(node) && + !isLogicalAssignmentExpressioin(node.parent) && + !(isOptionalChain(node.parent) && node.parent.expression === node); + } + function isTopLevelLogicalExpression(node: Node): boolean { while (isParenthesizedExpression(node.parent) || isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.ExclamationToken) { @@ -1174,23 +1186,19 @@ namespace ts { currentFlow = finishFlowLabel(postIfLabel); } - function bindLogicalAssignmentExpression(node: BinaryExpression) { + function bindLogicalAssignmentExpression(node: BinaryExpression, trueTarget: FlowLabel, falseTarget: FlowLabel) { const preRightLabel = createBranchLabel(); - const postExpressionLabel = createBranchLabel(); - if (node.operatorToken.kind === SyntaxKind.AmpersandAmpersandEqualsToken) { - bindCondition(node.left, preRightLabel, postExpressionLabel); + bindCondition(node.left, preRightLabel, falseTarget); } else { - bindCondition(node.left, postExpressionLabel, preRightLabel); + bindCondition(node.left, trueTarget, preRightLabel); } currentFlow = finishFlowLabel(preRightLabel); bind(node.operatorToken); - bind(node.right); + doWithConditionalBranches(bind, node.right, trueTarget, falseTarget); bindAssignmentTargetFlow(node.left); - - currentFlow = finishFlowLabel(postExpressionLabel); } function bindReturnOrThrow(node: ReturnStatement | ThrowStatement): void { @@ -1546,7 +1554,14 @@ namespace ts { completeNode(); } else if(isLogicalAssignmentOperator(operator)) { - bindLogicalAssignmentExpression(node); + if (isTopLevelLogicalAssignmentExpression(node)) { + const postExpressionLabel = createBranchLabel(); + bindLogicalAssignmentExpression(node, postExpressionLabel, postExpressionLabel); + currentFlow = finishFlowLabel(postExpressionLabel); + } + else { + bindLogicalAssignmentExpression(node, currentTrueTarget!, currentFalseTarget!); + } completeNode(); } else { diff --git a/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment4.ts b/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment4.ts index b142f0ece1d..3c35d94b336 100644 --- a/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment4.ts +++ b/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment4.ts @@ -1,5 +1,6 @@ // @strict: true // @target: esnext, es2020, es2015 +// @allowUnreachableCode: false function foo1(results: number[] | undefined) { (results ||= []).push(100);