
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
equivalent-exchange
Advanced tools
Transmute one JavaScript string into another by way of mutating its AST. Powered by [babel](https://babeljs.io/) and [recast](https://www.npmjs.com/package/recast).
Suchipi's flexible JS/TS codemodding/refactoring toolkit, powered by Babel and Recast.
"Transmute" one string of code into another by using the transmute function:
import { transmute } from "equivalent-exchange";
const someJs = "console.log('hi!');";
const result = transmute(someJs, (ast) => {
// Within this callback, we mutate the AST as desired for the
// codemod/refactor.
// Use https://astexplorer.net/ to see what this tree
// structure looks like!
ast.program.body[0].expression.callee.arguments[0].value = "goodbye!";
});
console.log(result.code); // console.log("goodbye!");
For more flexible codemods, use the included utilities:
import { transmute, traverse, types } from "equivalent-exchange";
// `traverse` and `types` come from Babel!
const someJs = "console.log('hi!', 'hi again!');";
const result = transmute(someJs, (ast) => {
// Walk the tree...
traverse(ast, {
// And for every StringLiteral node we find...
StringLiteral(path) {
const { node } = path;
// If it starts with 'hi'...
if (node.value.startsWith("hi")) {
const newValue = node.value.replace(/^hi/, "bye");
const newNode = types.stringLiteral(newValue);
// Change 'hi' to 'bye'
path.replaceWith(newNode);
}
},
});
});
console.log(result.code); // "console.log('bye!', 'bye again!');"
To transform files in bulk, use the "mapFiles" API:
import { transmute, traverse, types, mapFiles } from "equivalent-exchange";
const results = await mapFiles.fromGlob("src/*.ts", (content, path) => {
if (path === "src/build-docs.ts") {
// Skip this file
return;
}
return transmute(content, (ast) => {
// Walk the tree...
traverse(ast, {
// And for every StringLiteral node we find...
StringLiteral(path) {
const { node } = path;
// If it starts with 'hi'...
if (node.value.startsWith("hi")) {
const newValue = node.value.replace(/^hi/, "bye");
const newNode = types.stringLiteral(newValue);
// Change 'hi' to 'bye'
path.replaceWith(newNode);
}
},
});
});
});
See api/index.md.
MIT
FAQs
Transmute one JavaScript string into another by way of mutating its AST. Powered by [babel](https://babeljs.io/) and [recast](https://www.npmjs.com/package/recast).
The npm package equivalent-exchange receives a total of 734 weekly downloads. As such, equivalent-exchange popularity was classified as not popular.
We found that equivalent-exchange demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.