@@ -550,21 +550,24 @@ func setFlowNodeReferenced(flow *ast.FlowNode) {
550
550
}
551
551
}
552
552
553
- func hasAntecedent (list * ast.FlowList , antecedent * ast.FlowNode ) bool {
554
- for list != nil {
553
+ func (b * Binder ) addAntecedent (label * ast.FlowLabel , antecedent * ast.FlowNode ) {
554
+ if antecedent .Flags & ast .FlowFlagsUnreachable != 0 {
555
+ return
556
+ }
557
+ // If antecedent isn't already on the Antecedents list, add it to the end of the list
558
+ var last * ast.FlowList
559
+ for list := label .Antecedents ; list != nil ; list = list .Next {
555
560
if list .Flow == antecedent {
556
- return true
561
+ return
557
562
}
558
- list = list . Next
563
+ last = list
559
564
}
560
- return false
561
- }
562
-
563
- func (b * Binder ) addAntecedent (label * ast.FlowLabel , antecedent * ast.FlowNode ) {
564
- if antecedent .Flags & ast .FlowFlagsUnreachable == 0 && ! hasAntecedent (label .Antecedents , antecedent ) {
565
- label .Antecedents = b .newFlowList (antecedent , label .Antecedents )
566
- setFlowNodeReferenced (antecedent )
565
+ if last == nil {
566
+ label .Antecedents = b .newFlowList (antecedent , nil )
567
+ } else {
568
+ last .Next = b .newFlowList (antecedent , nil )
567
569
}
570
+ setFlowNodeReferenced (antecedent )
568
571
}
569
572
570
573
func (b * Binder ) finishFlowLabel (label * ast.FlowLabel ) * ast.FlowNode {
@@ -1897,13 +1900,12 @@ func (b *Binder) bindWhileStatement(node *ast.Node) {
1897
1900
preWhileLabel := b .setContinueTarget (node , b .createLoopLabel ())
1898
1901
preBodyLabel := b .createBranchLabel ()
1899
1902
postWhileLabel := b .createBranchLabel ()
1900
- topFlow := b .currentFlow
1903
+ b . addAntecedent ( preWhileLabel , b .currentFlow )
1901
1904
b .currentFlow = preWhileLabel
1902
1905
b .bindCondition (stmt .Expression , preBodyLabel , postWhileLabel )
1903
1906
b .currentFlow = b .finishFlowLabel (preBodyLabel )
1904
1907
b .bindIterativeStatement (stmt .Statement , postWhileLabel , preWhileLabel )
1905
1908
b .addAntecedent (preWhileLabel , b .currentFlow )
1906
- b .addAntecedent (preWhileLabel , topFlow )
1907
1909
b .currentFlow = b .finishFlowLabel (postWhileLabel )
1908
1910
}
1909
1911
@@ -1912,13 +1914,12 @@ func (b *Binder) bindDoStatement(node *ast.Node) {
1912
1914
preDoLabel := b .createLoopLabel ()
1913
1915
preConditionLabel := b .setContinueTarget (node , b .createBranchLabel ())
1914
1916
postDoLabel := b .createBranchLabel ()
1915
- topFlow := b .currentFlow
1917
+ b . addAntecedent ( preDoLabel , b .currentFlow )
1916
1918
b .currentFlow = preDoLabel
1917
1919
b .bindIterativeStatement (stmt .Statement , postDoLabel , preConditionLabel )
1918
1920
b .addAntecedent (preConditionLabel , b .currentFlow )
1919
1921
b .currentFlow = b .finishFlowLabel (preConditionLabel )
1920
1922
b .bindCondition (stmt .Expression , preDoLabel , postDoLabel )
1921
- b .addAntecedent (preDoLabel , topFlow )
1922
1923
b .currentFlow = b .finishFlowLabel (postDoLabel )
1923
1924
}
1924
1925
@@ -1929,7 +1930,7 @@ func (b *Binder) bindForStatement(node *ast.Node) {
1929
1930
preIncrementorLabel := b .createBranchLabel ()
1930
1931
postLoopLabel := b .createBranchLabel ()
1931
1932
b .bind (stmt .Initializer )
1932
- topFlow := b .currentFlow
1933
+ b . addAntecedent ( preLoopLabel , b .currentFlow )
1933
1934
b .currentFlow = preLoopLabel
1934
1935
b .bindCondition (stmt .Condition , preBodyLabel , postLoopLabel )
1935
1936
b .currentFlow = b .finishFlowLabel (preBodyLabel )
@@ -1938,7 +1939,6 @@ func (b *Binder) bindForStatement(node *ast.Node) {
1938
1939
b .currentFlow = b .finishFlowLabel (preIncrementorLabel )
1939
1940
b .bind (stmt .Incrementor )
1940
1941
b .addAntecedent (preLoopLabel , b .currentFlow )
1941
- b .addAntecedent (preLoopLabel , topFlow )
1942
1942
b .currentFlow = b .finishFlowLabel (postLoopLabel )
1943
1943
}
1944
1944
@@ -1947,7 +1947,7 @@ func (b *Binder) bindForInOrForOfStatement(node *ast.Node) {
1947
1947
preLoopLabel := b .setContinueTarget (node , b .createLoopLabel ())
1948
1948
postLoopLabel := b .createBranchLabel ()
1949
1949
b .bind (stmt .Expression )
1950
- topFlow := b .currentFlow
1950
+ b . addAntecedent ( preLoopLabel , b .currentFlow )
1951
1951
b .currentFlow = preLoopLabel
1952
1952
if node .Kind == ast .KindForOfStatement {
1953
1953
b .bind (stmt .AwaitModifier )
@@ -1959,7 +1959,6 @@ func (b *Binder) bindForInOrForOfStatement(node *ast.Node) {
1959
1959
}
1960
1960
b .bindIterativeStatement (stmt .Statement , postLoopLabel , preLoopLabel )
1961
1961
b .addAntecedent (preLoopLabel , b .currentFlow )
1962
- b .addAntecedent (preLoopLabel , topFlow )
1963
1962
b .currentFlow = b .finishFlowLabel (postLoopLabel )
1964
1963
}
1965
1964
0 commit comments