Skip to content

[api-extractor] (fix) Fix module resolution for some type import declarations #5288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion apps/api-extractor/src/analyzer/ExportAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ export class ExportAnalyzer {
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
moduleSpecifier: string
): boolean {
const specifier: ts.TypeNode | ts.Expression | undefined = ts.isImportTypeNode(importOrExportDeclaration)
let specifier: ts.TypeNode | ts.Expression | undefined = ts.isImportTypeNode(importOrExportDeclaration)
? importOrExportDeclaration.argument
: importOrExportDeclaration.moduleSpecifier;
if (specifier && ts.isLiteralTypeNode(specifier)) {
specifier = specifier.literal;
}
const mode: ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined =
specifier && ts.isStringLiteralLike(specifier)
? TypeScriptInternals.getModeForUsageLocation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"metadata": {
"toolPackage": "@microsoft/api-extractor",
"toolVersion": "[test mode]",
"schemaVersion": 1011,
"oldestForwardsCompatibleVersion": 1001,
"tsdocConfig": {
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"noStandardTags": true,
"tagDefinitions": [
{
"tagName": "@alpha",
"syntaxKind": "modifier"
},
{
"tagName": "@beta",
"syntaxKind": "modifier"
},
{
"tagName": "@defaultValue",
"syntaxKind": "block"
},
{
"tagName": "@decorator",
"syntaxKind": "block",
"allowMultiple": true
},
{
"tagName": "@deprecated",
"syntaxKind": "block"
},
{
"tagName": "@eventProperty",
"syntaxKind": "modifier"
},
{
"tagName": "@example",
"syntaxKind": "block",
"allowMultiple": true
},
{
"tagName": "@experimental",
"syntaxKind": "modifier"
},
{
"tagName": "@inheritDoc",
"syntaxKind": "inline"
},
{
"tagName": "@internal",
"syntaxKind": "modifier"
},
{
"tagName": "@label",
"syntaxKind": "inline"
},
{
"tagName": "@link",
"syntaxKind": "inline",
"allowMultiple": true
},
{
"tagName": "@override",
"syntaxKind": "modifier"
},
{
"tagName": "@packageDocumentation",
"syntaxKind": "modifier"
},
{
"tagName": "@param",
"syntaxKind": "block",
"allowMultiple": true
},
{
"tagName": "@privateRemarks",
"syntaxKind": "block"
},
{
"tagName": "@public",
"syntaxKind": "modifier"
},
{
"tagName": "@readonly",
"syntaxKind": "modifier"
},
{
"tagName": "@remarks",
"syntaxKind": "block"
},
{
"tagName": "@returns",
"syntaxKind": "block"
},
{
"tagName": "@sealed",
"syntaxKind": "modifier"
},
{
"tagName": "@see",
"syntaxKind": "block"
},
{
"tagName": "@throws",
"syntaxKind": "block",
"allowMultiple": true
},
{
"tagName": "@typeParam",
"syntaxKind": "block",
"allowMultiple": true
},
{
"tagName": "@virtual",
"syntaxKind": "modifier"
},
{
"tagName": "@betaDocumentation",
"syntaxKind": "modifier"
},
{
"tagName": "@internalRemarks",
"syntaxKind": "block"
},
{
"tagName": "@preapproved",
"syntaxKind": "modifier"
}
],
"supportForTags": {
"@alpha": true,
"@beta": true,
"@defaultValue": true,
"@decorator": true,
"@deprecated": true,
"@eventProperty": true,
"@example": true,
"@experimental": true,
"@inheritDoc": true,
"@internal": true,
"@label": true,
"@link": true,
"@override": true,
"@packageDocumentation": true,
"@param": true,
"@privateRemarks": true,
"@public": true,
"@readonly": true,
"@remarks": true,
"@returns": true,
"@sealed": true,
"@see": true,
"@throws": true,
"@typeParam": true,
"@virtual": true,
"@betaDocumentation": true,
"@internalRemarks": true,
"@preapproved": true
},
"reportUnsupportedHtmlElements": false
}
},
"kind": "Package",
"canonicalReference": "api-extractor-scenarios!",
"docComment": "",
"name": "api-extractor-scenarios",
"preserveMemberOrder": false,
"members": [
{
"kind": "EntryPoint",
"canonicalReference": "api-extractor-scenarios!",
"name": "",
"preserveMemberOrder": false,
"members": [
{
"kind": "Variable",
"canonicalReference": "api-extractor-scenarios!reexport:var",
"docComment": "/**\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "reexport: "
},
{
"kind": "Content",
"text": "import(\"./other\")."
},
{
"kind": "Reference",
"text": "Foo",
"canonicalReference": "api-extractor-scenarios!~Foo:class"
}
],
"fileUrlPath": "src/bundlerModuleResolution/index.ts",
"isReadonly": true,
"releaseTag": "Public",
"name": "reexport",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 3
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## API Report File for "api-extractor-scenarios"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).

```ts

// Warning: (ae-forgotten-export) The symbol "Foo" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export const reexport: Foo;

// (No @packageDocumentation comment for this package)

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare class Foo {
}

/**
* @public
*/
export declare const reexport: Foo;

export { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compiler": {
"tsconfigFilePath": "<projectFolder>/src/bundlerModuleResolution/config/tsconfig.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { foo } from './other';

/**
* @public
*/
export const reexport = foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {}

export const foo = new Foo();
18 changes: 16 additions & 2 deletions build-tests/run-scenarios-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function runScenariosAsync(
{ libFolderPath, additionalApiExtractorConfig, afterApiExtractorAsync }: IRunScenariosOptions
): Promise<void> {
const entryPoints: string[] = [];
const scenariosWithCustomCompilerOptions: string[] = [];

const scenarioFolderNames: string[] = [];
const folderItems: FolderItem[] = await FileSystem.readFolderItemsAsync(libFolderPath);
Expand All @@ -56,6 +57,10 @@ export async function runScenariosAsync(
}
}

if (apiExtractorJsonOverrides && 'compiler' in apiExtractorJsonOverrides) {
scenariosWithCustomCompilerOptions.push(scenarioFolderName);
}

const apiExtractorJson: {} = {
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json',

Expand Down Expand Up @@ -93,18 +98,27 @@ export async function runScenariosAsync(
{ concurrency: 10 }
);

let compilerState: CompilerState | undefined = undefined;
let baseCompilerState: CompilerState | undefined = undefined;
for (const scenarioFolderName of scenarioFolderNames) {
logger.terminal.writeLine(`Scenario: ${scenarioFolderName}`);

// Run API Extractor programmatically
const apiExtractorJsonPath: string = `${buildFolderPath}/temp/configs/api-extractor-${scenarioFolderName}.json`;
const extractorConfig: ExtractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath);

if (!compilerState) {
let compilerState: CompilerState;
if (scenariosWithCustomCompilerOptions.includes(scenarioFolderName)) {
logger.terminal.writeLine(`Using custom compiler state (${scenarioFolderName})`);
compilerState = CompilerState.create(extractorConfig, {
additionalEntryPoints: entryPoints
});
} else {
if (!baseCompilerState) {
baseCompilerState = CompilerState.create(extractorConfig, {
additionalEntryPoints: entryPoints
});
}
compilerState = baseCompilerState;
}

const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "Fixes a bug in ExtractorAnalyzer._isExternalModulePath where type import declarations were not resolved.",
"type": "patch"
}
],
"packageName": "@microsoft/api-extractor"
}