Skip to content

Commit 9981db8

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 cc5cc95 commit 9981db8

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
@@ -1107,7 +1107,7 @@ namespace ts {
11071107
}
11081108

11091109
function resolveModuleNamesReusingOldState(moduleNames: string[], file: SourceFile): readonly ResolvedModuleFull[] {
1110-
if (structuralIsReused === StructureIsReused.Not && !file.ambientModuleNames.length) {
1110+
if (!doesStructuralPermitResolutionsReuse() && !file.ambientModuleNames.length) {
11111111
// If the old program state does not permit reusing resolutions and `file` does not contain locally defined ambient modules,
11121112
// the best we can do is fallback to the default logic.
11131113
return resolveModuleNamesWorker(moduleNames, file, /*reusedNames*/ undefined);
@@ -1218,6 +1218,28 @@ namespace ts {
12181218

12191219
return result;
12201220

1221+
function doesStructuralPermitResolutionsReuse(): boolean {
1222+
switch (structuralIsReused) {
1223+
case StructureIsReused.Not: return false;
1224+
case StructureIsReused.SafeModules: return true;
1225+
case StructureIsReused.Completely: return true;
1226+
1227+
// It's possible for resolveModuleNamesReusingOldState to be called by
1228+
// tryReuseStructureFromOldProgram before structuralIsReused has been defined.
1229+
// In this case, the caller (tryReuseStructureFromOldProgram) expects this
1230+
// function to reuse resolutions for its checks. Returning true to accommodate
1231+
// for that specific scenario.
1232+
//
1233+
// TODO: Clean this because the above scenario is a bit unexpected. This case
1234+
// should ideally return false or throw.
1235+
case undefined: return true;
1236+
1237+
// The above cases should exhaustively cover all branches. Defaulting to false
1238+
// since that's safer in unexpected situations.
1239+
default: return false;
1240+
}
1241+
}
1242+
12211243
// If we change our policy of rechecking failed lookups on each program create,
12221244
// we should adjust the value returned here.
12231245
function moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName: string): boolean {

0 commit comments

Comments
 (0)