Skip to content

Commit e9a2b58

Browse files
authored
Merge pull request #3341 from zelliott/better-ts-file-error
[api-extractor] Throw an error if API Extractor attempts to process non-.d.ts files
2 parents bc3229b + faa5b37 commit e9a2b58

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

apps/api-extractor/src/api/ExtractorMessageId.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ export const enum ExtractorMessageId {
9595
/**
9696
* "The property ___ has a setter but no getter."
9797
*/
98-
MissingGetter = 'ae-missing-getter'
98+
MissingGetter = 'ae-missing-getter',
99+
100+
/**
101+
* "Incorrect file type; API Extractor expects to analyze compiler outputs with the .d.ts file extension.
102+
* Troubleshooting tips: `https://api-extractor.com/link/dts-error`"
103+
*/
104+
WrongInputFileType = 'ae-wrong-input-file-type'
99105
}
100106

101107
export const allExtractorMessageIds: Set<string> = new Set<string>([
@@ -114,5 +120,6 @@ export const allExtractorMessageIds: Set<string> = new Set<string>([
114120
'ae-cyclic-inherit-doc',
115121
'ae-unresolved-link',
116122
'ae-setter-with-docs',
117-
'ae-missing-getter'
123+
'ae-missing-getter',
124+
'ae-wrong-input-file-type'
118125
]);

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

Lines changed: 19 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,28 @@ 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+
// Typically there will be many such files -- to avoid too much noise, only report the first one.
210+
const badSourceFile: ts.SourceFile | undefined = sourceFiles.find(
211+
({ fileName }) => !ExtractorConfig.hasDtsFileExtension(fileName)
212+
);
213+
if (badSourceFile) {
214+
this.messageRouter.addAnalyzerIssueForPosition(
215+
ExtractorMessageId.WrongInputFileType,
216+
'Incorrect file type; API Extractor expects to analyze compiler outputs with the .d.ts file extension. ' +
217+
'Troubleshooting tips: https://api-extractor.com/link/dts-error',
218+
badSourceFile,
219+
0
220+
);
221+
}
222+
205223
// Build the entry point
206224
const entryPointSourceFile: ts.SourceFile = this.workingPackage.entryPointSourceFile;
207225

apps/api-extractor/src/schemas/api-extractor-defaults.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
"ae-unresolved-inheritdoc-base": {
7373
"logLevel": "warning",
7474
"addToApiReportFile": true
75+
},
76+
"ae-wrong-input-file-type": {
77+
"logLevel": "error"
7578
}
7679
},
7780
"tsdocMessageReporting": {
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": "minor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

common/reviews/api/api-extractor.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export const enum ExtractorMessageId {
141141
SetterWithDocs = "ae-setter-with-docs",
142142
UnresolvedInheritDocBase = "ae-unresolved-inheritdoc-base",
143143
UnresolvedInheritDocReference = "ae-unresolved-inheritdoc-reference",
144-
UnresolvedLink = "ae-unresolved-link"
144+
UnresolvedLink = "ae-unresolved-link",
145+
WrongInputFileType = "ae-wrong-input-file-type"
145146
}
146147

147148
// @public

0 commit comments

Comments
 (0)