Skip to content

Commit

Permalink
fix: making substitution work for classes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Sep 22, 2024
1 parent 5747234 commit ed9e54e
Showing 1 changed file with 25 additions and 59 deletions.
84 changes: 25 additions & 59 deletions src/emitter.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 {
Expand All @@ -80,3 +42,7 @@ export function before() {
};
};
}

export function simpleDecorator() {
//
}

0 comments on commit ed9e54e

Please sign in to comment.