X Tutup
Skip to content

Commit df16320

Browse files
committed
Basic implementation of "bundledPackages" feature
1 parent 774e16d commit df16320

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

apps/api-extractor/src/analyzer/AstSymbolTable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class AstSymbolTable {
8585
= new Map<ts.Identifier, AstEntity | undefined>();
8686

8787
public constructor(program: ts.Program, typeChecker: ts.TypeChecker, packageJsonLookup: PackageJsonLookup,
88-
messageRouter: MessageRouter) {
88+
bundledPackageNames: Set<string>, messageRouter: MessageRouter) {
8989

9090
this._program = program;
9191
this._typeChecker = typeChecker;
@@ -94,6 +94,7 @@ export class AstSymbolTable {
9494
this._exportAnalyzer = new ExportAnalyzer(
9595
this._program,
9696
this._typeChecker,
97+
bundledPackageNames,
9798
{
9899
analyze: this.analyze.bind(this),
99100
fetchAstSymbol: this._fetchAstSymbol.bind(this)

apps/api-extractor/src/analyzer/ExportAnalyzer.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as ts from 'typescript';
5-
import { InternalError } from '@microsoft/node-core-library';
5+
import { InternalError, PackageName } from '@microsoft/node-core-library';
66

77
import { TypeScriptHelpers } from './TypeScriptHelpers';
88
import { AstSymbol } from './AstSymbol';
@@ -55,6 +55,7 @@ interface IAstModuleReference {
5555
export class ExportAnalyzer {
5656
private readonly _program: ts.Program;
5757
private readonly _typeChecker: ts.TypeChecker;
58+
private readonly _bundledPackageNames: Set<string>;
5859
private readonly _astSymbolTable: IAstSymbolTable;
5960

6061
private readonly _astModulesByModuleSymbol: Map<ts.Symbol, AstModule>
@@ -65,9 +66,11 @@ export class ExportAnalyzer {
6566

6667
private readonly _astImportsByKey: Map<string, AstImport> = new Map<string, AstImport>();
6768

68-
public constructor(program: ts.Program, typeChecker: ts.TypeChecker, astSymbolTable: IAstSymbolTable) {
69+
public constructor(program: ts.Program, typeChecker: ts.TypeChecker, bundledPackageNames: Set<string>,
70+
astSymbolTable: IAstSymbolTable) {
6971
this._program = program;
7072
this._typeChecker = typeChecker;
73+
this._bundledPackageNames = bundledPackageNames;
7174
this._astSymbolTable = astSymbolTable;
7275
}
7376

@@ -93,7 +96,7 @@ export class ExportAnalyzer {
9396
if (moduleReference !== undefined) {
9497
// Match: "@microsoft/sp-lodash-subset" or "lodash/has"
9598
// but ignore: "../folder/LocalFile"
96-
if (!ts.isExternalModuleNameRelative(moduleReference.moduleSpecifier)) {
99+
if (this._isExternalModulePath(moduleReference.moduleSpecifier)) {
97100
externalModulePath = moduleReference.moduleSpecifier;
98101
}
99102
}
@@ -231,6 +234,30 @@ export class ExportAnalyzer {
231234
return entryPointAstModule.astModuleExportInfo;
232235
}
233236

237+
/**
238+
* Returns true if the module specifier refers to an external package. Ignores packages listed in the
239+
* "bundledPackages" setting from the api-extractor.json config file.
240+
*
241+
* @remarks
242+
* Examples:
243+
*
244+
* - NO: `./file1`
245+
* - YES: `library1`
246+
* - YES: `@my-scope/my-package`
247+
*/
248+
private _isExternalModulePath(moduleSpecifier: string): boolean {
249+
if (ts.isExternalModuleNameRelative(moduleSpecifier)) {
250+
return false;
251+
}
252+
253+
// TODO: The moduleSpecifier may include a path like "@my-scope/my-package/path1/path2".
254+
255+
if (this._bundledPackageNames.has(moduleSpecifier)) {
256+
return false;
257+
}
258+
return true;
259+
}
260+
234261
/**
235262
* Returns true if when we analyzed sourceFile, we found that it contains an "export=" statement that allows
236263
* it to behave /either/ as an ambient module /or/ as a regular importable module. In this case,
@@ -617,7 +644,7 @@ export class ExportAnalyzer {
617644

618645
// Match: "@microsoft/sp-lodash-subset" or "lodash/has"
619646
// but ignore: "../folder/LocalFile"
620-
if (!ts.isExternalModuleNameRelative(moduleSpecifier)) {
647+
if (this._isExternalModulePath(moduleSpecifier)) {
621648
return moduleSpecifier;
622649
}
623650

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ export class Collector {
113113
this.typeChecker = options.program.getTypeChecker();
114114

115115
this._tsdocParser = new tsdoc.TSDocParser(AedocDefinitions.tsdocConfiguration);
116+
117+
const bundledPackageNames: Set<string> = new Set<string>(this.extractorConfig.bundledPackages);
118+
116119
this.astSymbolTable = new AstSymbolTable(this.program, this.typeChecker, this.packageJsonLookup,
117-
this.messageRouter);
120+
bundledPackageNames, this.messageRouter);
118121
this.astReferenceResolver = new AstReferenceResolver(this.astSymbolTable, this.workingPackage);
119122
}
120123

0 commit comments

Comments
 (0)
X Tutup