Skip to content

Commit 8f57395

Browse files
MLuc24MLuc24
andauthored
[api-extractor] Report unresolvable inline import paths in .d.ts rollups (#5925)
When an inline import() type could not be resolved to a rolled up entity, its span was emitted verbatim. A relative path such as import('../Bar') means nothing next to the rollup, which does not preserve the original file layout, so the emitted .d.ts does not compile and the only clue was an unrelated ae-forgotten-export warning. Such paths are now reported as ae-unresolved-import-path. Co-authored-by: MLuc24 <test.prediction3d2@gmail.com>
1 parent 455f22d commit 8f57395

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ export enum ExtractorMessageId {
116116
*/
117117
MissingGetter = 'ae-missing-getter',
118118

119+
/**
120+
* "The inline import path ___ cannot be resolved in the .d.ts rollup, because the rollup does not
121+
* preserve the original file layout."
122+
*/
123+
UnresolvedImportPath = 'ae-unresolved-import-path',
124+
119125
/**
120126
* "Incorrect file type; API Extractor expects to analyze compiler outputs with the .d.ts file extension.
121127
* Troubleshooting tips: `https://api-extractor.com/link/dts-error`"
@@ -141,5 +147,6 @@ export const allExtractorMessageIds: Set<string> = new Set<string>([
141147
'ae-unresolved-link',
142148
'ae-setter-with-docs',
143149
'ae-missing-getter',
150+
'ae-unresolved-import-path',
144151
'ae-wrong-input-file-type'
145152
]);

apps/api-extractor/src/generators/DtsEmitHelpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { Span } from '../analyzer/Span';
1313
import type { IndentedWriter } from './IndentedWriter';
1414
import { SourceFileLocationFormatter } from '../analyzer/SourceFileLocationFormatter';
1515
import { TypeScriptHelpers } from '../analyzer/TypeScriptHelpers';
16+
import { ExtractorMessageId } from '../api/ExtractorMessageId';
1617

1718
/**
1819
* Some common code shared between DtsRollupGenerator and ApiReportGenerator.
@@ -170,6 +171,20 @@ export class DtsEmitHelpers {
170171
span.modification.skipAll();
171172
span.modification.prefix = `${referencedEntity.nameForEmit}${typeArgumentsText}${separatorAfter}`;
172173
}
174+
} else if (ts.isLiteralTypeNode(node.argument) && ts.isStringLiteral(node.argument.literal)) {
175+
// The import was not resolved to a rolled up entity, so its span gets emitted verbatim. A relative
176+
// path is meaningless in the rollup, which does not preserve the original file layout, so the
177+
// emitted .d.ts would not compile. Report that instead of leaving the user to discover it later.
178+
const modulePath: string = node.argument.literal.text;
179+
if (modulePath.startsWith('.')) {
180+
collector.messageRouter.addAnalyzerIssue(
181+
ExtractorMessageId.UnresolvedImportPath,
182+
`The inline import path "${modulePath}" could not be resolved, so it would be emitted unchanged` +
183+
` into the .d.ts rollup, where it does not resolve to anything. Import the symbol at the top` +
184+
` of the file instead of using an inline import() type.`,
185+
astDeclaration
186+
);
187+
}
173188
}
174189
}
175190

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Report a new `ae-unresolved-import-path` message when an inline `import()` type with a relative path cannot be resolved, instead of silently emitting the unusable path into the .d.ts rollup.",
5+
"type": "minor",
6+
"packageName": "@microsoft/api-extractor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export enum ExtractorMessageId {
158158
PreapprovedUnsupportedType = "ae-preapproved-unsupported-type",
159159
SetterWithDocs = "ae-setter-with-docs",
160160
Undocumented = "ae-undocumented",
161+
UnresolvedImportPath = "ae-unresolved-import-path",
161162
UnresolvedInheritDocBase = "ae-unresolved-inheritdoc-base",
162163
UnresolvedInheritDocReference = "ae-unresolved-inheritdoc-reference",
163164
UnresolvedLink = "ae-unresolved-link",

0 commit comments

Comments
 (0)