Yuku
JavaScript
The yuku package parses, analyzes, walks, builds, and prints JavaScript and TypeScript, with no third-party dependencies. It runs natively where its addon for the platform installs, and as WebAssembly everywhere else.
npm install yuku
Cloudflare Workers and Vercel Edge run WebAssembly only from a file, so there also install @yuku-toolchain/wasm.
import { b, parse, print, walk } from "yuku";
const file = parse(`
const RATE = 0.2;
export const tax = (price: number) => price * RATE;
export const scale = (RATE: number) => RATE * 2;
`, { path: "tax.ts", analyze: true });
const rate = file.rootScope.find("RATE");
walk(file.program, {
Identifier(node, ctx) {
if (file.referenceOf(node)?.binding === rate) ctx.replace(b.Literal({ value: 0.2 }));
},
});
print(file, { strip: true }).code;
// const RATE = 0.2;
// export const tax = (price) => price * 0.2;
// export const scale = (RATE) => RATE * 2;