From 9fa5971af08c0075648556cb65ed0620c277fe5d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 4 May 2016 15:17:39 -0700 Subject: [PATCH] Skip simple conditional flows in control flow graph --- src/compiler/binder.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 45a3bfe3e5a..8234075612e 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -695,6 +695,23 @@ namespace ts { }; } + function skipSimpleConditionalFlow(flow: FlowNode) { + // We skip over simple conditional flows of the form 'x ? aaa : bbb', where 'aaa' and 'bbb' contain + // no constructs that affect control flow type analysis. Such simple flows have no effect on the + // code paths that follow and ignoring them means we'll do less work. + if (flow.flags & FlowFlags.BranchLabel && (flow).antecedents.length === 2) { + const a = (flow).antecedents[0]; + const b = (flow).antecedents[1]; + if ((a.flags & FlowFlags.TrueCondition && b.flags & FlowFlags.FalseCondition || + a.flags & FlowFlags.FalseCondition && b.flags & FlowFlags.TrueCondition) && + (a).antecedent === (b).antecedent && + (a).expression === (b).expression) { + return (a).antecedent; + } + } + return flow; + } + function finishFlowLabel(flow: FlowLabel): FlowNode { const antecedents = flow.antecedents; if (!antecedents) { @@ -703,7 +720,7 @@ namespace ts { if (antecedents.length === 1) { return antecedents[0]; } - return flow; + return skipSimpleConditionalFlow(flow); } function isStatementCondition(node: Node) {