Fix reachability analysis for ?? operator (#34702)

* Exclude ?? operator from true/false literal check in createFlowCondition

* Accept new API baselines

* Add tests

* Accept new baselines

* Address CR feedback

* Accept new API baselines
This commit is contained in:
Anders Hejlsberg
2019-10-24 15:57:14 -07:00
committed by GitHub
parent a03227d60e
commit c404c08fde
8 changed files with 166 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
/* @internal */
namespace ts {
export const enum ModuleInstanceState {
@@ -951,11 +952,10 @@ namespace ts {
if (!expression) {
return flags & FlowFlags.TrueCondition ? antecedent : unreachableFlow;
}
if (expression.kind === SyntaxKind.TrueKeyword && flags & FlowFlags.FalseCondition ||
expression.kind === SyntaxKind.FalseKeyword && flags & FlowFlags.TrueCondition) {
if (!isExpressionOfOptionalChainRoot(expression)) {
return unreachableFlow;
}
if ((expression.kind === SyntaxKind.TrueKeyword && flags & FlowFlags.FalseCondition ||
expression.kind === SyntaxKind.FalseKeyword && flags & FlowFlags.TrueCondition) &&
!isExpressionOfOptionalChainRoot(expression) && !isNullishCoalesce(expression.parent)) {
return unreachableFlow;
}
if (!isNarrowingExpression(expression)) {
return antecedent;

View File

@@ -5955,6 +5955,10 @@ namespace ts {
return isOptionalChainRoot(node.parent) && node.parent.expression === node;
}
export function isNullishCoalesce(node: Node) {
return node.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>node).operatorToken.kind === SyntaxKind.QuestionQuestionToken;
}
export function isNewExpression(node: Node): node is NewExpression {
return node.kind === SyntaxKind.NewExpression;
}