Skip to content

Commit e9a0c56

Browse files
committed
Fixed bugs and linting
1 parent 6fba804 commit e9a0c56

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/compiler/program.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,16 @@ namespace ts {
10731073
// - This calls resolveModuleNames, and then calls findSourceFile for each resolved module.
10741074
// As all these operations happen - and are nested - within the createProgram call, they close over the below variables.
10751075
// The current resolution depth is tracked by incrementing/decrementing as the depth first search progresses.
1076-
const maxNodeModulesJsDepth = options.maxNodeModuleJsDepth || 2;
1076+
const maxNodeModulesJsDepth = typeof options.maxNodeModuleJsDepth === "number" ? options.maxNodeModuleJsDepth : 2;
10771077
let currentNodeModulesJsDepth = 0;
10781078

1079+
// If a module has some of its imports skipped due to being at the depth limit under node_modules, then track
1080+
// this, as it may be imported at a shallower depth later, and then it will need its skipped imports processed.
1081+
const modulesWithElidedImports: Map<boolean> = {};
1082+
1083+
// Track source files that are JavaScript files found by searching under node_modules, as these shouldn't be compiled.
1084+
const jsFilesFoundSearchingNodeModules: Map<boolean> = {};
1085+
10791086
const start = new Date().getTime();
10801087

10811088
host = host || createCompilerHost(options);
@@ -1230,6 +1237,7 @@ namespace ts {
12301237
(oldOptions.rootDir !== options.rootDir) ||
12311238
(oldOptions.configFilePath !== options.configFilePath) ||
12321239
(oldOptions.baseUrl !== options.baseUrl) ||
1240+
(oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) ||
12331241
!arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) ||
12341242
!arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) ||
12351243
!mapIsEqualTo(oldOptions.paths, options.paths)) {
@@ -1353,7 +1361,10 @@ namespace ts {
13531361
getNewLine: () => host.getNewLine(),
13541362
getSourceFile: program.getSourceFile,
13551363
getSourceFileByPath: program.getSourceFileByPath,
1356-
getSourceFiles: program.getSourceFiles,
1364+
getSourceFiles: () => filter(program.getSourceFiles(),
1365+
// Remove JavaScript files found by searching node_modules from the source files to emit
1366+
sourceFile => !lookUp(jsFilesFoundSearchingNodeModules, sourceFile.path)
1367+
),
13571368
writeFile: writeFileCallback || (
13581369
(fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)),
13591370
isEmitBlocked,
@@ -1888,6 +1899,14 @@ namespace ts {
18881899
reportFileNamesDifferOnlyInCasingError(fileName, file.fileName, refFile, refPos, refEnd);
18891900
}
18901901

1902+
// See if we need to reprocess the imports due to prior skipped imports
1903+
if (file && lookUp(modulesWithElidedImports, file.path)) {
1904+
if (currentNodeModulesJsDepth < maxNodeModulesJsDepth) { // TODO: Check for off-by-ones
1905+
modulesWithElidedImports[file.path] = false;
1906+
processImportedModules(file, getDirectoryPath(fileName));
1907+
}
1908+
}
1909+
18911910
return file;
18921911
}
18931912

@@ -2026,27 +2045,36 @@ namespace ts {
20262045
for (let i = 0; i < moduleNames.length; i++) {
20272046
const resolution = resolutions[i];
20282047
setResolvedModule(file, moduleNames[i], resolution);
2048+
const resolvedPath = resolution ? toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined;
2049+
20292050
// add file to program only if:
20302051
// - resolution was successful
20312052
// - noResolve is falsy
20322053
// - module name comes from the list of imports
20332054
// - it's not a top level JavaScript module that exceeded the search max
20342055
const isJsFileUnderNodeModules = resolution && resolution.isExternalLibraryImport &&
20352056
hasJavaScriptFileExtension(resolution.resolvedFileName);
2057+
20362058
if (isJsFileUnderNodeModules) {
2059+
jsFilesFoundSearchingNodeModules[resolvedPath] = true;
20372060
currentNodeModulesJsDepth++;
20382061
}
2039-
const shouldAddFile = resolution && !options.noResolve && i < file.imports.length &&
2040-
!(isJsFileUnderNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth);
20412062

2042-
if (shouldAddFile) {
2063+
const elideImport = isJsFileUnderNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth;
2064+
const shouldAddFile = resolution && !options.noResolve && i < file.imports.length && !elideImport;
2065+
2066+
if (elideImport) {
2067+
modulesWithElidedImports[file.path] = true;
2068+
}
2069+
else if (shouldAddFile) {
20432070
findSourceFile(resolution.resolvedFileName,
2044-
toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName),
2071+
resolvedPath,
20452072
/*isDefaultLib*/ false, /*isReference*/ false,
20462073
file,
20472074
skipTrivia(file.text, file.imports[i].pos),
20482075
file.imports[i].end);
20492076
}
2077+
20502078
if (isJsFileUnderNodeModules) {
20512079
currentNodeModulesJsDepth--;
20522080
}

src/compiler/utilities.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ namespace ts {
22752275
else {
22762276
const sourceFiles = targetSourceFile === undefined ? host.getSourceFiles() : [targetSourceFile];
22772277
for (const sourceFile of sourceFiles) {
2278-
// Don't emit if source file is a declaration file, or TODO: was found by a search under 'node_modules'
2278+
// Don't emit if source file is a declaration file
22792279
if (!isDeclarationFile(sourceFile)) {
22802280
onSingleFileEmit(host, sourceFile);
22812281
}
@@ -2310,7 +2310,6 @@ namespace ts {
23102310
function onBundledEmit(host: EmitHost) {
23112311
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
23122312
const bundledSources = filter(host.getSourceFiles(),
2313-
// TODO: Don't emit from source resolved by searching under node_modules
23142313
sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file
23152314
(!isExternalModule(sourceFile) || // non module file
23162315
!!getEmitModuleKind(options))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted

0 commit comments

Comments
 (0)