Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions apps/api-extractor/src/analyzer/AstSymbolTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ export class AstSymbolTable {
return this.#exportAnalyzer.fetchAstModuleExportInfo(astModule);
}

/**
* Returns the set of source files that contain a declaration that was analyzed, i.e. that is
* reachable from the entry point.
*
* @remarks
* API Extractor analyzes the compiler's `.d.ts` outputs, and by default does not use
* `skipLibCheck`, so type-checking the entire program would bind-and-check every transitively
* reachable declaration file -- including deep dependencies that are not part of the API surface.
* This set allows compiler diagnostics to be scoped to just the files that contribute
* declarations to the analyzed API.
*/
public collectAnalyzedSourceFiles(): Set<ts.SourceFile> {
const sourceFiles: Set<ts.SourceFile> = new Set<ts.SourceFile>();
for (const declaration of this.#astDeclarationsByDeclaration.keys()) {
sourceFiles.add(declaration.getSourceFile());
}
return sourceFiles;
}

/**
* Attempts to retrieve an export by name from the specified `AstModule`.
* Returns undefined if no match was found.
Expand Down
19 changes: 18 additions & 1 deletion apps/api-extractor/src/analyzer/TypeScriptInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,24 @@ export class TypeScriptInternals {
if (!typeChecker.getEmitResolver) {
throw new InternalError('Missing TypeChecker.getEmitResolver');
}
const resolver: any = typeChecker.getEmitResolver();

// We only use `EmitResolver.hasGlobalName`, which simply queries the type checker's `globals`
// symbol table. That table (including ambient declarations, `jsGlobalAugmentations`, and
// `declare global` augmentations) is populated synchronously when the checker is created, so it
// does not require type checking to have run.
//
// By default, `getEmitResolver()` forces a full-program diagnostic type-check before returning
// the resolver. Since API Extractor otherwise only type-checks the source files that are
// reachable from the entry point (see `Collector.analyze`), that full check would dominate the
// analysis cost. Passing `skipDiagnostics: true` avoids it while still returning a resolver
// whose `hasGlobalName` behaves identically. The parameter is a compiler internal; older
// TypeScript versions that lack it simply ignore the extra argument (falling back to the
// previous, slower behavior).
const resolver: any = typeChecker.getEmitResolver(
/*sourceFile*/ undefined,
/*cancellationToken*/ undefined,
/*skipDiagnostics*/ true
);
if (!resolver.hasGlobalName) {
throw new InternalError('Missing EmitResolver.hasGlobalName');
}
Expand Down
22 changes: 15 additions & 7 deletions apps/api-extractor/src/collector/Collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ export class Collector {
throw new Error('DtsRollupGenerator.analyze() was already called');
}

// This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations)
// with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday
// compile errors that would result from a full compilation.
for (const diagnostic of this.#program.getSemanticDiagnostics()) {
this.messageRouter.addCompilerDiagnostic(diagnostic);
}

const sourceFiles: readonly ts.SourceFile[] = this.program.getSourceFiles();

if (this.messageRouter.showDiagnostics) {
Expand Down Expand Up @@ -294,6 +287,21 @@ export class Collector {
}
}

// Report compiler diagnostics only for the files that contribute to the API report: the files
// with an analyzed declaration, plus the intermediate re-export files visited while resolving
// exports. Diagnostics for unrelated files are the responsibility of the normal compiler build,
// not API Extractor. (See `collectAnalyzedSourceFiles()` for why checking the whole program here
// would be needlessly expensive.)
const diagnosticSourceFiles: Set<ts.SourceFile> = this.astSymbolTable.collectAnalyzedSourceFiles();
for (const sourceFile of nonExternalSourceFiles) {
diagnosticSourceFiles.add(sourceFile);
}
for (const sourceFile of diagnosticSourceFiles) {
for (const diagnostic of this.#program.getSemanticDiagnostics(sourceFile)) {
this.messageRouter.addCompilerDiagnostic(diagnostic);
}
}

// Here, we're collecting reference directives from all non-external source files
// that were encountered while looking for exports, but only those references that
// were explicitly written by the developer and marked with the `preserve="true"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "Improve analysis performance by scoping compiler diagnostics to the source files that are reachable from the entry point, rather than type-checking the entire program. As a result, compiler errors are now only reported for declarations that contribute to the analyzed API surface.",
"type": "minor",
"packageName": "@microsoft/api-extractor"
}
],
"packageName": "@microsoft/api-extractor",
"email": "dmichon-msft@users.noreply.github.com"
}
Loading