Use undefined instead of empty array, and check for existence of "node_modules/@types" instead of just for "node_modules".

This commit is contained in:
Andy Hanson 2016-09-02 07:30:14 -07:00
parent 0e8e5ec3e5
commit cbd00b9a92
2 changed files with 13 additions and 14 deletions

View file

@ -182,23 +182,22 @@ namespace ts {
return currentDirectory && getDefaultTypeRoots(currentDirectory, host);
}
/**
* Returns the path to every node_modules/@types directory from some ancestor directory.
* Returns undefined if there are none.
*/
function getDefaultTypeRoots(currentDirectory: string, host: ModuleResolutionHost): string[] | undefined {
return map(getAllNodeModulesDirectories(currentDirectory, host), nodeModules => combinePaths(nodeModules, "@types"));
}
/** Returns the path to every node_modules directory from some ancestor directory. */
function getAllNodeModulesDirectories(currentDirectory: string, host: ModuleResolutionHost): string[] | undefined {
if (!host.directoryExists) {
return [combinePaths(currentDirectory, "node_modules")];
// And if it doesn't exist, tough.
}
const all: string[] = [];
let typeRoots: string[];
while (true) {
const nodeModules = combinePaths(currentDirectory, "node_modules");
if (host.directoryExists(nodeModules)) {
all.push(nodeModules);
const atTypes = combinePaths(currentDirectory, nodeModulesAtTypes);
if (host.directoryExists(atTypes)) {
(typeRoots || (typeRoots = [])).push(atTypes);
}
const parent = getDirectoryPath(currentDirectory);
@ -208,8 +207,10 @@ namespace ts {
currentDirectory = parent;
}
return all;
return typeRoots;
}
const nodeModulesAtTypes = combinePaths("node_modules", "@types");
/**
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups

View file

@ -1,8 +1,6 @@
[
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory '/src/node_modules/@types'. ========",
"Resolving with primary search path '/src/node_modules/@types'",
"File '/src/node_modules/@types/jquery/package.json' does not exist.",
"File '/src/node_modules/@types/jquery/index.d.ts' does not exist.",
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory not set. ========",
"Root directory cannot be determined, skipping primary search paths.",
"Looking up in 'node_modules' folder, initial location '/src'",
"File '/src/node_modules/jquery.ts' does not exist.",
"File '/src/node_modules/jquery.d.ts' does not exist.",