From a71eb31d5a2da0a3c787e99185dfcafa1901650b Mon Sep 17 00:00:00 2001 From: "Micael Levi L. Cavalcante" Date: Tue, 25 Jun 2024 12:58:16 -0400 Subject: [PATCH] refactor(utils): replace get-all logic with find-first logic --- src/utils/metadata.manager.ts | 87 +++++++++++++++++------------------ 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/src/utils/metadata.manager.ts b/src/utils/metadata.manager.ts index 10a0906c7..cd6e6b3f5 100644 --- a/src/utils/metadata.manager.ts +++ b/src/utils/metadata.manager.ts @@ -30,34 +30,35 @@ export class MetadataManager { this.content, ScriptTarget.ES2017, ); - const decoratorNodes: Node[] = this.getDecoratorMetadata(source, 'Module'); - const node: Node = decoratorNodes[0]; + const moduleDecoratorNode = this.findFirstDecoratorMetadata( + source, + 'Module', + ); // If there is no occurrence of `@Module` decorator, nothing will be inserted - if (!node) { + if (!moduleDecoratorNode) { return; } - const matchingProperties: ObjectLiteralElement[] = ( - node as ObjectLiteralExpression - ).properties - .filter((prop) => prop.kind === SyntaxKind.PropertyAssignment) - .filter((prop: PropertyAssignment) => { - const name = prop.name; - switch (name.kind) { - case SyntaxKind.Identifier: - return (name as Identifier).getText(source) === metadata; - case SyntaxKind.StringLiteral: - return (name as StringLiteral).text === metadata; - default: - return false; - } - }); + const matchingProperties: ObjectLiteralElement[] = + moduleDecoratorNode.properties + .filter((prop) => prop.kind === SyntaxKind.PropertyAssignment) + .filter((prop: PropertyAssignment) => { + const name = prop.name; + switch (name.kind) { + case SyntaxKind.Identifier: + return (name as Identifier).getText(source) === metadata; + case SyntaxKind.StringLiteral: + return (name as StringLiteral).text === metadata; + default: + return false; + } + }); symbol = this.mergeSymbolAndExpr(symbol, staticOptions); const addBlankLinesIfDynamic = () => { symbol = staticOptions ? this.addBlankLines(symbol) : symbol; }; if (matchingProperties.length === 0) { - const expr = node as ObjectLiteralExpression; + const expr = moduleDecoratorNode as ObjectLiteralExpression; if (expr.properties.length === 0) { addBlankLinesIfDynamic(); return this.insertMetadataToEmptyModuleDecorator( @@ -84,34 +85,32 @@ export class MetadataManager { } } - private getDecoratorMetadata(source: SourceFile, identifier: string): Node[] { - return this.getSourceNodes(source) - .filter( - (node) => - node.kind === SyntaxKind.Decorator && - (node as Decorator).expression.kind === SyntaxKind.CallExpression, - ) - .map((node) => (node as Decorator).expression as CallExpression) - .filter((expr) => { - const isExpectedExpression = - expr.arguments[0] && - expr.arguments[0].kind === SyntaxKind.ObjectLiteralExpression; + private findFirstDecoratorMetadata( + source: SourceFile, + identifier: string, + ): ObjectLiteralExpression | undefined { + for (const node of this.getSourceNodes(source)) { + const isDecoratorFactoryNode = + node.kind === SyntaxKind.Decorator && + (node as Decorator).expression.kind === SyntaxKind.CallExpression; + if (!isDecoratorFactoryNode) continue; - if (!isExpectedExpression) { - return false; - } + const expr = (node as Decorator).expression as CallExpression; - if (expr.expression.kind === SyntaxKind.Identifier) { - const escapedText = (expr.expression as Identifier).escapedText; - const isIdentifier = escapedText - ? escapedText.toLowerCase() === identifier.toLowerCase() - : true; - return isIdentifier; - } + const isExpectedExpression = + expr.arguments[0]?.kind === SyntaxKind.ObjectLiteralExpression; + if (!isExpectedExpression) continue; - return true; - }) - .map((expr) => expr.arguments[0] as ObjectLiteralExpression); + if (expr.expression.kind === SyntaxKind.Identifier) { + const escapedText = (expr.expression as Identifier).escapedText; + const isTargetIdentifier = escapedText + ? escapedText.toLowerCase() === identifier.toLowerCase() + : true; + if (isTargetIdentifier) { + return expr.arguments[0] as ObjectLiteralExpression; + } + } + } } private getSourceNodes(sourceFile: SourceFile): Node[] {