Skip to content

Commit

Permalink
refactor(utils): replace get-all logic with find-first logic
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Jun 25, 2024
1 parent 3d2f361 commit a71eb31
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions src/utils/metadata.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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[] {
Expand Down

0 comments on commit a71eb31

Please sign in to comment.