From ed9e54e2a5de2c773d893268488a12420bb2d200 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Sun, 22 Sep 2024 09:27:31 -0300 Subject: [PATCH] fix: making substitution work for classes and methods --- src/emitter.ts | 84 +++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 59 deletions(-) diff --git a/src/emitter.ts b/src/emitter.ts index fa7db5d..eac7634 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -1,6 +1,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import * as ts from 'typescript'; -import { Expression, SyntaxKind } from 'typescript'; import { TypeScriptBinaryLoader } from './typescript-loader'; const tsBinary = new TypeScriptBinaryLoader().load(); @@ -10,66 +9,29 @@ export function before() { return (sf: ts.SourceFile) => { const visitNode = (node: ts.Node): ts.Node => { try { - if (tsBinary.isPropertyDeclaration(node)) { - const currentDecorators = tsBinary.getDecorators(node); - if (currentDecorators) return node; - const expression = ts.factory.createDecorator({} as Expression); - return tsBinary.factory.updatePropertyDeclaration( + if ((tsBinary.isMethodDeclaration(node) + || tsBinary.isPropertyDeclaration(node) + || tsBinary.isClassDeclaration(node)) + && !tsBinary.getDecorators(node)?.length + ) { + const decorator = tsBinary.factory.createDecorator( + tsBinary.factory.createPropertyAccessChain( + tsBinary.factory.createCallExpression( + tsBinary.factory.createIdentifier('require'), + undefined, + [ + tsBinary.factory.createStringLiteral('nestjs-emitter'), + ] + ), undefined, 'simpleDecorator' + ) + ); + node = tsBinary.isClassDeclaration(node) + ? tsBinary.visitEachChild(node, visitNode, ctx) + : tsBinary.factory.replaceDecoratorsAndModifiers( node, - [ - ...(tsBinary.getModifiers(node) ?? []), - { - kind: SyntaxKind.Decorator, - expression, - flags: 0, - end: 0, - }, - ] as ts.Modifier[], - node.name, - node.questionToken ?? node.exclamationToken, - node.type, - node.initializer, + [...(node.modifiers ?? []), decorator] ); - } else if (tsBinary.isClassDeclaration(node)) { - // const classNode = tsBinary.visitEachChild(node, visitNode, ctx); - // const currentDecorators = tsBinary.getDecorators(classNode); - // if (currentDecorators) return classNode; - // const expression = ts.factory.createDecorator({} as Expression); - // return tsBinary.factory.updateClassDeclaration( - // classNode, - // [ - // ...(tsBinary.getModifiers(classNode) ?? []), - // { - // kind: SyntaxKind.Decorator, - // expression, - // flags: 0, - // end: 0, - // parent: classNode, - // }, - // ] as ts.Modifier[], - // classNode.name, - // classNode.typeParameters, - // classNode.heritageClauses, - // classNode.members, - // ); - } else if (tsBinary.isMethodDeclaration(node)) { - // const currentDecorators = tsBinary.getDecorators(node); - // if (currentDecorators) return node; - // const expression = ts.factory.createDecorator({} as Expression); - // return tsBinary.factory.updateMethodDeclaration( - // node, - // [ - // ...(tsBinary.getModifiers(node) ?? []), - // expression, - // ] as ts.Modifier[], - // node.asteriskToken, - // node.name, - // node.questionToken, - // node.typeParameters, - // node.parameters, - // node.type, - // node.body, - // ); + return node; } return tsBinary.visitEachChild(node, visitNode, ctx); } catch { @@ -80,3 +42,7 @@ export function before() { }; }; } + +export function simpleDecorator() { + // +}