@@ -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
@@ -132,9 +137,35 @@ export class ReviewFileGenerator {
132137
133138 case ts . SyntaxKind . ExportKeyword :
134139 case ts . SyntaxKind . DefaultKeyword :
140+ case ts . SyntaxKind . DeclareKeyword :
141+ // Delete any explicit "export" or "declare" keywords -- we will re-add them below
135142 span . modification . skipAll ( ) ;
136143 break ;
137144
145+ case ts . SyntaxKind . InterfaceKeyword :
146+ case ts . SyntaxKind . ClassKeyword :
147+ case ts . SyntaxKind . EnumKeyword :
148+ case ts . SyntaxKind . NamespaceKeyword :
149+ case ts . SyntaxKind . ModuleKeyword :
150+ case ts . SyntaxKind . TypeKeyword :
151+ case ts . SyntaxKind . FunctionKeyword :
152+ // Replace the stuff we possibly deleted above
153+ let replacedModifiers : string = '' ;
154+
155+ if ( entity . shouldInlineExport ) {
156+ replacedModifiers = 'export ' + replacedModifiers ;
157+ }
158+
159+ if ( previousSpan && previousSpan . kind === ts . SyntaxKind . SyntaxList ) {
160+ // If there is a previous span of type SyntaxList, then apply it before any other modifiers
161+ // (e.g. "abstract") that appear there.
162+ previousSpan . modification . prefix = replacedModifiers + previousSpan . modification . prefix ;
163+ } else {
164+ // Otherwise just stick it in front of this span
165+ span . modification . prefix = replacedModifiers + span . modification . prefix ;
166+ }
167+ break ;
168+
138169 case ts . SyntaxKind . SyntaxList :
139170 if ( span . parent ) {
140171 if ( AstDeclaration . isSupportedSyntaxKind ( span . parent . kind ) ) {
@@ -165,9 +196,12 @@ export class ReviewFileGenerator {
165196 }
166197 const listPrefix : string = list . getSourceFile ( ) . text
167198 . substring ( list . getStart ( ) , list . declarations [ 0 ] . getStart ( ) ) ;
168- span . modification . prefix = 'declare ' + listPrefix + span . modification . prefix ;
169-
199+ span . modification . prefix = listPrefix + span . modification . prefix ;
170200 span . modification . suffix = ';' ;
201+
202+ if ( entity . shouldInlineExport ) {
203+ span . modification . prefix = 'export ' + span . modification . prefix ;
204+ }
171205 }
172206 break ;
173207
0 commit comments