mirror of
https://github.com/bendtherules/ts-transformer-visualizer.git
synced 2026-08-18 22:01:45 +00:00
examples - refactor to load transformer from separate file
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import SwapExpression from "./swap-expression";
|
||||
import { TransformModuleExport } from "src/types";
|
||||
|
||||
export type ModuleNames = "swap-expression";
|
||||
|
||||
export const moduleMap: { [key in ModuleNames]: TransformModuleExport } = {
|
||||
"swap-expression": SwapExpression
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
import * as ts from "typescript";
|
||||
import { TransformModuleExport } from "../types";
|
||||
|
||||
const sourceCodeString = `x = y; a = b + c`;
|
||||
|
||||
const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
|
||||
return sourceFile => {
|
||||
const visitor = (node: ts.Node): ts.Node => {
|
||||
if (
|
||||
ts.isBinaryExpression(node) &&
|
||||
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
||||
ts.isIdentifier(node.left) &&
|
||||
ts.isIdentifier(node.right)
|
||||
) {
|
||||
return ts.updateBinary(node, node.right, node.left);
|
||||
}
|
||||
|
||||
return ts.visitEachChild(node, visitor, context);
|
||||
};
|
||||
|
||||
return ts.visitNode(sourceFile, visitor);
|
||||
};
|
||||
};
|
||||
|
||||
const toExport: TransformModuleExport = {
|
||||
sourceCodeString,
|
||||
transformer
|
||||
};
|
||||
|
||||
export default toExport;
|
||||
Reference in New Issue
Block a user