@@ -7,6 +7,7 @@ namespace ts {
77 /* @internal */ export let emitTime = 0 ;
88 /* @internal */ export let ioReadTime = 0 ;
99 /* @internal */ export let ioWriteTime = 0 ;
10+ /* @internal */ export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024 ;
1011
1112 /** The version of the TypeScript compiler release */
1213
@@ -348,6 +349,8 @@ namespace ts {
348349 let diagnosticsProducingTypeChecker : TypeChecker ;
349350 let noDiagnosticsTypeChecker : TypeChecker ;
350351 let classifiableNames : Map < string > ;
352+ const programSizeLimitExceeded = - 1 ;
353+ let programSizeForNonTsFiles = 0 ;
351354
352355 let skipDefaultLib = options . noLib ;
353356 const supportedExtensions = getSupportedExtensions ( options ) ;
@@ -394,7 +397,8 @@ namespace ts {
394397 ( oldOptions . target !== options . target ) ||
395398 ( oldOptions . noLib !== options . noLib ) ||
396399 ( oldOptions . jsx !== options . jsx ) ||
397- ( oldOptions . allowJs !== options . allowJs ) ) {
400+ ( oldOptions . allowJs !== options . allowJs ) ||
401+ ( oldOptions . disableSizeLimit !== options . disableSizeLimit ) ) {
398402 oldProgram = undefined ;
399403 }
400404 }
@@ -442,6 +446,10 @@ namespace ts {
442446
443447 return program ;
444448
449+ function exceedProgramSizeLimit ( ) {
450+ return ! options . disableSizeLimit && programSizeForNonTsFiles === programSizeLimitExceeded ;
451+ }
452+
445453 function getCommonSourceDirectory ( ) {
446454 if ( typeof commonSourceDirectory === "undefined" ) {
447455 if ( options . rootDir && checkSourceFilesBelongToPath ( files , options . rootDir ) ) {
@@ -1054,7 +1062,7 @@ namespace ts {
10541062 }
10551063 }
10561064
1057- if ( diagnostic ) {
1065+ if ( diagnostic && ! exceedProgramSizeLimit ( ) ) {
10581066 if ( refFile !== undefined && refEnd !== undefined && refPos !== undefined ) {
10591067 fileProcessingDiagnostics . add ( createFileDiagnostic ( refFile , refPos , refEnd - refPos , diagnostic , ...diagnosticArgument ) ) ;
10601068 }
@@ -1087,6 +1095,11 @@ namespace ts {
10871095 return file ;
10881096 }
10891097
1098+ const isNonTsFile = ! hasTypeScriptFileExtension ( fileName ) ;
1099+ if ( isNonTsFile && exceedProgramSizeLimit ( ) ) {
1100+ return undefined ;
1101+ }
1102+
10901103 // We haven't looked for this file, do so now and cache result
10911104 const file = host . getSourceFile ( fileName , options . target , hostErrorMessage => {
10921105 if ( refFile !== undefined && refPos !== undefined && refEnd !== undefined ) {
@@ -1098,6 +1111,25 @@ namespace ts {
10981111 }
10991112 } ) ;
11001113
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+
11011133 filesByName . set ( path , file ) ;
11021134 if ( file ) {
11031135 file . path = path ;
0 commit comments