Skip to content

Commit 3d2f361

Browse files
committed
fix(utils): when finding the @Module decorator occurrence
1 parent 0418457 commit 3d2f361

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/utils/metadata.manager.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class MetadataManager {
3030
this.content,
3131
ScriptTarget.ES2017,
3232
);
33-
const decoratorNodes: Node[] = this.getDecoratorMetadata(source, '@Module');
33+
const decoratorNodes: Node[] = this.getDecoratorMetadata(source, 'Module');
3434
const node: Node = decoratorNodes[0];
3535
// If there is no occurrence of `@Module` decorator, nothing will be inserted
3636
if (!node) {
@@ -84,7 +84,6 @@ export class MetadataManager {
8484
}
8585
}
8686

87-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8887
private getDecoratorMetadata(source: SourceFile, identifier: string): Node[] {
8988
return this.getSourceNodes(source)
9089
.filter(
@@ -93,11 +92,25 @@ export class MetadataManager {
9392
(node as Decorator).expression.kind === SyntaxKind.CallExpression,
9493
)
9594
.map((node) => (node as Decorator).expression as CallExpression)
96-
.filter(
97-
(expr) =>
95+
.filter((expr) => {
96+
const isExpectedExpression =
9897
expr.arguments[0] &&
99-
expr.arguments[0].kind === SyntaxKind.ObjectLiteralExpression,
100-
)
98+
expr.arguments[0].kind === SyntaxKind.ObjectLiteralExpression;
99+
100+
if (!isExpectedExpression) {
101+
return false;
102+
}
103+
104+
if (expr.expression.kind === SyntaxKind.Identifier) {
105+
const escapedText = (expr.expression as Identifier).escapedText;
106+
const isIdentifier = escapedText
107+
? escapedText.toLowerCase() === identifier.toLowerCase()
108+
: true;
109+
return isIdentifier;
110+
}
111+
112+
return true;
113+
})
101114
.map((expr) => expr.arguments[0] as ObjectLiteralExpression);
102115
}
103116

@@ -193,11 +206,9 @@ export class MetadataManager {
193206
toInsert = staticOptions ? this.addBlankLines(symbol) : `${symbol}`;
194207
} else {
195208
const text = (node as Node).getFullText(source);
196-
const itemSeparator = (
197-
text.match(/^\r?\n(\r?)\s+/) ||
209+
const itemSeparator = (text.match(/^\r?\n(\r?)\s+/) ||
198210
text.match(/^\r?\n/) ||
199-
' '
200-
)[0];
211+
' ')[0];
201212
toInsert = `,${itemSeparator}${symbol}`;
202213
}
203214
return this.content.split('').reduce((content, char, index) => {

0 commit comments

Comments
 (0)