@@ -33,8 +33,6 @@ import {
33
33
import { EnumMetadataValuesMapOptions } from '../../schema-builder/metadata' ;
34
34
import { EnumOptions } from '../../type-factories' ;
35
35
36
- const importsToAddPerFile = new Map < string , Set < string > > ( ) ;
37
-
38
36
const ALLOWED_DECORATORS = [
39
37
ObjectType . name ,
40
38
InterfaceType . name ,
@@ -52,6 +50,8 @@ function capitalizeFirstLetter(word: string) {
52
50
}
53
51
54
52
export class ModelClassVisitor {
53
+ importsToAdd : Set < string > ;
54
+
55
55
inlineEnumsMap : { name : string ; values : { [ name : string ] : string } } [ ] ;
56
56
enumsMetadata : Map < ts . EnumDeclaration , EnumMetadata > ;
57
57
packageVarIdentifier : ts . Identifier ;
@@ -65,7 +65,7 @@ export class ModelClassVisitor {
65
65
) {
66
66
this . inlineEnumsMap = [ ] ;
67
67
this . enumsMetadata = new Map ( ) ;
68
-
68
+ this . importsToAdd = new Set < string > ( ) ;
69
69
this . isCommonJs = ctx . getCompilerOptions ( ) . module === ModuleKind . CommonJS ;
70
70
71
71
const typeChecker = program . getTypeChecker ( ) ;
@@ -113,10 +113,8 @@ export class ModelClassVisitor {
113
113
} else if ( ts . isSourceFile ( node ) ) {
114
114
const visitedNode = ts . visitEachChild ( node , visitNode , ctx ) ;
115
115
116
- const importStatements : ts . Statement [ ] = this . createEagerImports (
117
- factory ,
118
- node . fileName ,
119
- ) ;
116
+ const importStatements : ts . Statement [ ] =
117
+ this . createEagerImports ( factory ) ;
120
118
121
119
const implicitEnumsStatements = this . createImplicitEnums ( factory ) ;
122
120
@@ -490,13 +488,7 @@ export class ModelClassVisitor {
490
488
undefined ,
491
489
overrideType
492
490
? f . createIdentifier ( overrideType )
493
- : this . createTypePropertyAssignment (
494
- f ,
495
- node . type ,
496
- typeChecker ,
497
- hostFilename ,
498
- pluginOptions ,
499
- ) ,
491
+ : this . getTypeUsingTypeChecker ( f , node . type , typeChecker , hostFilename ) ,
500
492
) ;
501
493
502
494
const description = pluginOptions . introspectComments
@@ -517,49 +509,23 @@ export class ModelClassVisitor {
517
509
return objectLiteral ;
518
510
}
519
511
520
- private createTypePropertyAssignment (
512
+ private getTypeUsingTypeChecker (
521
513
f : ts . NodeFactory ,
522
514
node : ts . TypeNode ,
523
515
typeChecker : ts . TypeChecker ,
524
516
hostFilename : string ,
525
- pluginOptions ?: PluginOptions ,
526
517
) {
527
- if ( node ) {
528
- if ( ts . isTypeLiteralNode ( node ) ) {
529
- const propertyAssignments = Array . from ( node . members || [ ] ) . map (
530
- ( member ) => {
531
- const literalExpr = this . createDecoratorObjectLiteralExpr (
532
- f ,
533
- member as ts . PropertySignature ,
534
- typeChecker ,
535
- undefined ,
536
- hostFilename ,
537
- pluginOptions ,
538
- ) ;
539
- return f . createPropertyAssignment (
540
- f . createIdentifier ( member . name . getText ( ) ) ,
541
- literalExpr ,
542
- ) ;
543
- } ,
544
- ) ;
545
-
546
- return f . createParenthesizedExpression (
547
- f . createObjectLiteralExpression ( propertyAssignments ) ,
548
- ) ;
549
- } else if ( ts . isUnionTypeNode ( node ) ) {
550
- const nullableType = findNullableTypeFromUnion ( node , typeChecker ) ;
551
- const remainingTypes = node . types . filter (
552
- ( item ) => item !== nullableType ,
518
+ if ( node && ts . isUnionTypeNode ( node ) ) {
519
+ const nullableType = findNullableTypeFromUnion ( node , typeChecker ) ;
520
+ const remainingTypes = node . types . filter ( ( item ) => item !== nullableType ) ;
521
+
522
+ if ( remainingTypes . length === 1 ) {
523
+ return this . getTypeUsingTypeChecker (
524
+ f ,
525
+ remainingTypes [ 0 ] ,
526
+ typeChecker ,
527
+ hostFilename ,
553
528
) ;
554
-
555
- if ( remainingTypes . length === 1 ) {
556
- return this . createTypePropertyAssignment (
557
- f ,
558
- remainingTypes [ 0 ] ,
559
- typeChecker ,
560
- hostFilename ,
561
- ) ;
562
- }
563
529
}
564
530
}
565
531
@@ -568,38 +534,31 @@ export class ModelClassVisitor {
568
534
return undefined ;
569
535
}
570
536
571
- let typeReference = getTypeReferenceAsString ( type , typeChecker ) ;
572
- if ( ! typeReference ) {
537
+ const _typeReference = getTypeReferenceAsString ( type , typeChecker ) ;
538
+
539
+ if ( ! _typeReference ) {
573
540
return undefined ;
574
541
}
575
- typeReference = replaceImportPath ( typeReference , hostFilename ) ;
576
- if ( typeReference && typeReference . includes ( 'require' ) ) {
542
+
543
+ const { typeReference, importPath } = replaceImportPath (
544
+ _typeReference ,
545
+ hostFilename ,
546
+ ) ;
547
+
548
+ if ( importPath ) {
577
549
// add top-level import to eagarly load class metadata
578
- const importPath = / \( \" ( [ ^ ) ] ) .+ ( \" ) / . exec ( typeReference ) [ 0 ] ;
579
- if ( importPath ) {
580
- let importsToAdd = importsToAddPerFile . get ( hostFilename ) ;
581
- if ( ! importsToAdd ) {
582
- importsToAdd = new Set ( ) ;
583
- importsToAddPerFile . set ( hostFilename , importsToAdd ) ;
584
- }
585
- importsToAdd . add ( importPath . slice ( 2 , importPath . length - 1 ) ) ;
586
- }
550
+ this . importsToAdd . add ( importPath ) ;
587
551
}
588
552
589
553
return f . createIdentifier ( typeReference ) ;
590
554
}
591
555
592
- private createEagerImports (
593
- f : ts . NodeFactory ,
594
- fileName : string ,
595
- ) : ts . ImportEqualsDeclaration [ ] {
596
- const importsToAdd = importsToAddPerFile . get ( fileName ) ;
597
-
598
- if ( ! importsToAdd ) {
556
+ private createEagerImports ( f : ts . NodeFactory ) : ts . ImportEqualsDeclaration [ ] {
557
+ if ( ! this . importsToAdd . size ) {
599
558
return [ ] ;
600
559
}
601
560
602
- return Array . from ( importsToAdd ) . map ( ( path , index ) => {
561
+ return Array . from ( this . importsToAdd ) . map ( ( path , index ) => {
603
562
return createImportEquals ( f , 'eager_import_' + index , path ) ;
604
563
} ) ;
605
564
}
0 commit comments