Skip to content

Commit

Permalink
Merge pull request #1808 from micalevisk/fix-controller-generator-edg…
Browse files Browse the repository at this point in the history
…e-case

fix: when finding the `@Module()` decorator occurrence
  • Loading branch information
kamilmysliwiec authored Jul 2, 2024
2 parents 9dfede2 + a71eb31 commit c45d85e
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 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,21 +85,32 @@ export class MetadataManager {
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
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) =>
expr.arguments[0] &&
expr.arguments[0].kind === SyntaxKind.ObjectLiteralExpression,
)
.map((expr) => expr.arguments[0] as 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;

const expr = (node as Decorator).expression as CallExpression;

const isExpectedExpression =
expr.arguments[0]?.kind === SyntaxKind.ObjectLiteralExpression;
if (!isExpectedExpression) continue;

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 Expand Up @@ -193,11 +205,9 @@ export class MetadataManager {
toInsert = staticOptions ? this.addBlankLines(symbol) : `${symbol}`;
} else {
const text = (node as Node).getFullText(source);
const itemSeparator = (
text.match(/^\r?\n(\r?)\s+/) ||
const itemSeparator = (text.match(/^\r?\n(\r?)\s+/) ||
text.match(/^\r?\n/) ||
' '
)[0];
' ')[0];
toInsert = `,${itemSeparator}${symbol}`;
}
return this.content.split('').reduce((content, char, index) => {
Expand Down

0 comments on commit c45d85e

Please sign in to comment.