Skip to content

Commit 096d953

Browse files
committed
Implemented member trimming
1 parent bc5de2e commit 096d953

5 files changed

Lines changed: 69 additions & 112 deletions

File tree

‎apps/api-extractor/src/generators/packageTypings/PackageTypingsGenerator.ts‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import { Text } from '@microsoft/node-core-library';
1010
import { ExtractorContext } from '../../ExtractorContext';
1111
import { IndentedWriter } from '../../utils/IndentedWriter';
1212
import { TypeScriptHelpers } from '../../utils/TypeScriptHelpers';
13-
import { Span } from '../../utils/Span';
13+
import { Span, SpanModification } from '../../utils/Span';
1414
import { ReleaseTag } from '../../aedoc/ReleaseTag';
1515
import { AstSymbolTable } from './AstSymbolTable';
1616
import { AstEntryPoint } from './AstEntryPoint';
1717
import { AstSymbol } from './AstSymbol';
1818
import { AstImport } from './AstImport';
1919
import { DtsEntry } from './DtsEntry';
2020
import { AstDeclaration } from './AstDeclaration';
21+
import { SymbolAnalyzer } from './SymbolAnalyzer';
2122

2223
/**
2324
* Used with PackageTypingsGenerator.writeTypingsFile()
@@ -255,12 +256,12 @@ export class PackageTypingsGenerator {
255256
indentedWriter.writeLine();
256257

257258
const span: Span = new Span(astDeclaration.declaration);
258-
this._modifySpan(span, dtsEntry);
259+
this._modifySpan(span, dtsEntry, astDeclaration, dtsKind);
259260
indentedWriter.writeLine(span.getModifiedText());
260261
}
261262
} else {
262263
indentedWriter.writeLine();
263-
indentedWriter.writeLine(`// Removed for this release type: ${dtsEntry.nameForEmit}`);
264+
indentedWriter.writeLine(`/* Excluded from this release type: ${dtsEntry.nameForEmit} */`);
264265
}
265266
}
266267
}
@@ -269,11 +270,12 @@ export class PackageTypingsGenerator {
269270
/**
270271
* Before writing out a declaration, _modifySpan() applies various fixups to make it nice.
271272
*/
272-
private _modifySpan(span: Span, dtsEntry: DtsEntry): void {
273+
private _modifySpan(span: Span, dtsEntry: DtsEntry, astDeclaration: AstDeclaration,
274+
dtsKind: PackageTypingsDtsKind): void {
275+
273276
const previousSpan: Span | undefined = span.previousSibling;
274277

275278
let recurseChildren: boolean = true;
276-
277279
switch (span.kind) {
278280
case ts.SyntaxKind.JSDocComment:
279281
// For now, we don't transform JSDoc comment nodes at all
@@ -364,7 +366,37 @@ export class PackageTypingsGenerator {
364366

365367
if (recurseChildren) {
366368
for (const child of span.children) {
367-
this._modifySpan(child, dtsEntry);
369+
let childAstDeclaration: AstDeclaration = astDeclaration;
370+
371+
// Should we trim this node?
372+
let trimmed: boolean = false;
373+
if (SymbolAnalyzer.isAstDeclaration(child.kind)) {
374+
childAstDeclaration = this._astSymbolTable.getChildAstDeclarationByNode(child.node, astDeclaration);
375+
376+
const releaseTag: ReleaseTag = this._getReleaseTagForAstSymbol(childAstDeclaration.astSymbol);
377+
if (!this._shouldIncludeReleaseTag(releaseTag, dtsKind)) {
378+
const modification: SpanModification = child.modification;
379+
380+
// Yes, trim it and stop here
381+
const name: string = childAstDeclaration.astSymbol.localName;
382+
modification.omitChildren = true;
383+
384+
modification.prefix = `/* Excluded from this release type: ${name} */`;
385+
modification.suffix = '';
386+
387+
if (child.children.length > 0) {
388+
// If there are grandchildren, then keep the last grandchild's separator,
389+
// since it often has useful whitespace
390+
modification.suffix = child.children[child.children.length - 1].separator;
391+
}
392+
393+
trimmed = true;
394+
}
395+
}
396+
397+
if (!trimmed) {
398+
this._modifySpan(child, dtsEntry, childAstDeclaration, dtsKind);
399+
}
368400
}
369401
}
370402
}
@@ -397,7 +429,10 @@ export class PackageTypingsGenerator {
397429
const declarationReleaseTag: ReleaseTag = this._getReleaseTagForDeclaration(astDeclaration.declaration);
398430
if (releaseTag !== ReleaseTag.None && declarationReleaseTag !== releaseTag) {
399431
// this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
432+
break;
400433
}
434+
435+
releaseTag = declarationReleaseTag;
401436
}
402437

403438
if (releaseTag !== ReleaseTag.None) {

‎build-tests/api-extractor-test-01/dist/index-preview.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export declare class ReexportedClass {
188188
getValue(): string;
189189
}
190190

191-
// Removed for this release type: _TypeReferencesInAedoc
191+
/* Excluded from this release type: _TypeReferencesInAedoc */
192192

193193
/**
194194
* Example decorator

‎build-tests/api-extractor-test-01/dist/index-public.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export declare class ForgottenExportConsumer2 {
120120
test2(): IForgottenExport_2 | undefined;
121121
}
122122

123-
// Removed for this release type: ForgottenExportConsumer3
123+
/* Excluded from this release type: ForgottenExportConsumer3 */
124124

125125
/**
126126
* This class is directly consumed by ForgottenExportConsumer3.
@@ -181,7 +181,7 @@ export declare class ReexportedClass {
181181
getValue(): string;
182182
}
183183

184-
// Removed for this release type: _TypeReferencesInAedoc
184+
/* Excluded from this release type: _TypeReferencesInAedoc */
185185

186186
/**
187187
* Example decorator

‎build-tests/api-extractor-test-04/dist/index-preview.d.ts‎

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99

10-
// Removed for this release type: AlphaClass
10+
/* Excluded from this release type: AlphaClass */
1111

1212
/**
1313
* This is a beta class
@@ -18,16 +18,8 @@ export declare class BetaClass implements BetaInterface {
1818
* This is a comment
1919
*/
2020
undecoratedMember(): void;
21-
/**
22-
* This is an alpha comment
23-
* @alpha
24-
*/
25-
alphaMember(): void;
26-
/**
27-
* This is an internal member
28-
* @internal
29-
*/
30-
_internalMember(): void;
21+
/* Excluded from this release type: alphaMember */
22+
/* Excluded from this release type: _internalMember */
3123
}
3224

3325
/**
@@ -39,16 +31,8 @@ export declare interface BetaInterface {
3931
* This is a comment
4032
*/
4133
undecoratedMember(): void;
42-
/**
43-
* This is an alpha comment
44-
* @alpha
45-
*/
46-
alphaMember(): void;
47-
/**
48-
* This is an internal member
49-
* @internal
50-
*/
51-
_internalMember(): void;
34+
/* Excluded from this release type: alphaMember */
35+
/* Excluded from this release type: _internalMember */
5236
}
5337

5438
/**
@@ -60,16 +44,8 @@ export declare const enum ConstEnum {
6044
* This member inherits its \@beta status from the parent
6145
*/
6246
BetaMember2 = "BetaMember2",
63-
/**
64-
* This member is marked as \@alpha
65-
* @alpha
66-
*/
67-
AlphaMember = "AlphaMember",
68-
/**
69-
* This member is marked as \@internal
70-
* @internal
71-
*/
72-
_InternalMember = "_InternalMember",
47+
/* Excluded from this release type: AlphaMember */,
48+
/* Excluded from this release type: _InternalMember */,
7349
}
7450

7551
/**
@@ -82,36 +58,14 @@ export declare namespace EntangledNamespace {
8258
* The "beta" release tag is inherited from the parent.
8359
*/
8460
export declare namespace N2 {
85-
/**
86-
* This class is in a nested namespace.
87-
* @alpha
88-
*/
89-
export declare class ClassX {
90-
/**
91-
* The "alpha" release tag is inherited from the parent.
92-
*/
93-
static a: string;
94-
}
61+
/* Excluded from this release type: ClassX */
9562
}
9663
/**
9764
* This is a nested namespace.
9865
* The "beta" release tag is inherited from the parent.
9966
*/
10067
export declare namespace N3 {
101-
/**
102-
* This class is in a nested namespace.
103-
* @internal
104-
*/
105-
export declare class _ClassY {
106-
/**
107-
* This definition refers to a "alpha" namespaced class.
108-
*/
109-
b: EntangledNamespace.N2.ClassX;
110-
/**
111-
* This definition refers to the type of a "alpha" namespaced member.
112-
*/
113-
c(): typeof N2.ClassX.a;
114-
}
68+
/* Excluded from this release type: _ClassY */
11569
}
11670
}
11771

@@ -120,7 +74,7 @@ export declare namespace EntangledNamespace {
12074
*/
12175
export declare type ExportedAlias = AlphaClass;
12276

123-
// Removed for this release type: InternalClass
77+
/* Excluded from this release type: InternalClass */
12478

12579
/**
12680
* This is a public class
@@ -141,16 +95,8 @@ export declare class PublicClass {
14195
* @beta
14296
*/
14397
betaMember(): void;
144-
/**
145-
* This is an alpha comment
146-
* @alpha
147-
*/
148-
alphaMember(): void;
149-
/**
150-
* This is an internal member
151-
* @internal
152-
*/
153-
_internalMember(): void;
98+
/* Excluded from this release type: alphaMember */
99+
/* Excluded from this release type: _internalMember */
154100
}
155101

156102
/**
@@ -162,16 +108,8 @@ export declare enum RegularEnum {
162108
* This member inherits its \@beta status from the parent
163109
*/
164110
BetaMember = 100,
165-
/**
166-
* This member is marked as \@alpha
167-
* @alpha
168-
*/
169-
AlphaMember = 101,
170-
/**
171-
* This member is marked as \@internal
172-
* @internal
173-
*/
174-
_InternalMember = 102,
111+
/* Excluded from this release type: AlphaMember */,
112+
/* Excluded from this release type: _InternalMember */,
175113
}
176114

177115
declare const variableDeclaration: string;

‎build-tests/api-extractor-test-04/dist/index-public.d.ts‎

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,38 @@
77
*/
88

99

10-
// Removed for this release type: AlphaClass
10+
/* Excluded from this release type: AlphaClass */
1111

12-
// Removed for this release type: BetaClass
12+
/* Excluded from this release type: BetaClass */
1313

14-
// Removed for this release type: BetaInterface
14+
/* Excluded from this release type: BetaInterface */
1515

16-
// Removed for this release type: ConstEnum
16+
/* Excluded from this release type: ConstEnum */
1717

18-
// Removed for this release type: EntangledNamespace
18+
/* Excluded from this release type: EntangledNamespace */
1919

2020
/**
2121
* This is an exported type alias.
2222
*/
2323
export declare type ExportedAlias = AlphaClass;
2424

25-
// Removed for this release type: InternalClass
25+
/* Excluded from this release type: InternalClass */
2626

2727
/**
2828
* This is a public class
2929
* @public
3030
*/
3131
export declare class PublicClass {
32-
/**
33-
* This is a beta field
34-
* @beta
35-
*/
36-
betaField: string;
32+
/* Excluded from this release type: betaField */
3733
/**
3834
* This is a comment
3935
*/
4036
undecoratedMember(): void;
41-
/**
42-
* This is a beta comment
43-
* @beta
44-
*/
45-
betaMember(): void;
46-
/**
47-
* This is an alpha comment
48-
* @alpha
49-
*/
50-
alphaMember(): void;
51-
/**
52-
* This is an internal member
53-
* @internal
54-
*/
55-
_internalMember(): void;
37+
/* Excluded from this release type: betaMember */
38+
/* Excluded from this release type: alphaMember */
39+
/* Excluded from this release type: _internalMember */
5640
}
5741

58-
// Removed for this release type: RegularEnum
42+
/* Excluded from this release type: RegularEnum */
5943

6044
declare const variableDeclaration: string;

0 commit comments

Comments
 (0)