Skip to content

Commit bc5de2e

Browse files
committed
_getReleaseTagForAstSymbol() now correctly models inheritance and siblings
For example: ``` interface IMerged { // This is beta because of the merged parent below x: string; } /** @beta */ interface IMerged { } ```
1 parent 4842d88 commit bc5de2e

2 files changed

Lines changed: 63 additions & 42 deletions

File tree

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

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

44
import { AstSymbol } from './AstSymbol';
5-
import { ReleaseTag } from '../../aedoc/ReleaseTag';
65

76
/**
87
* Constructor parameters for DtsEntry
@@ -11,7 +10,6 @@ export interface IDtsEntryParameters {
1110
readonly astSymbol: AstSymbol;
1211
readonly originalName: string;
1312
readonly exported: boolean;
14-
readonly releaseTag: ReleaseTag;
1513
}
1614

1715
/**
@@ -39,11 +37,6 @@ export class DtsEntry {
3937
*/
4038
public readonly exported: boolean;
4139

42-
/**
43-
* The ReleaseTag used for trimming
44-
*/
45-
public readonly releaseTag: ReleaseTag;
46-
4740
private _nameForEmit: string | undefined = undefined;
4841

4942
private _sortKey: string | undefined = undefined;
@@ -52,7 +45,6 @@ export class DtsEntry {
5245
this.astSymbol = parameters.astSymbol;
5346
this.originalName = parameters.originalName;
5447
this.exported = parameters.exported;
55-
this.releaseTag = parameters.releaseTag;
5648
}
5749

5850
/**

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

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class PackageTypingsGenerator {
5353
private _dtsEntries: DtsEntry[] = [];
5454
private _dtsEntriesByAstSymbol: Map<AstSymbol, DtsEntry> = new Map<AstSymbol, DtsEntry>();
5555
private _dtsEntriesBySymbol: Map<ts.Symbol, DtsEntry> = new Map<ts.Symbol, DtsEntry>();
56+
private _releaseTagByAstSymbol: Map<AstSymbol, ReleaseTag> = new Map<AstSymbol, ReleaseTag>();
5657

5758
/**
5859
* A list of names (e.g. "example-library") that should appear in a reference like this:
@@ -131,13 +132,10 @@ export class PackageTypingsGenerator {
131132
let dtsEntry: DtsEntry | undefined = this._dtsEntriesByAstSymbol.get(astSymbol);
132133

133134
if (!dtsEntry) {
134-
const releaseTag: ReleaseTag = this._getReleaseTagForSymbol(astSymbol.followedSymbol);
135-
136135
dtsEntry = new DtsEntry({
137136
astSymbol: astSymbol,
138137
originalName: exportedName || astSymbol.localName,
139-
exported: !!exportedName,
140-
releaseTag: releaseTag
138+
exported: !!exportedName
141139
});
142140

143141
this._dtsEntriesByAstSymbol.set(astSymbol, dtsEntry);
@@ -247,7 +245,9 @@ export class PackageTypingsGenerator {
247245
for (const dtsEntry of this._dtsEntries) {
248246
if (!dtsEntry.astSymbol.astImport) {
249247

250-
if (this._shouldIncludeReleaseTag(dtsEntry.releaseTag, dtsKind)) {
248+
const releaseTag: ReleaseTag = this._getReleaseTagForAstSymbol(dtsEntry.astSymbol);
249+
250+
if (this._shouldIncludeReleaseTag(releaseTag, dtsKind)) {
251251

252252
// Emit all the declarations for this entry
253253
for (const astDeclaration of dtsEntry.astSymbol.astDeclarations || []) {
@@ -383,46 +383,75 @@ export class PackageTypingsGenerator {
383383
throw new Error(`PackageTypingsDtsKind[dtsKind] is not implemented`);
384384
}
385385

386+
private _getReleaseTagForAstSymbol(astSymbol: AstSymbol): ReleaseTag {
387+
let releaseTag: ReleaseTag | undefined = this._releaseTagByAstSymbol.get(astSymbol);
388+
if (releaseTag) {
389+
return releaseTag;
390+
}
391+
392+
releaseTag = ReleaseTag.None;
393+
394+
let current: AstSymbol | undefined = astSymbol;
395+
while (current) {
396+
for (const astDeclaration of current.astDeclarations) {
397+
const declarationReleaseTag: ReleaseTag = this._getReleaseTagForDeclaration(astDeclaration.declaration);
398+
if (releaseTag !== ReleaseTag.None && declarationReleaseTag !== releaseTag) {
399+
// this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
400+
}
401+
}
402+
403+
if (releaseTag !== ReleaseTag.None) {
404+
break;
405+
}
406+
407+
current = current.parentAstSymbol;
408+
}
409+
410+
if (releaseTag === ReleaseTag.None) {
411+
releaseTag = ReleaseTag.Public; // public by default
412+
}
413+
414+
this._releaseTagByAstSymbol.set(astSymbol, releaseTag);
415+
416+
return releaseTag;
417+
}
418+
386419
// NOTE: THIS IS A TEMPORARY WORKAROUND.
387420
// In the near future we will overhaul the AEDoc parser to separate syntactic/semantic analysis,
388421
// at which point this will be wired up to the same ApiDocumentation layer used for the API Review files
389-
private _getReleaseTagForSymbol(symbol: ts.Symbol): ReleaseTag {
390-
const fullyFollowedSymbol: ts.Symbol = TypeScriptHelpers.followAliases(symbol, this._typeChecker);
391-
422+
private _getReleaseTagForDeclaration(declaration: ts.Node): ReleaseTag {
392423
let releaseTag: ReleaseTag = ReleaseTag.None;
393424

394425
// We don't want to match "bill@example.com". But we do want to match "/**@public*/".
395426
// So for now we require whitespace or a star before/after the string.
396427
const releaseTagRegExp: RegExp = /(?:\s|\*)@(internal|alpha|beta|public)(?:\s|\*)/g;
397428

