🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

equivalent-exchange

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

equivalent-exchange

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).

latest
Source
npmnpm
Version
3.2.0
Version published
Weekly downloads
747
-12.43%
Maintainers
1
Weekly downloads
 
Created
Source

equivalent-exchange

Suchipi's flexible JS/TS codemodding/refactoring toolkit, powered by Babel and Recast.

Features

  • Can parse code using modern ES20XX syntax, as well as JSX/TSX and TypeScript/Flow syntax.
  • Maintains the source formatting of the original source, where possible; only modified parts of the code will be touched.
  • Can generate a source map that maps your input file into your transformed output.

Usage Example

"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);
        }
      },
    });
  });
});

API Documentation

See api/index.md.

License

MIT

Keywords

ast

FAQs

Package last updated on 18 Nov 2025

Did you know?

Socket

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.

Install

Related posts