Skip to content

Commit 823d385

Browse files
author
Andy Hanson
committed
Add Push interface
1 parent 8308ab3 commit 823d385

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

src/compiler/moduleNameResolver.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace ts {
1414
return compilerOptions.traceResolution && host.trace !== undefined;
1515
}
1616

17+
/** Array that is only intended to be pushed to, never read. */
18+
interface Push<T> {
19+
push(value: T): void;
20+
}
21+
1722
/**
1823
* Result of trying to resolve a module.
1924
* At least one of `ts` and `js` should be defined, or the whole thing should be `undefined`.
@@ -334,7 +339,7 @@ namespace ts {
334339
* 'typings' entry or file 'index' with some supported extension
335340
* - Classic loader will only try to interpret '/a/b/c' as file.
336341
*/
337-
type ResolutionKindSpecificLoader = (extensions: Extensions, candidate: string, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState) => Resolved | undefined;
342+
type ResolutionKindSpecificLoader = (extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState) => Resolved | undefined;
338343

339344
/**
340345
* Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to
@@ -397,7 +402,7 @@ namespace ts {
397402
* entries in 'rootDirs', use them to build absolute path out of (*) and try to resolve module from this location.
398403
*/
399404
function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
400-
failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
405+
failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
401406

402407
if (moduleHasNonRelativeName(moduleName)) {
403408
return tryLoadModuleUsingBaseUrl(extensions, moduleName, loader, failedLookupLocations, state);
@@ -408,7 +413,7 @@ namespace ts {
408413
}
409414

410415
function tryLoadModuleUsingRootDirs(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
411-
failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
416+
failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
412417

413418
if (!state.compilerOptions.rootDirs) {
414419
return undefined;
@@ -484,7 +489,7 @@ namespace ts {
484489
return undefined;
485490
}
486491

487-
function tryLoadModuleUsingBaseUrl(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
492+
function tryLoadModuleUsingBaseUrl(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
488493
if (!state.compilerOptions.baseUrl) {
489494
return undefined;
490495
}
@@ -578,7 +583,7 @@ namespace ts {
578583
return { path: real, extension: resolved.extension };
579584
}
580585

581-
function nodeLoadModuleByRelativeName(extensions: Extensions, candidate: string, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
586+
function nodeLoadModuleByRelativeName(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
582587
if (state.traceEnabled) {
583588
trace(state.host, Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate);
584589
}
@@ -597,7 +602,7 @@ namespace ts {
597602
* @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary
598603
* in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations.
599604
*/
600-
function loadModuleFromFile(extensions: Extensions, candidate: string, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
605+
function loadModuleFromFile(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
601606
// First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts"
602607
const resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocations, onlyRecordFailures, state);
603608
if (resolvedByAddingExtension) {
@@ -617,7 +622,7 @@ namespace ts {
617622
}
618623

619624
/** Try to return an existing file that adds one of the `extensions` to `candidate`. */
620-
function tryAddingExtensions(candidate: string, extensions: Extensions, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
625+
function tryAddingExtensions(candidate: string, extensions: Extensions, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
621626
if (!onlyRecordFailures) {
622627
// check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing
623628
const directory = getDirectoryPath(candidate);
@@ -642,7 +647,7 @@ namespace ts {
642647
}
643648

644649
/** Return the file if it exists. */
645-
function tryFile(fileName: string, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
650+
function tryFile(fileName: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
646651
if (!onlyRecordFailures && state.host.fileExists(fileName)) {
647652
if (state.traceEnabled) {
648653
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
@@ -658,7 +663,7 @@ namespace ts {
658663
}
659664
}
660665

661-
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, failedLookupLocations: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
666+
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
662667
const packageJsonPath = pathToPackageJson(candidate);
663668
const directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host);
664669

@@ -701,7 +706,7 @@ namespace ts {
701706
return combinePaths(directory, "package.json");
702707
}
703708

704-
function loadModuleFromNodeModulesFolder(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
709+
function loadModuleFromNodeModulesFolder(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
705710
const nodeModulesFolder = combinePaths(directory, "node_modules");
706711
const nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host);
707712
const candidate = normalizePath(combinePaths(nodeModulesFolder, moduleName));
@@ -710,15 +715,15 @@ namespace ts {
710715
loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state);
711716
}
712717

713-
function loadModuleFromNodeModules(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
718+
function loadModuleFromNodeModules(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
714719
return loadModuleFromNodeModulesWorker(extensions, moduleName, directory, failedLookupLocations, state, /*typesOnly*/ false);
715720
}
716-
function loadModuleFromNodeModulesAtTypes(moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState): Resolved | undefined {
721+
function loadModuleFromNodeModulesAtTypes(moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
717722
// Extensions parameter here doesn't actually matter, because typesOnly ensures we're just doing @types lookup, which is always DtsOnly.
718723
return loadModuleFromNodeModulesWorker(Extensions.DtsOnly, moduleName, directory, failedLookupLocations, state, /*typesOnly*/ true);
719724
}
720725

721-
function loadModuleFromNodeModulesWorker(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState, typesOnly: boolean): Resolved | undefined {
726+
function loadModuleFromNodeModulesWorker(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState, typesOnly: boolean): Resolved | undefined {
722727
return forEachAncestorDirectory(normalizeSlashes(directory), ancestorDirectory => {
723728
if (getBaseFileName(ancestorDirectory) !== "node_modules") {
724729
return loadModuleFromNodeModulesOneLevel(extensions, moduleName, ancestorDirectory, failedLookupLocations, state, typesOnly);
@@ -727,7 +732,7 @@ namespace ts {
727732
}
728733

729734
/** Load a module from a single node_modules directory, but not from any ancestors' node_modules directories. */
730-
function loadModuleFromNodeModulesOneLevel(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState, typesOnly = false): Resolved | undefined {
735+
function loadModuleFromNodeModulesOneLevel(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState, typesOnly = false): Resolved | undefined {
731736
const packageResult = typesOnly ? undefined : loadModuleFromNodeModulesFolder(extensions, moduleName, directory, failedLookupLocations, state);
732737
if (packageResult) {
733738
return packageResult;

0 commit comments

Comments
 (0)