@@ -10,14 +10,15 @@ import { Text } from '@microsoft/node-core-library';
1010import { ExtractorContext } from '../../ExtractorContext' ;
1111import { IndentedWriter } from '../../utils/IndentedWriter' ;
1212import { TypeScriptHelpers } from '../../utils/TypeScriptHelpers' ;
13- import { Span } from '../../utils/Span' ;
13+ import { Span , SpanModification } from '../../utils/Span' ;
1414import { ReleaseTag } from '../../aedoc/ReleaseTag' ;
1515import { AstSymbolTable } from './AstSymbolTable' ;
1616import { AstEntryPoint } from './AstEntryPoint' ;
1717import { AstSymbol } from './AstSymbol' ;
1818import { AstImport } from './AstImport' ;
1919import { DtsEntry } from './DtsEntry' ;
2020import { AstDeclaration } from './AstDeclaration' ;
21+ import { SymbolAnalyzer } from './SymbolAnalyzer' ;
2122
2223/**
2324 * Used with PackageTypingsGenerator.writeTypingsFile()
@@ -53,6 +54,7 @@ export class PackageTypingsGenerator {
5354 private _dtsEntries : DtsEntry [ ] = [ ] ;
5455 private _dtsEntriesByAstSymbol : Map < AstSymbol , DtsEntry > = new Map < AstSymbol , DtsEntry > ( ) ;
5556 private _dtsEntriesBySymbol : Map < ts . Symbol , DtsEntry > = new Map < ts . Symbol , DtsEntry > ( ) ;
57+ private _releaseTagByAstSymbol : Map < AstSymbol , ReleaseTag > = new Map < AstSymbol , ReleaseTag > ( ) ;
5658
5759 /**
5860 * A list of names (e.g. "example-library") that should appear in a reference like this:
@@ -131,13 +133,10 @@ export class PackageTypingsGenerator {
131133 let dtsEntry : DtsEntry | undefined = this . _dtsEntriesByAstSymbol . get ( astSymbol ) ;
132134
133135 if ( ! dtsEntry ) {
134- const releaseTag : ReleaseTag = this . _getReleaseTagForSymbol ( astSymbol . followedSymbol ) ;
135-
136136 dtsEntry = new DtsEntry ( {
137137 astSymbol : astSymbol ,
138138 originalName : exportedName || astSymbol . localName ,
139- exported : ! ! exportedName ,
140- releaseTag : releaseTag
139+ exported : ! ! exportedName
141140 } ) ;
142141
143142 this . _dtsEntriesByAstSymbol . set ( astSymbol , dtsEntry ) ;
@@ -247,20 +246,22 @@ export class PackageTypingsGenerator {
247246 for ( const dtsEntry of this . _dtsEntries ) {
248247 if ( ! dtsEntry . astSymbol . astImport ) {
249248
250- if ( this . _shouldIncludeReleaseTag ( dtsEntry . releaseTag , dtsKind ) ) {
249+ const releaseTag : ReleaseTag = this . _getReleaseTagForAstSymbol ( dtsEntry . astSymbol ) ;
250+
251+ if ( this . _shouldIncludeReleaseTag ( releaseTag , dtsKind ) ) {
251252
252253 // Emit all the declarations for this entry
253254 for ( const astDeclaration of dtsEntry . astSymbol . astDeclarations || [ ] ) {
254255
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
@@ -295,7 +297,12 @@ export class PackageTypingsGenerator {
295297 case ts . SyntaxKind . TypeKeyword :
296298 case ts . SyntaxKind . FunctionKeyword :
297299 // Replace the stuff we possibly deleted above
298- let replacedModifiers : string = 'declare ' ;
300+ let replacedModifiers : string = '' ;
301+
302+ // Add a declare statement for root declarations (but not for nested declarations)
303+ if ( ! astDeclaration . parent ) {
304+ replacedModifiers += 'declare ' ;
305+ }
299306
300307 if ( dtsEntry . exported ) {
301308 replacedModifiers = 'export ' + replacedModifiers ;
@@ -364,7 +371,47 @@ export class PackageTypingsGenerator {
364371
365372 if ( recurseChildren ) {
366373 for ( const child of span . children ) {
367- this . _modifySpan ( child , dtsEntry ) ;
374+ let childAstDeclaration : AstDeclaration = astDeclaration ;
375+
376+ // Should we trim this node?
377+ let trimmed : boolean = false ;
378+ if ( SymbolAnalyzer . isAstDeclaration ( child . kind ) ) {
379+ childAstDeclaration = this . _astSymbolTable . getChildAstDeclarationByNode ( child . node , astDeclaration ) ;
380+
381+ const releaseTag : ReleaseTag = this . _getReleaseTagForAstSymbol ( childAstDeclaration . astSymbol ) ;
382+ if ( ! this . _shouldIncludeReleaseTag ( releaseTag , dtsKind ) ) {
383+ const modification : SpanModification = child . modification ;
384+
385+ // Yes, trim it and stop here
386+ const name : string = childAstDeclaration . astSymbol . localName ;
387+ modification . omitChildren = true ;
388+
389+ modification . prefix = `/* Excluded from this release type: ${ name } */` ;
390+ modification . suffix = '' ;
391+
392+ if ( child . children . length > 0 ) {
393+ // If there are grandchildren, then keep the last grandchild's separator,
394+ // since it often has useful whitespace
395+ modification . suffix = child . children [ child . children . length - 1 ] . separator ;
396+ }
397+
398+ if ( child . nextSibling ) {
399+ // If the thing we are trimming is followed by a comma, then trim the comma also.
400+ // An example would be an enum member.
401+ if ( child . nextSibling . kind === ts . SyntaxKind . CommaToken ) {
402+ // Keep its separator since it often has useful whitespace
403+ modification . suffix += child . nextSibling . separator ;
404+ child . nextSibling . modification . skipAll ( ) ;
405+ }
406+ }
407+
408+ trimmed = true ;
409+ }
410+ }
411+
412+ if ( ! trimmed ) {
413+ this . _modifySpan ( child , dtsEntry , childAstDeclaration , dtsKind ) ;
414+ }
368415 }
369416 }
370417 }
@@ -383,46 +430,78 @@ export class PackageTypingsGenerator {
383430 throw new Error ( `PackageTypingsDtsKind[dtsKind] is not implemented` ) ;
384431 }
385432
433+ private _getReleaseTagForAstSymbol ( astSymbol : AstSymbol ) : ReleaseTag {
434+ let releaseTag : ReleaseTag | undefined = this . _releaseTagByAstSymbol . get ( astSymbol ) ;
435+ if ( releaseTag ) {
436+ return releaseTag ;
437+ }
438+
439+ releaseTag = ReleaseTag . None ;
440+
441+ let current : AstSymbol | undefined = astSymbol ;
442+ while ( current ) {
443+ for ( const astDeclaration of current . astDeclarations ) {
444+ const declarationReleaseTag : ReleaseTag = this . _getReleaseTagForDeclaration ( astDeclaration . declaration ) ;
445+ if ( releaseTag !== ReleaseTag . None && declarationReleaseTag !== releaseTag ) {
446+ // this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
447+ break ;
448+ }
449+
450+ releaseTag = declarationReleaseTag ;
451+ }
452+
453+ if ( releaseTag !== ReleaseTag . None ) {
454+ break ;
455+ }
456+
457+ current = current . parentAstSymbol ;
458+ }
459+
460+ if ( releaseTag === ReleaseTag . None ) {
461+ releaseTag = ReleaseTag . Public ; // public by default
462+ }
463+
464+ this . _releaseTagByAstSymbol . set ( astSymbol , releaseTag ) ;
465+
466+ return releaseTag ;
467+ }
468+
386469 // NOTE: THIS IS A TEMPORARY WORKAROUND.
387470 // In the near future we will overhaul the AEDoc parser to separate syntactic/semantic analysis,
388471 // 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-
472+ private _getReleaseTagForDeclaration ( declaration : ts . Node ) : ReleaseTag {
392473 let releaseTag : ReleaseTag = ReleaseTag . None ;
393474
394475 // We don't want to match "bill@example.com". But we do want to match "/**@public*/".
395476 // So for now we require whitespace or a star before/after the string.
396477 const releaseTagRegExp : RegExp = / (?: \s | \* ) @ ( i n t e r n a l | a l p h a | b e t a | p u b l i c ) (?: \s | \* ) / g;
397478
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- }
479+ const sourceFileText : string = declaration . getSourceFile ( ) . text ;
480+
481+ for ( const commentRange of TypeScriptHelpers . getJSDocCommentRanges ( declaration , sourceFileText ) || [ ] ) {
482+ // NOTE: This string includes "/**"
483+ const comment : string = sourceFileText . substring ( commentRange . pos , commentRange . end ) ;
484+
485+ let match : RegExpMatchArray | null ;
486+ while ( match = releaseTagRegExp . exec ( comment ) ) {
487+ let foundReleaseTag : ReleaseTag = ReleaseTag . None ;
488+ switch ( match [ 1 ] ) {
489+ case 'internal' :
490+ foundReleaseTag = ReleaseTag . Internal ; break ;
491+ case 'alpha' :
492+ foundReleaseTag = ReleaseTag . Alpha ; break ;
493+ case 'beta' :
494+ foundReleaseTag = ReleaseTag . Beta ; break ;
495+ case 'public' :
496+ foundReleaseTag = ReleaseTag . Public ; break ;
497+ }
423498
424- releaseTag = foundReleaseTag ;
499+ if ( releaseTag !== ReleaseTag . None && foundReleaseTag !== releaseTag ) {
500+ // this._analyzeWarnings.push('WARNING: Conflicting release tags found for ' + symbol.name);
501+ return releaseTag ;
425502 }
503+
504+ releaseTag = foundReleaseTag ;
426505 }
427506 }
428507
0 commit comments