Skip to content

Commit cbd00b9

Browse files
author
Andy Hanson
committed
Use undefined instead of empty array, and check for existence of "node_modules/@types" instead of just for "node_modules".
1 parent 0e8e5ec commit cbd00b9

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/compiler/program.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,22 @@ namespace ts {
182182
return currentDirectory && getDefaultTypeRoots(currentDirectory, host);
183183
}
184184

185+
/**
186+
* Returns the path to every node_modules/@types directory from some ancestor directory.
187+
* Returns undefined if there are none.
188+
*/
185189
function getDefaultTypeRoots(currentDirectory: string, host: ModuleResolutionHost): string[] | undefined {
186-
return map(getAllNodeModulesDirectories(currentDirectory, host), nodeModules => combinePaths(nodeModules, "@types"));
187-
}
188-
189-
/** Returns the path to every node_modules directory from some ancestor directory. */
190-
function getAllNodeModulesDirectories(currentDirectory: string, host: ModuleResolutionHost): string[] | undefined {
191190
if (!host.directoryExists) {
192191
return [combinePaths(currentDirectory, "node_modules")];
193192
// And if it doesn't exist, tough.
194193
}
195194

196-
const all: string[] = [];
195+
let typeRoots: string[];
197196

198197
while (true) {
199-
const nodeModules = combinePaths(currentDirectory, "node_modules");
200-
if (host.directoryExists(nodeModules)) {
201-
all.push(nodeModules);
198+
const atTypes = combinePaths(currentDirectory, nodeModulesAtTypes);
199+
if (host.directoryExists(atTypes)) {
200+
(typeRoots || (typeRoots = [])).push(atTypes);
202201
}
203202

204203
const parent = getDirectoryPath(currentDirectory);
@@ -208,8 +207,10 @@ namespace ts {
208207
currentDirectory = parent;
209208
}
210209

211-
return all;
210+
return typeRoots;
212211
}
212+
const nodeModulesAtTypes = combinePaths("node_modules", "@types");
213+
213214
/**
214215
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
215216
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups

tests/baselines/reference/library-reference-3.trace.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[
2-
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory '/src/node_modules/@types'. ========",
3-
"Resolving with primary search path '/src/node_modules/@types'",
4-
"File '/src/node_modules/@types/jquery/package.json' does not exist.",
5-
"File '/src/node_modules/@types/jquery/index.d.ts' does not exist.",
2+
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory not set. ========",
3+
"Root directory cannot be determined, skipping primary search paths.",
64
"Looking up in 'node_modules' folder, initial location '/src'",
75
"File '/src/node_modules/jquery.ts' does not exist.",
86
"File '/src/node_modules/jquery.d.ts' does not exist.",

0 commit comments

Comments
 (0)