Skip to content

Commit a71eb31

Browse files
committed
refactor(utils): replace get-all logic with find-first logic
1 parent 3d2f361 commit a71eb31

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/utils/metadata.manager.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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,34 +85,32 @@ export class MetadataManager {
8485
}
8586
}
8687

87-
private getDecoratorMetadata(source: SourceFile, identifier: string): Node[] {
88-
return this.getSourceNodes(source)
89-
.filter(
90-
(node) =>
91-
node.kind === SyntaxKind.Decorator &&
92-
(node as Decorator).expression.kind === SyntaxKind.CallExpression,
93-
)
94-
.map((node) => (node as Decorator).expression as CallExpression)
95-
.filter((expr) => {
96-
const isExpectedExpression =
97-
expr.arguments[0] &&
98-
expr.arguments[0].kind === SyntaxKind.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;
9997

100-
if (!isExpectedExpression) {
101-
return false;
102-
}
98+
const expr = (node as Decorator).expression as CallExpression;
10399

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-
}
100+
const isExpectedExpression =
101+
expr.arguments[0]?.kind === SyntaxKind.ObjectLiteralExpression;
102+
if (!isExpectedExpression) continue;
111103

112-
return true;
113-
})
114-
.map((expr) => expr.arguments[0] as ObjectLiteralExpression);
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+
}
115114
}
116115

117116
private getSourceNodes(sourceFile: SourceFile): Node[] {

0 commit comments

Comments
 (0)