Skip to content

Commit e51fb15

Browse files
committed
Merge pull request microsoft#7228 from Microsoft/bundledDeclarationEmit
avoid multiple passes over the program when computing diagnostics for…
1 parent e5dd34f commit e51fb15

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace ts {
3333
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {
3434
const declarationDiagnostics = createDiagnosticCollection();
3535
forEachExpectedEmitFile(host, getDeclarationDiagnosticsFromFile, targetSourceFile);
36-
return declarationDiagnostics.getDiagnostics(targetSourceFile.fileName);
36+
return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined);
3737

3838
function getDeclarationDiagnosticsFromFile({ declarationFilePath }, sources: SourceFile[], isBundledEmit: boolean) {
3939
emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit);

src/compiler/program.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)