Transmute one JavaScript string into another by way of mutating its AST. Powered by babel and recast.
MIT License
Transmute one JavaScript string into another by way of mutating its AST. Powered by babel and recast.
import { transmute, traverse, types } from "equivalent-exchange";
const someJs = "console.log('hi!');";
const result = transmute(someJs, (ast) => {
traverse(ast, {
StringLiteral(path) {
const { node } = path;
if (node.value === "hi!") {
path.replaceWith(types.stringLiteral("goodbye!"));
}
},
});
});
console.log(result.code); // console.log("goodbye!");
Note that you don't have to use the provided traverse
or types
; you can mutate the ast using whatever traversal method you prefer.
Please see api/index.d.ts for API documentation. There are lots of comments.
MIT