Skip to content

Commit 933b463

Browse files
committed
Throw an early error if API Extractor attempts to process non.d.ts files
1 parent 57d2b0c commit 933b463

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

apps/api-extractor/src/collector/Collector.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export class Collector {
188188
this.messageRouter.addCompilerDiagnostic(diagnostic);
189189
}
190190

191+
const sourceFiles: readonly ts.SourceFile[] = this.program.getSourceFiles();
192+
191193
if (this.messageRouter.showDiagnostics) {
192194
this.messageRouter.logDiagnosticHeader('Root filenames');
193195
for (const fileName of this.program.getRootFileNames()) {
@@ -196,12 +198,25 @@ export class Collector {
196198
this.messageRouter.logDiagnosticFooter();
197199

198200
this.messageRouter.logDiagnosticHeader('Files analyzed by compiler');
199-
for (const sourceFile of this.program.getSourceFiles()) {
201+
for (const sourceFile of sourceFiles) {
200202
this.messageRouter.logDiagnostic(sourceFile.fileName);
201203
}
202204
this.messageRouter.logDiagnosticFooter();
203205
}
204206

207+
// We can throw this error earlier in CompilerState.ts, but intentionally wait until after we've logged the
208+
// associated diagnostic message above to make debugging easier for developers.
209+
const badSourceFile: boolean = sourceFiles.some(
210+
({ fileName }) => !ExtractorConfig.hasDtsFileExtension(fileName)
211+
);
212+
if (badSourceFile) {
213+
throw new Error(
214+
'API Extractor expects to only process .d.ts files, but encountered non-.d.ts file(s).\n' +
215+
'Run with the "--diagnostics" flag and inspect the "Files analyzed by compiler" to find the unexpected ' +
216+
'file(s).'
217+
);
218+
}
219+
205220
// Build the entry point
206221
const entryPointSourceFile: ts.SourceFile = this.workingPackage.entryPointSourceFile;
207222

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Throw an error early if API Extractor will attempt to process non-.d.ts files",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

0 commit comments

Comments
 (0)