examples - refactor to load transformer from separate file

This commit is contained in:
2020-01-27 19:13:38 +05:30
parent 687a7e4582
commit b9ba061f31
6 changed files with 96 additions and 62 deletions
+8
View File
@@ -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
};
+30
View File
@@ -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;