@@ -30,34 +30,35 @@ export class MetadataManager {
3030 this . content ,
3131 ScriptTarget . ES2017 ,
3232 ) ;
33- const decoratorNodes : Node [ ] = this . getDecoratorMetadata ( source , '@Module' ) ;
34- const node : Node = decoratorNodes [ 0 ] ;
33+ const moduleDecoratorNode = this . findFirstDecoratorMetadata (
34+ source ,
35+ 'Module' ,
36+ ) ;
3537 // If there is no occurrence of `@Module` decorator, nothing will be inserted
36- if ( ! node ) {
38+ if ( ! moduleDecoratorNode ) {
3739 return ;
3840 }
39- const matchingProperties : ObjectLiteralElement [ ] = (
40- node as ObjectLiteralExpression
41- ) . properties
42- . filter ( ( prop ) => prop . kind === SyntaxKind . PropertyAssignment )
43- . filter ( ( prop : PropertyAssignment ) => {
44- const name = prop . name ;
45- switch ( name . kind ) {
46- case SyntaxKind . Identifier :
47- return ( name as Identifier ) . getText ( source ) === metadata ;
48- case SyntaxKind . StringLiteral :
49- return ( name as StringLiteral ) . text === metadata ;
50- default :
51- return false ;
52- }
53- } ) ;
41+ const matchingProperties : ObjectLiteralElement [ ] =
42+ moduleDecoratorNode . properties
43+ . filter ( ( prop ) => prop . kind === SyntaxKind . PropertyAssignment )
44+ . filter ( ( prop : PropertyAssignment ) => {
45+ const name = prop . name ;
46+ switch ( name . kind ) {
47+ case SyntaxKind . Identifier :
48+ return ( name as Identifier ) . getText ( source ) === metadata ;
49+ case SyntaxKind . StringLiteral :
50+ return ( name as StringLiteral ) . text === metadata ;
51+ default :
52+ return false ;
53+ }
54+ } ) ;
5455
5556 symbol = this . mergeSymbolAndExpr ( symbol , staticOptions ) ;
5657 const addBlankLinesIfDynamic = ( ) => {
5758 symbol = staticOptions ? this . addBlankLines ( symbol ) : symbol ;
5859 } ;
5960 if ( matchingProperties . length === 0 ) {
60- const expr = node as ObjectLiteralExpression ;
61+ const expr = moduleDecoratorNode as ObjectLiteralExpression ;
6162 if ( expr . properties . length === 0 ) {
6263 addBlankLinesIfDynamic ( ) ;
6364 return this . insertMetadataToEmptyModuleDecorator (
@@ -84,21 +85,32 @@ export class MetadataManager {
8485 }
8586 }
8687
87- // eslint-disable-next-line @typescript-eslint/no-unused-vars
88- private getDecoratorMetadata ( source : SourceFile , identifier : string ) : Node [ ] {
89- return this . getSourceNodes ( source )
90- . filter (
91- ( node ) =>
92- node . kind === SyntaxKind . Decorator &&
93- ( node as Decorator ) . expression . kind === SyntaxKind . CallExpression ,
94- )
95- . map ( ( node ) => ( node as Decorator ) . expression as CallExpression )
96- . filter (
97- ( expr ) =>
98- expr . arguments [ 0 ] &&
99- expr . arguments [ 0 ] . kind === SyntaxKind . ObjectLiteralExpression ,
100- )
101- . map ( ( expr ) => expr . arguments [ 0 ] as ObjectLiteralExpression ) ;
88+ private findFirstDecoratorMetadata (
89+ source : SourceFile ,
90+ identifier : string ,
91+ ) : ObjectLiteralExpression | undefined {
92+ for ( const node of this . getSourceNodes ( source ) ) {
93+ const isDecoratorFactoryNode =
94+ node . kind === SyntaxKind . Decorator &&
95+ ( node as Decorator ) . expression . kind === SyntaxKind . CallExpression ;
96+ if ( ! isDecoratorFactoryNode ) continue ;
97+
98+ const expr = ( node as Decorator ) . expression as CallExpression ;
99+
100+ const isExpectedExpression =
101+ expr . arguments [ 0 ] ?. kind === SyntaxKind . ObjectLiteralExpression ;
102+ if ( ! isExpectedExpression ) continue ;
103+
104+ if ( expr . expression . kind === SyntaxKind . Identifier ) {
105+ const escapedText = ( expr . expression as Identifier ) . escapedText ;
106+ const isTargetIdentifier = escapedText
107+ ? escapedText . toLowerCase ( ) === identifier . toLowerCase ( )
108+ : true ;
109+ if ( isTargetIdentifier ) {
110+ return expr . arguments [ 0 ] as ObjectLiteralExpression ;
111+ }
112+ }
113+ }
102114 }
103115
104116 private getSourceNodes ( sourceFile : SourceFile ) : Node [ ] {
@@ -193,11 +205,9 @@ export class MetadataManager {
193205 toInsert = staticOptions ? this . addBlankLines ( symbol ) : `${ symbol } ` ;
194206 } else {
195207 const text = ( node as Node ) . getFullText ( source ) ;
196- const itemSeparator = (
197- text . match ( / ^ \r ? \n ( \r ? ) \s + / ) ||
208+ const itemSeparator = ( text . match ( / ^ \r ? \n ( \r ? ) \s + / ) ||
198209 text . match ( / ^ \r ? \n / ) ||
199- ' '
200- ) [ 0 ] ;
210+ ' ' ) [ 0 ] ;
201211 toInsert = `,${ itemSeparator } ${ symbol } ` ;
202212 }
203213 return this . content . split ( '' ) . reduce ( ( content , char , index ) => {
0 commit comments