398-
for (const declaration of fullyFollowedSymbol.declarations || []) {
399-
const sourceFileText: string = declaration.getSourceFile().text;
400-
401-
for (const commentRange of TypeScriptHelpers.getJSDocCommentRanges(declaration, sourceFileText) || []) {
402-
// NOTE: This string includes "/**"
403-
const comment: string = sourceFileText.substring(commentRange.pos, commentRange.end);
404-
405-
let match: RegExpMatchArray | null;
406-
while (match = releaseTagRegExp.exec(comment)) {
407-
let foundReleaseTag: ReleaseTag = ReleaseTag.None;
408-
switch (match[1]) {
409-
case 'internal':
410-
foundReleaseTag = ReleaseTag.Internal; break;
411-
case 'alpha':
412-
foundReleaseTag = ReleaseTag.Alpha; break;
413-
case 'beta':
414-
foundReleaseTag = ReleaseTag.Beta; break;
415-
case 'public':
416-
foundReleaseTag = ReleaseTag.Public; break;
417-
}
418-
419-
if (releaseTag !== ReleaseTag.None && foundReleaseTag !== releaseTag) {
420-
// this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
421-
return releaseTag;
422-
}
429+
const sourceFileText: string = declaration.getSourceFile().text;
430+
431+
for (const commentRange of TypeScriptHelpers.getJSDocCommentRanges(declaration, sourceFileText) || []) {
432+
// NOTE: This string includes "/**"
433+
const comment: string = sourceFileText.substring(commentRange.pos, commentRange.end);
434+
435+
let match: RegExpMatchArray | null;
436+
while (match = releaseTagRegExp.exec(comment)) {
437+
let foundReleaseTag: ReleaseTag = ReleaseTag.None;
438+
switch (match[1]) {
439+
case 'internal':
440+
foundReleaseTag = ReleaseTag.Internal; break;
441+
case 'alpha':
442+
foundReleaseTag = ReleaseTag.Alpha; break;
443+
case 'beta':
444+
foundReleaseTag = ReleaseTag.Beta; break;
445+
case 'public':
446+
foundReleaseTag = ReleaseTag.Public; break;
447+
}
423448

424-
releaseTag = foundReleaseTag;
449+
if (releaseTag !== ReleaseTag.None && foundReleaseTag !== releaseTag) {
450+
// this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
451+
return releaseTag;
425452
}
453+
454+
releaseTag = foundReleaseTag;
426455
}
427456
}
428457

0 commit comments

Comments
 (0)