Skip to content

Commit c01064e

Browse files
committed
Record flow analysis errors in NodeLinks
1 parent 93b1c53 commit c01064e

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ namespace ts {
541541
let flowLoopCount = 0;
542542
let sharedFlowCount = 0;
543543
let flowNodeCount = -1;
544-
let flowAnalysisDisabled = false;
544+
let flowAnalysisErrors = false;
545545

546546
const emptyStringType = getLiteralType("");
547547
const zeroType = getLiteralType(0);
@@ -15950,17 +15950,14 @@ namespace ts {
1595015950
return false;
1595115951
}
1595215952

15953-
function reportFlowControlError(node: Node) {
15954-
const block = <Block | ModuleBlock | SourceFile>findAncestor(node, isFunctionOrModuleBlock);
15955-
const sourceFile = getSourceFileOfNode(node);
15956-
const span = getSpanOfTokenAtPosition(sourceFile, block.statements.pos);
15957-
diagnostics.add(createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.The_containing_function_or_module_body_is_too_complex_for_control_flow_analysis));
15953+
function getContainingBlock(node: Node) {
15954+
return <Block | ModuleBlock | SourceFile>findAncestor(node, isFunctionOrModuleBlock);
1595815955
}
1595915956

1596015957
function getFlowTypeOfReference(reference: Node, declaredType: Type, initialType = declaredType, flowContainer?: Node, couldBeUninitialized?: boolean) {
1596115958
let key: string | undefined;
1596215959
let flowDepth = 0;
15963-
if (flowAnalysisDisabled) {
15960+
if (flowAnalysisErrors && getNodeLinks(getContainingBlock(reference)).flags & NodeCheckFlags.FlowAnalysisError) {
1596415961
return errorType;
1596515962
}
1596615963
if (!reference.flowNode || !couldBeUninitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
@@ -15984,9 +15981,14 @@ namespace ts {
1598415981
// We have made 2000 recursive invocations or visited 500000 control flow nodes while analyzing
1598515982
// the containing function or module body, the limit at which we consider the function or module
1598615983
// body is too complex and disable further control flow analysis.
15987-
if (!flowAnalysisDisabled) {
15988-
flowAnalysisDisabled = true;
15989-
reportFlowControlError(reference);
15984+
const block = getContainingBlock(reference);
15985+
const nodeLinks = getNodeLinks(block);
15986+
if (!(nodeLinks.flags & NodeCheckFlags.FlowAnalysisError)) {
15987+
nodeLinks.flags |= NodeCheckFlags.FlowAnalysisError;
15988+
const sourceFile = getSourceFileOfNode(block);
15989+
const span = getSpanOfTokenAtPosition(sourceFile, block.statements.pos);
15990+
diagnostics.add(createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.The_containing_function_or_module_body_is_too_complex_for_control_flow_analysis));
15991+
flowAnalysisErrors = true;
1599015992
}
1599115993
return errorType;
1599215994
}
@@ -26032,11 +26034,9 @@ namespace ts {
2603226034
}
2603326035
if (isFunctionOrModuleBlock(node)) {
2603426036
const saveFlowNodeCount = flowNodeCount;
26035-
const saveFlowAnalysisDisabled = flowAnalysisDisabled;
2603626037
flowNodeCount = 0;
2603726038
forEach(node.statements, checkSourceElement);
2603826039
flowNodeCount = saveFlowNodeCount;
26039-
flowAnalysisDisabled = saveFlowAnalysisDisabled;
2604026040
}
2604126041
else {
2604226042
forEach(node.statements, checkSourceElement);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,6 +3855,7 @@ namespace ts {
38553855
AssignmentsMarked = 0x00800000, // Parameter assignments have been marked
38563856
ClassWithConstructorReference = 0x01000000, // Class that contains a binding to its constructor inside of the class body.
38573857
ConstructorReferenceInClass = 0x02000000, // Binding to a class constructor inside of the class's body.
3858+
FlowAnalysisError = 0x04000000, // Control flow analysis error in this block
38583859
}
38593860

38603861
/* @internal */

0 commit comments

Comments
 (0)