Skip to content

Commit 9b7d72c

Browse files
committed
Merge pull request #7228 from Microsoft/bundledDeclarationEmit
avoid multiple passes over the program when computing diagnostics for…
2 parents 0f67f4b + e090043 commit 9b7d72c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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
@@ -1030,7 +1030,14 @@ namespace ts {
10301030
}
10311031

10321032
function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1033-
return getDiagnosticsHelper(sourceFile, getDeclarationDiagnosticsForFile, cancellationToken);
1033+
const options = program.getCompilerOptions();
1034+
// collect diagnostics from the program only once if either no source file was specified or out/outFile is set (bundled emit)
1035+
if (!sourceFile || options.out || options.outFile) {
1036+
return getDeclarationDiagnosticsWorker(sourceFile, cancellationToken);
1037+
}
1038+
else {
1039+
return getDiagnosticsHelper(sourceFile, getDeclarationDiagnosticsForFile, cancellationToken);
1040+
}
10341041
}
10351042

10361043
function getSyntacticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
@@ -1244,17 +1251,19 @@ namespace ts {
12441251
});
12451252
}
12461253

1247-
function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1254+
function getDeclarationDiagnosticsWorker(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
12481255
return runWithCancellationToken(() => {
1249-
if (!isDeclarationFile(sourceFile)) {
1250-
const resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken);
1251-
// Don't actually write any files since we're just getting diagnostics.
1252-
const writeFile: WriteFileCallback = () => { };
1253-
return ts.getDeclarationDiagnostics(getEmitHost(writeFile), resolver, sourceFile);
1254-
}
1256+
const resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken);
1257+
// Don't actually write any files since we're just getting diagnostics.
1258+
const writeFile: WriteFileCallback = () => { };
1259+
return ts.getDeclarationDiagnostics(getEmitHost(writeFile), resolver, sourceFile);
12551260
});
12561261
}
12571262

1263+
function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1264+
return isDeclarationFile(sourceFile) ? [] : getDeclarationDiagnosticsWorker(sourceFile, cancellationToken);
1265+
}
1266+
12581267
function getOptionsDiagnostics(): Diagnostic[] {
12591268
const allDiagnostics: Diagnostic[] = [];
12601269
addRange(allDiagnostics, fileProcessingDiagnostics.getGlobalDiagnostics());

0 commit comments

Comments
 (0)