Skip to content

Commit ecba000

Browse files
committed
Check structuralIsReused with switch statement
Returning `true` when `structuralIsReused === undefined` is a bit unexpected, but matches the existing behavior of the program. Changing it to `false` breaks the following test: 1) unittests:: Reuse program structure:: General can reuse ambient module declarations from non-modified files: should reuse 'fs' since node.d.ts was not changed + expected - actual
1 parent 2dfa9cf commit ecba000

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/compiler/program.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ namespace ts {
10691069
}
10701070

10711071
function resolveModuleNamesReusingOldState(moduleNames: string[], containingFile: string, file: SourceFile) {
1072-
if (structuralIsReused === StructureIsReused.Not && !file.ambientModuleNames.length) {
1072+
if (!doesStructuralPermitResolutionsReuse() && !file.ambientModuleNames.length) {
10731073
// If the old program state does not permit reusing resolutions and `file` does not contain locally defined ambient modules,
10741074
// the best we can do is fallback to the default logic.
10751075
return resolveModuleNamesWorker(moduleNames, containingFile, /*reusedNames*/ undefined, getResolvedProjectReferenceToRedirect(file.originalFileName));
@@ -1180,6 +1180,28 @@ namespace ts {
11801180

11811181
return result;
11821182

1183+
function doesStructuralPermitResolutionsReuse(): boolean {
1184+
switch (structuralIsReused) {
1185+
case StructureIsReused.Not: return false;
1186+
case StructureIsReused.SafeModules: return true;
1187+
case StructureIsReused.Completely: return true;
1188+
1189+
// It's possible for resolveModuleNamesReusingOldState to be called by
1190+
// tryReuseStructureFromOldProgram before structuralIsReused has been defined.
1191+
// In this case, the caller (tryReuseStructureFromOldProgram) expects this
1192+
// function to reuse resolutions for its checks. Returning true to accommodate
1193+
// for that specific scenario.
1194+
//
1195+
// TODO: Clean this because the above scenario is a bit unexpected. This case
1196+
// should ideally return false or throw.
1197+
case undefined: return true;
1198+
1199+
// The above cases should exhaustively cover all branches. Defaulting to false
1200+
// since that's safer in unexpected situations.
1201+
default: return false;
1202+
}
1203+
}
1204+
11831205
// If we change our policy of rechecking failed lookups on each program create,
11841206
// we should adjust the value returned here.
11851207
function moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName: string): boolean {

0 commit comments

Comments
 (0)