-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AST automatic adding cqrs message handler decorator (#82)
- Loading branch information
Showing
12 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,5 @@ export default { | |
} | ||
|
||
return cachedTransformer; | ||
}, | ||
} | ||
}; |
64 changes: 64 additions & 0 deletions
64
src/Compiler/Transformer/Feature/FeatureApplicationMessageHandlerTsTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { FeatureApplicationCommandMeta, type FeatureApplicationQueryMeta } from '../../../Util/Feature/Meta'; | ||
import ts from "typescript"; | ||
import { type FeatureMeta } from "../../../Util/Feature/Meta"; | ||
import { AbstractFeatureTsTransformer } from "./AbstractFeatureTsTransformer"; | ||
import type { FeatureTransformContext } from "./FeatureTransformContext"; | ||
import type { FeatureSourcePath } from "../../../Util/Feature/FeatureModuleDiscoverer"; | ||
import { ImportDeclarationWrapper } from "../Helper/ImportDeclarationWrapper"; | ||
|
||
interface VisitContext extends FeatureTransformContext { | ||
meta: FeatureApplicationCommandMeta | FeatureApplicationQueryMeta; | ||
handlerDecoratorName: string; | ||
cqrsImport: ImportDeclarationWrapper; | ||
} | ||
|
||
export class FeatureApplicationMessageHandlerTsTransformer extends AbstractFeatureTsTransformer { | ||
|
||
public supports(featureSourcePath: FeatureSourcePath, feature: FeatureMeta): boolean { | ||
const meta = feature.applicationCqrsHandlerMap.get(featureSourcePath.localSourcePath); | ||
return !!meta; | ||
} | ||
|
||
public transform(source: ts.SourceFile, context: FeatureTransformContext): ts.SourceFile { | ||
const messageMeta = context.feature.applicationCqrsHandlerMap.get(context.featureSourcePath.localSourcePath)!; | ||
const handlerDecoratorName = messageMeta instanceof FeatureApplicationCommandMeta ? "CommandHandler" : "QueryHandler"; | ||
const cqrsImport = ImportDeclarationWrapper.create(handlerDecoratorName, "@nestjs/cqrs", context.tsContext); | ||
const visitContext: VisitContext = { | ||
...context, | ||
meta: messageMeta, | ||
handlerDecoratorName: handlerDecoratorName, | ||
cqrsImport | ||
}; | ||
|
||
const transformed = ts.factory.updateSourceFile(source, [ | ||
cqrsImport.declaration, | ||
...ts.visitNodes( | ||
source.statements, | ||
this.visitSourceStatement.bind(this, visitContext), | ||
(node): node is ts.Statement => ts.isStatement(node) | ||
) | ||
]); | ||
|
||
return transformed; | ||
} | ||
|
||
private visitSourceStatement(visitContext: VisitContext, node: ts.Statement) { | ||
if (ts.isClassDeclaration(node)) { | ||
const handlerDecorator = ts.factory.createDecorator(ts.factory.createCallExpression( | ||
visitContext.cqrsImport.get(visitContext.handlerDecoratorName), | ||
undefined, | ||
[ts.factory.createIdentifier(visitContext.meta.className)] | ||
)); | ||
return ts.factory.updateClassDeclaration( | ||
node, | ||
[handlerDecorator, ...node.modifiers ?? []], | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
node.members | ||
); | ||
} | ||
|
||
return node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Meta"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
test/helper/libs/test-lib/src/Book/Application/Book/Query/GetById/BookGetByIdQueryHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters