@@ -330,6 +330,7 @@ namespace ts {
330330 let files : SourceFile [ ] = [ ] ;
331331 let fileProcessingDiagnostics = createDiagnosticCollection ( ) ;
332332 let programDiagnostics = createDiagnosticCollection ( ) ;
333+ let hasEmitBlockingDiagnostics : Map < boolean > = { } ; // Map storing if there is emit blocking diagnostics for given input
333334
334335 let commonSourceDirectory : string ;
335336 let diagnosticsProducingTypeChecker : TypeChecker ;
@@ -374,13 +375,9 @@ namespace ts {
374375 }
375376 }
376377
377- verifyCompilerOptions ( ) ;
378-
379378 // unconditionally set oldProgram to undefined to prevent it from being captured in closure
380379 oldProgram = undefined ;
381380
382- programTime += new Date ( ) . getTime ( ) - start ;
383-
384381 program = {
385382 getRootFileNames : ( ) => rootNames ,
386383 getSourceFile : getSourceFile ,
@@ -403,6 +400,11 @@ namespace ts {
403400 getTypeCount : ( ) => getDiagnosticsProducingTypeChecker ( ) . getTypeCount ( ) ,
404401 getFileProcessingDiagnostics : ( ) => fileProcessingDiagnostics
405402 } ;
403+
404+ verifyCompilerOptions ( ) ;
405+
406+ programTime += new Date ( ) . getTime ( ) - start ;
407+
406408 return program ;
407409
408410 function getClassifiableNames ( ) {
@@ -519,6 +521,7 @@ namespace ts {
519521 getSourceFiles : program . getSourceFiles ,
520522 writeFile : writeFileCallback || (
521523 ( fileName , data , writeByteOrderMark , onError ) => host . writeFile ( fileName , data , writeByteOrderMark , onError ) ) ,
524+ isEmitBlocked : emitFileName => hasProperty ( hasEmitBlockingDiagnostics , emitFileName ) ,
522525 } ;
523526 }
524527
@@ -1245,6 +1248,55 @@ namespace ts {
12451248 options . target !== ScriptTarget . ES6 ) {
12461249 programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . Option_experimentalAsyncFunctions_cannot_be_specified_when_targeting_ES5_or_lower ) ) ;
12471250 }
1251+
1252+ if ( ! options . noEmit ) {
1253+ let emitHost = getEmitHost ( ) ;
1254+ let emitFilesSeen : Map < SourceFile [ ] > = { } ;
1255+
1256+ // Build map of files seen
1257+ for ( let file of files ) {
1258+ let { jsFilePath, declarationFilePath } = getEmitFileNames ( file , emitHost ) ;
1259+ if ( jsFilePath ) {
1260+ let filesEmittingJsFilePath = lookUp ( emitFilesSeen , jsFilePath ) ;
1261+ if ( ! filesEmittingJsFilePath ) {
1262+ emitFilesSeen [ jsFilePath ] = [ file ] ;
1263+ if ( options . declaration ) {
1264+ emitFilesSeen [ declarationFilePath ] = [ file ] ;
1265+ }
1266+ }
1267+ else {
1268+ filesEmittingJsFilePath . push ( file ) ;
1269+ }
1270+ }
1271+ }
1272+
1273+ // Verify that all the emit files are unique and dont overwrite input files
1274+ forEachKey ( emitFilesSeen , emitFilePath => {
1275+ // Report error if the output overwrites input file
1276+ if ( hasFile ( files , emitFilePath ) ) {
1277+ createEmitBlockingDiagnostics ( emitFilePath , Diagnostics . Cannot_write_file_0_which_is_one_of_the_input_files ) ;
1278+ }
1279+
1280+ // Report error if multiple files write into same file (except if specified by --out or --outFile)
1281+ if ( emitFilePath !== ( options . outFile || options . out ) ) {
1282+ // Not --out or --outFile emit, There should be single file emitting to this file
1283+ if ( emitFilesSeen [ emitFilePath ] . length > 1 ) {
1284+ createEmitBlockingDiagnostics ( emitFilePath , Diagnostics . Cannot_write_file_0_since_one_or_more_input_files_would_emit_into_it ) ;
1285+ }
1286+ }
1287+ else {
1288+ // --out or --outFile, error if there exist file emitting to single file colliding with --out
1289+ if ( forEach ( emitFilesSeen [ emitFilePath ] , sourceFile => shouldEmitToOwnFile ( sourceFile , options ) ) ) {
1290+ createEmitBlockingDiagnostics ( emitFilePath , Diagnostics . Cannot_write_file_0_since_one_or_more_input_files_would_emit_into_it ) ;
1291+ }
1292+ }
1293+ } ) ;
1294+ }
1295+ }
1296+
1297+ function createEmitBlockingDiagnostics ( emitFileName : string , message : DiagnosticMessage ) {
1298+ hasEmitBlockingDiagnostics [ emitFileName ] = true ;
1299+ programDiagnostics . add ( createCompilerDiagnostic ( message , emitFileName ) ) ;
12481300 }
12491301 }
12501302}
0 commit comments