@@ -677,7 +677,14 @@ namespace ts {
677677 }
678678
679679 function getDeclarationDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
680- return getDiagnosticsHelper ( sourceFile , getDeclarationDiagnosticsForFile , cancellationToken ) ;
680+ const options = program . getCompilerOptions ( ) ;
681+ // collect diagnostics from the program only once if either no source file was specified or out/outFile is set (bundled emit)
682+ if ( ! sourceFile || options . out || options . outFile ) {
683+ return getDeclarationDiagnosticsWorker ( sourceFile , cancellationToken ) ;
684+ }
685+ else {
686+ return getDiagnosticsHelper ( sourceFile , getDeclarationDiagnosticsForFile , cancellationToken ) ;
687+ }
681688 }
682689
683690 function getSyntacticDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
@@ -890,17 +897,19 @@ namespace ts {
890897 } ) ;
891898 }
892899
893- function getDeclarationDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
900+ function getDeclarationDiagnosticsWorker ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
894901 return runWithCancellationToken ( ( ) => {
895- if ( ! isDeclarationFile ( sourceFile ) ) {
896- const resolver = getDiagnosticsProducingTypeChecker ( ) . getEmitResolver ( sourceFile , cancellationToken ) ;
897- // Don't actually write any files since we're just getting diagnostics.
898- const writeFile : WriteFileCallback = ( ) => { } ;
899- return ts . getDeclarationDiagnostics ( getEmitHost ( writeFile ) , resolver , sourceFile ) ;
900- }
902+ const resolver = getDiagnosticsProducingTypeChecker ( ) . getEmitResolver ( sourceFile , cancellationToken ) ;
903+ // Don't actually write any files since we're just getting diagnostics.
904+ const writeFile : WriteFileCallback = ( ) => { } ;
905+ return ts . getDeclarationDiagnostics ( getEmitHost ( writeFile ) , resolver , sourceFile ) ;
901906 } ) ;
902907 }
903908
909+ function getDeclarationDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
910+ return isDeclarationFile ( sourceFile ) ? [ ] : getDeclarationDiagnosticsWorker ( sourceFile , cancellationToken ) ;
911+ }
912+
904913 function getOptionsDiagnostics ( ) : Diagnostic [ ] {
905914 const allDiagnostics : Diagnostic [ ] = [ ] ;
906915 addRange ( allDiagnostics , fileProcessingDiagnostics . getGlobalDiagnostics ( ) ) ;
0 commit comments