Skip to content

Commit 429c22a

Browse files
committed
refactor: Update ApiReportGenerator to support a minimum release level
1 parent caab398 commit 429c22a

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

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

+46-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import type { AstModuleExportInfo } from '../analyzer/AstModule';
2222
import { SourceFileLocationFormatter } from '../analyzer/SourceFileLocationFormatter';
2323
import { ExtractorMessageId } from '../api/ExtractorMessageId';
2424

25+
/**
26+
* Options for {@link ApiReportGenerator.generateReviewFileContent}.
27+
*/
28+
export interface IApiReportOptions {
29+
/**
30+
* The release level with which the report is associated.
31+
* Can also be viewed as the minimal release level of items that should be included in the report.
32+
*
33+
* @defaultValue Include everything. I.e. {@link ReleaseTag.Internal}.
34+
*/
35+
readonly releaseLevel?: Omit<ReleaseTag, ReleaseTag.None>;
36+
}
37+
2538
export class ApiReportGenerator {
2639
private static _trimSpacesRegExp: RegExp = / +$/gm;
2740

@@ -42,10 +55,12 @@ export class ApiReportGenerator {
4255
return normalizedActual === normalizedExpected;
4356
}
4457

45-
public static generateReviewFileContent(collector: Collector): string {
58+
public static generateReviewFileContent(collector: Collector, options?: IApiReportOptions): string {
4659
const writer: IndentedWriter = new IndentedWriter();
4760
writer.trimLeadingSpaces = true;
4861

62+
const releaseLevel: ReleaseTag = (options?.releaseLevel as ReleaseTag) ?? ReleaseTag.Internal;
63+
4964
writer.writeLine(
5065
[
5166
`## API Report File for "${collector.workingPackage.name}"`,
@@ -128,7 +143,7 @@ export class ApiReportGenerator {
128143
if (apiItemMetadata.isPreapproved) {
129144
ApiReportGenerator._modifySpanForPreapproved(span);
130145
} else {
131-
ApiReportGenerator._modifySpan(collector, span, entity, astDeclaration, false);
146+
ApiReportGenerator._modifySpan(collector, span, entity, astDeclaration, false, releaseLevel);
132147
}
133148

134149
span.writeModifiedText(writer);
@@ -255,11 +270,12 @@ export class ApiReportGenerator {
255270
span: Span,
256271
entity: CollectorEntity,
257272
astDeclaration: AstDeclaration,
258-
insideTypeLiteral: boolean
273+
insideTypeLiteral: boolean,
274+
releaseLevel: ReleaseTag
259275
): void {
260276
// Should we process this declaration at all?
261277
// eslint-disable-next-line no-bitwise
262-
if (!ApiReportGenerator._shouldIncludeInReport(astDeclaration)) {
278+
if (!ApiReportGenerator._shouldIncludeInReport(collector, astDeclaration, releaseLevel)) {
263279
span.modification.skipAll();
264280
return;
265281
}
@@ -385,7 +401,8 @@ export class ApiReportGenerator {
385401
childSpan,
386402
entity,
387403
childAstDeclaration,
388-
insideTypeLiteral
404+
insideTypeLiteral,
405+
releaseLevel
389406
);
390407
}
391408
);
@@ -402,7 +419,7 @@ export class ApiReportGenerator {
402419
astDeclaration
403420
);
404421

405-
if (ApiReportGenerator._shouldIncludeInReport(childAstDeclaration)) {
422+
if (ApiReportGenerator._shouldIncludeInReport(collector, childAstDeclaration, releaseLevel)) {
406423
if (sortChildren) {
407424
span.modification.sortChildren = true;
408425
child.modification.sortKey = Collector.getSortKeyIgnoringUnderscore(
@@ -426,15 +443,35 @@ export class ApiReportGenerator {
426443
}
427444
}
428445

429-
ApiReportGenerator._modifySpan(collector, child, entity, childAstDeclaration, insideTypeLiteral);
446+
ApiReportGenerator._modifySpan(
447+
collector,
448+
child,
449+
entity,
450+
childAstDeclaration,
451+
insideTypeLiteral,
452+
releaseLevel
453+
);
430454
}
431455
}
432456
}
433457

434-
private static _shouldIncludeInReport(astDeclaration: AstDeclaration): boolean {
458+
private static _shouldIncludeInReport(
459+
collector: Collector,
460+
astDeclaration: AstDeclaration,
461+
releaseLevel: ReleaseTag
462+
): boolean {
435463
// Private declarations are not included in the API report
436464
// eslint-disable-next-line no-bitwise
437-
return (astDeclaration.modifierFlags & ts.ModifierFlags.Private) === 0;
465+
if ((astDeclaration.modifierFlags & ts.ModifierFlags.Private) !== 0) {
466+
return false;
467+
}
468+
469+
const apiItemMetadata: ApiItemMetadata = collector.fetchApiItemMetadata(astDeclaration);
470+
const releaseTag: ReleaseTag = apiItemMetadata.effectiveReleaseTag;
471+
472+
// If the declaration has a release tag that is not in scope, omit it from the report.
473+
// No release tag is considered the same as `@public`.
474+
return releaseTag === ReleaseTag.Public || releaseTag >= releaseLevel;
438475
}
439476

440477
/**

build-tests/api-extractor-test-02/etc/api-extractor-test-02.api.md

-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ export class SubclassWithImport extends RenamedReexportedClass3 implements ISimp
3838
test(): void;
3939
}
4040

41-
4241
```

build-tests/api-extractor-test-04/etc/api-extractor-test-04.api.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ export namespace EntangledNamespace {
6161
export type ExportedAlias = AlphaClass;
6262

6363
// Warning: (ae-internal-missing-underscore) The name "InternalClass" should be prefixed with an underscore because the declaration is marked as @internal
64-
//
64+
//
6565
// @internal
6666
export class InternalClass {
6767
undecoratedMember(): void;
6868
}
6969

7070
// Warning: (ae-internal-missing-underscore) The name "IPublicClassInternalParameters" should be prefixed with an underscore because the declaration is marked as @internal
71-
//
71+
//
7272
// @internal
7373
export interface IPublicClassInternalParameters {
7474
}
@@ -110,5 +110,4 @@ export enum RegularEnum {
110110
// @beta
111111
export const variableDeclaration: string;
112112

113-
114113
```

build-tests/heft-node-everything-test/etc/heft-node-everything-test.api.md

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
export class TestClass {
99
}
1010

11-
1211
// (No @packageDocumentation comment for this package)
1312

1413
```

0 commit comments

Comments
 (0)