@@ -16,6 +16,7 @@ import { AstImport } from '../analyzer/AstImport';
1616import { AstSymbol } from '../analyzer/AstSymbol' ;
1717import { ExtractorMessage } from '../api/ExtractorMessage' ;
1818import { StringWriter } from './StringWriter' ;
19+ import { DtsEmitHelpers } from './DtsEmitHelpers' ;
1920
2021export class ReviewFileGenerator {
2122 /**
@@ -45,6 +46,20 @@ export class ReviewFileGenerator {
4546 // Write the opening delimiter for the Markdown code fence
4647 stringWriter . writeLine ( '```ts\n' ) ;
4748
49+ // Emit the imports
50+ let importsEmitted : boolean = false ;
51+ for ( const entity of collector . entities ) {
52+ if ( entity . astEntity instanceof AstImport ) {
53+ DtsEmitHelpers . emitImport ( stringWriter , entity , entity . astEntity ) ;
54+ importsEmitted = true ;
55+ }
56+ }
57+
58+ if ( importsEmitted ) {
59+ stringWriter . writeLine ( ) ;
60+ }
61+
62+ // Emit the regular declarations
4863 for ( const entity of collector . entities ) {
4964 if ( entity . exported ) {
5065 if ( entity . astEntity instanceof AstSymbol ) {
@@ -58,30 +73,18 @@ export class ReviewFileGenerator {
5873 span . writeModifiedText ( stringWriter . stringBuilder ) ;
5974 stringWriter . writeLine ( '\n' ) ;
6075 }
61- } else {
62- // This definition is reexported from another package, so write it as an "export" line
63- // In general, we don't report on external packages; if that's important we assume API Extractor
64- // would be enabled for the upstream project. But see GitHub issue #896 for a possible exception.
65- const astImport : AstImport = entity . astEntity ;
66-
67- if ( astImport . exportName === '*' ) {
68- stringWriter . write ( `export * as ${ entity . nameForEmit } ` ) ;
69- } else if ( entity . nameForEmit !== astImport . exportName ) {
70- stringWriter . write ( `export { ${ astImport . exportName } as ${ entity . nameForEmit } }` ) ;
71- } else {
72- stringWriter . write ( `export { ${ astImport . exportName } }` ) ;
76+ }
77+
78+ for ( const exportName of entity . exportNames ) {
79+ if ( ! entity . shouldInlineExport ) {
80+ DtsEmitHelpers . emitNamedExport ( stringWriter , exportName , entity ) ;
81+ stringWriter . writeLine ( ) ;
7382 }
74- stringWriter . writeLine ( ` from '${ astImport . modulePath } ';` ) ;
7583 }
7684 }
7785 }
7886
79- if ( collector . starExportedExternalModulePaths . length > 0 ) {
80- stringWriter . writeLine ( ) ;
81- for ( const starExportedExternalModulePath of collector . starExportedExternalModulePaths ) {
82- stringWriter . writeLine ( `export * from "${ starExportedExternalModulePath } ";` ) ;
83- }
84- }
87+ DtsEmitHelpers . emitStarExports ( stringWriter , collector ) ;
8588
8689 // Write the unassociated warnings at the bottom of the file
8790 const unassociatedMessages : ExtractorMessage [ ] = collector . messageRouter
@@ -120,6 +123,8 @@ export class ReviewFileGenerator {
120123 return ;
121124 }
122125
126+ const previousSpan : Span | undefined = span . previousSibling ;
127+
123128 let recurseChildren : boolean = true ;
124129 let sortChildren : boolean = false ;
125130
@@ -135,6 +140,30 @@ export class ReviewFileGenerator {
135140 span . modification . skipAll ( ) ;
136141 break ;
137142
143+ case ts . SyntaxKind . InterfaceKeyword :
144+ case ts . SyntaxKind . ClassKeyword :
145+ case ts . SyntaxKind . EnumKeyword :
146+ case ts . SyntaxKind . NamespaceKeyword :
147+ case ts . SyntaxKind . ModuleKeyword :
148+ case ts . SyntaxKind . TypeKeyword :
149+ case ts . SyntaxKind . FunctionKeyword :
150+ // Replace the stuff we possibly deleted above
151+ let replacedModifiers : string = '' ;
152+
153+ if ( entity . shouldInlineExport ) {
154+ replacedModifiers = 'export ' + replacedModifiers ;
155+ }
156+
157+ if ( previousSpan && previousSpan . kind === ts . SyntaxKind . SyntaxList ) {
158+ // If there is a previous span of type SyntaxList, then apply it before any other modifiers
159+ // (e.g. "abstract") that appear there.
160+ previousSpan . modification . prefix = replacedModifiers + previousSpan . modification . prefix ;
161+ } else {
162+ // Otherwise just stick it in front of this span
163+ span . modification . prefix = replacedModifiers + span . modification . prefix ;
164+ }
165+ break ;
166+
138167 case ts . SyntaxKind . SyntaxList :
139168 if ( span . parent ) {
140169 if ( AstDeclaration . isSupportedSyntaxKind ( span . parent . kind ) ) {
@@ -166,8 +195,11 @@ export class ReviewFileGenerator {
166195 const listPrefix : string = list . getSourceFile ( ) . text
167196 . substring ( list . getStart ( ) , list . declarations [ 0 ] . getStart ( ) ) ;
168197 span . modification . prefix = 'declare ' + listPrefix + span . modification . prefix ;
169-
170198 span . modification . suffix = ';' ;
199+
200+ if ( entity . shouldInlineExport ) {
201+ span . modification . prefix = 'export ' + span . modification . prefix ;
202+ }
171203 }
172204 break ;
173205
0 commit comments