@@ -4,13 +4,7 @@ import * as assert from "assert";
4
4
import { resolve } from "path" ;
5
5
6
6
import { Application } from "../application" ;
7
- import {
8
- Type ,
9
- ProjectReflection ,
10
- ReflectionKind ,
11
- ContainerReflection ,
12
- DeclarationReflection ,
13
- } from "../models/index" ;
7
+ import { Type , ProjectReflection , ReflectionKind } from "../models/index" ;
14
8
import { Context } from "./context" ;
15
9
import { ConverterComponent } from "./components" ;
16
10
import { Component , ChildableComponent } from "../utils/component" ;
@@ -249,34 +243,40 @@ export class Converter extends ChildableComponent<
249
243
*/
250
244
private compile ( entryPoints : readonly string [ ] , context : Context ) {
251
245
const baseDir = getCommonDirectory ( entryPoints ) ;
252
- const entries : [ string , ts . SourceFile , ts . Program ] [ ] = [ ] ;
253
-
254
- entryLoop: for ( const entry of entryPoints . map ( normalizePath ) ) {
246
+ const entries : {
247
+ file : string ;
248
+ sourceFile : ts . SourceFile ;
249
+ program : ts . Program ;
250
+ context ?: Context ;
251
+ } [ ] = [ ] ;
252
+
253
+ entryLoop: for ( const file of entryPoints . map ( normalizePath ) ) {
255
254
for ( const program of context . programs ) {
256
- const sourceFile = program . getSourceFile ( entry ) ;
255
+ const sourceFile = program . getSourceFile ( file ) ;
257
256
if ( sourceFile ) {
258
- entries . push ( [ entry , sourceFile , program ] ) ;
257
+ entries . push ( { file , sourceFile, program } ) ;
259
258
continue entryLoop;
260
259
}
261
260
}
262
261
this . application . logger . warn (
263
- `Unable to locate entry point: ${ entry } `
262
+ `Unable to locate entry point: ${ file } `
264
263
) ;
265
264
}
266
265
267
- for ( const [ entry , file , program ] of entries ) {
268
- context . setActiveProgram ( program ) ;
269
- this . convertExports (
266
+ for ( const entry of entries ) {
267
+ context . setActiveProgram ( entry . program ) ;
268
+ entry . context = this . convertExports (
270
269
context ,
271
- file ,
270
+ entry . sourceFile ,
272
271
entryPoints ,
273
- getModuleName ( resolve ( entry ) , baseDir )
272
+ getModuleName ( resolve ( entry . file ) , baseDir )
274
273
) ;
275
274
}
276
275
277
- for ( const [ , file , program ] of entries ) {
278
- context . setActiveProgram ( program ) ;
279
- this . convertReExports ( context , file ) ;
276
+ for ( const { sourceFile, context } of entries ) {
277
+ // active program is already set on context
278
+ assert ( context ) ;
279
+ this . convertReExports ( context , sourceFile ) ;
280
280
}
281
281
282
282
context . setActiveProgram ( undefined ) ;
@@ -296,49 +296,28 @@ export class Converter extends ChildableComponent<
296
296
// create modules for each entry. Register the project as this module.
297
297
context . project . registerReflection ( context . project , symbol ) ;
298
298
moduleContext = context ;
299
- } else if ( symbol ) {
299
+ } else {
300
300
const reflection = context . createDeclarationReflection (
301
301
ReflectionKind . Module ,
302
302
symbol ,
303
303
entryName
304
304
) ;
305
305
moduleContext = context . withScope ( reflection ) ;
306
- } else {
307
- this . application . logger . warn (
308
- `If specifying a global file as an entry point, only one entry point may be specified. (${ node . fileName } )`
309
- ) ;
310
- return ;
311
306
}
312
307
313
308
for ( const exp of getExports ( context , node ) . filter ( ( exp ) =>
314
- context
315
- . resolveAliasedSymbol ( exp )
316
- . getDeclarations ( )
317
- ?. every ( ( d ) => d . getSourceFile ( ) === node . getSourceFile ( ) )
309
+ isDirectExport ( context . resolveAliasedSymbol ( exp ) , node )
318
310
) ) {
319
311
convertSymbol ( moduleContext , exp ) ;
320
312
}
321
- }
322
-
323
- private convertReExports ( context : Context , node : ts . SourceFile ) {
324
- const symbol = context . checker . getSymbolAtLocation ( node ) ?? node . symbol ;
325
- // Was a global "module"... no re exports.
326
- if ( symbol == null ) return ;
327
313
328
- const moduleReflection = context . project . getReflectionFromSymbol (
329
- symbol
330
- ) ;
331
- assert (
332
- moduleReflection instanceof ContainerReflection ||
333
- moduleReflection instanceof DeclarationReflection
334
- ) ;
314
+ return moduleContext ;
315
+ }
335
316
336
- const moduleContext = context . withScope ( moduleReflection ) ;
337
- for ( const exp of getExports ( context , node ) . filter ( ( exp ) =>
338
- context
339
- . resolveAliasedSymbol ( exp )
340
- . getDeclarations ( )
341
- ?. some ( ( d ) => d . getSourceFile ( ) !== node . getSourceFile ( ) )
317
+ private convertReExports ( moduleContext : Context , node : ts . SourceFile ) {
318
+ for ( const exp of getExports ( moduleContext , node ) . filter (
319
+ ( exp ) =>
320
+ ! isDirectExport ( moduleContext . resolveAliasedSymbol ( exp ) , node )
342
321
) ) {
343
322
convertSymbol ( moduleContext , exp ) ;
344
323
}
@@ -437,10 +416,24 @@ function getExports(
437
416
// and lift that up one level
438
417
if (
439
418
globalSymbols . length === 1 &&
440
- globalSymbols [ 0 ] . getDeclarations ( ) ?. every ( ts . isModuleDeclaration )
419
+ globalSymbols [ 0 ]
420
+ . getDeclarations ( )
421
+ ?. every (
422
+ ( declaration ) =>
423
+ ts . isModuleDeclaration ( declaration ) &&
424
+ ts . isStringLiteral ( declaration . name )
425
+ )
441
426
) {
442
427
return context . checker . getExportsOfModule ( globalSymbols [ 0 ] ) ;
443
428
}
444
429
445
430
return globalSymbols ;
446
431
}
432
+
433
+ function isDirectExport ( symbol : ts . Symbol , file : ts . SourceFile ) : boolean {
434
+ return (
435
+ symbol
436
+ . getDeclarations ( )
437
+ ?. every ( ( decl ) => decl . getSourceFile ( ) === file ) ?? false
438
+ ) ;
439
+ }
0 commit comments