@@ -7,6 +7,7 @@ namespace ts {
7
7
/* @internal */ export let emitTime = 0 ;
8
8
/* @internal */ export let ioReadTime = 0 ;
9
9
/* @internal */ export let ioWriteTime = 0 ;
10
+ /* @internal */ export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024 ;
10
11
11
12
/** The version of the TypeScript compiler release */
12
13
@@ -348,6 +349,8 @@ namespace ts {
348
349
let diagnosticsProducingTypeChecker : TypeChecker ;
349
350
let noDiagnosticsTypeChecker : TypeChecker ;
350
351
let classifiableNames : Map < string > ;
352
+ const programSizeLimitExceeded = - 1 ;
353
+ let programSizeForNonTsFiles = 0 ;
351
354
352
355
let skipDefaultLib = options . noLib ;
353
356
const supportedExtensions = getSupportedExtensions ( options ) ;
@@ -394,7 +397,8 @@ namespace ts {
394
397
( oldOptions . target !== options . target ) ||
395
398
( oldOptions . noLib !== options . noLib ) ||
396
399
( oldOptions . jsx !== options . jsx ) ||
397
- ( oldOptions . allowJs !== options . allowJs ) ) {
400
+ ( oldOptions . allowJs !== options . allowJs ) ||
401
+ ( oldOptions . disableSizeLimit !== options . disableSizeLimit ) ) {
398
402
oldProgram = undefined ;
399
403
}
400
404
}
@@ -442,6 +446,10 @@ namespace ts {
442
446
443
447
return program ;
444
448
449
+ function exceedProgramSizeLimit ( ) {
450
+ return ! options . disableSizeLimit && programSizeForNonTsFiles === programSizeLimitExceeded ;
451
+ }
452
+
445
453
function getCommonSourceDirectory ( ) {
446
454
if ( typeof commonSourceDirectory === "undefined" ) {
447
455
if ( options . rootDir && checkSourceFilesBelongToPath ( files , options . rootDir ) ) {
@@ -1054,7 +1062,7 @@ namespace ts {
1054
1062
}
1055
1063
}
1056
1064
1057
- if ( diagnostic ) {
1065
+ if ( diagnostic && ! exceedProgramSizeLimit ( ) ) {
1058
1066
if ( refFile !== undefined && refEnd !== undefined && refPos !== undefined ) {
1059
1067
fileProcessingDiagnostics . add ( createFileDiagnostic ( refFile , refPos , refEnd - refPos , diagnostic , ...diagnosticArgument ) ) ;
1060
1068
}
@@ -1087,6 +1095,11 @@ namespace ts {
1087
1095
return file ;
1088
1096
}
1089
1097
1098
+ const isNonTsFile = ! hasTypeScriptFileExtension ( fileName ) ;
1099
+ if ( isNonTsFile && exceedProgramSizeLimit ( ) ) {
1100
+ return undefined ;
1101
+ }
1102
+
1090
1103
// We haven't looked for this file, do so now and cache result
1091
1104
const file = host . getSourceFile ( fileName , options . target , hostErrorMessage => {
1092
1105
if ( refFile !== undefined && refPos !== undefined && refEnd !== undefined ) {
@@ -1098,6 +1111,25 @@ namespace ts {
1098
1111
}
1099
1112
} ) ;
1100
1113
1114
+ if ( isNonTsFile && ! options . disableSizeLimit && file && file . text ) {
1115
+ programSizeForNonTsFiles += file . text . length ;
1116
+ if ( programSizeForNonTsFiles > maxProgramSizeForNonTsFiles ) {
1117
+ // If the program size limit was reached when processing a file, this file is
1118
+ // likely in the problematic folder than contains too many files.
1119
+ // Normally the folder is one level down from the commonSourceDirectory, for example,
1120
+ // if the commonSourceDirectory is "/src/", and the last processed path was "/src/node_modules/a/b.js",
1121
+ // we should show in the error message "/src/node_modules/".
1122
+ const commonSourceDirectory = getCommonSourceDirectory ( ) ;
1123
+ let rootLevelDirectory = path . substring ( 0 , Math . max ( commonSourceDirectory . length , path . indexOf ( directorySeparator , commonSourceDirectory . length ) ) ) ;
1124
+ if ( rootLevelDirectory [ rootLevelDirectory . length - 1 ] !== directorySeparator ) {
1125
+ rootLevelDirectory += directorySeparator ;
1126
+ }
1127
+ programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . Too_many_JavaScript_files_in_the_project_Consider_specifying_the_exclude_setting_in_project_configuration_to_limit_included_source_folders_The_likely_folder_to_exclude_is_0_To_disable_the_project_size_limit_set_the_disableSizeLimit_compiler_option_to_true , rootLevelDirectory ) ) ;
1128
+ programSizeForNonTsFiles = programSizeLimitExceeded ;
1129
+ return undefined ;
1130
+ }
1131
+ }
1132
+
1101
1133
filesByName . set ( path , file ) ;
1102
1134
if ( file ) {
1103
1135
file . path = path ;
0 commit comments