npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-ast-transform

v1.0.3

Published

Transforming and modifying a JS AST tree simplified.

Downloads

9

Readme

easy-ast-transform

Transforming and modifying a JS AST tree simplified.

Purpose

Say you want to transform this

function func(a, b, c) {
  return a + b + c;
}

into

function func(a, b, c) {
  return a - b / c;
}

That would take a lot of work. You will need to traverse through the AST, write an uncountable amount of if statements, and finally modify it.

With this, you can simply put in a before and after "template" AST node, and this library will search through the AST according to your specifications and modify it accordingly.

Examples

Let's transform the above code. (Using recast is recommended and is perfect when used in combination with this library, but using acorn + astring.generate works as well).

const EasyAstTransform = require('easy-ast-transform');
const recast = require('recast');

let templateBefore = recast.parse('function func(a, b, c) { return a + b + c; }');
let templateAfter = recast.parse('function func(a, b, c) { return a - b / c; }');

let inputAst = recast.parse('(function() { function func(a, b, c) { return a + b + c; } })()');

let transformer = new EasyAstTransform(templateBefore.program.body, templateAfter.program.body);
transformer.transform(inputAst); // returns 1 for the number of occurrences replaced

recast.print(inputAst).code === '(function() { function func(a, b, c) { return a - b / c; } })()';

You can also make the transformer more generalized by adding GENERAL_ before your identifier to specify which ones are generalized, using PLACEHOLDER to match all the remaining body or PLACEHOLDER_1 to match one node.

templateBefore = recast.parse('function GENERAL_someFunc(a, b, c) { PLACEHOLDER; return a + b + c; }');
templateAfter = recast.parse('function GENERAL_someFunc(a, b, c) { PLACEHOLDER; return a - b / c; }');

inputAst = recast.parse(`
(function() {
  function func(a, b, c) {
    if (a > b + c) a += b - c;
    while (false) {
      console.log('More stuff here');
    }
    switch (3) {
      case 'I believe':
        break;
      case 'you get the point':
        break;
    }
    return a + b + c;
  }
})();`);

transformer = new EasyAstTransform(templateBefore.program.body, templateAfter.program.body, {
  generalizedPrefix: 'GENERAL_',
  placeholder: 'PLACEHOLDER'
});
transformer.transform(inputAst); // returns 1

recast.print(inputAst).code === `
(function() {
  function func(a, b, c) {
    if (a > b + c) a += b - c;
    while (false) {
      console.log('More stuff here');
    }
    switch (3) {
      case 'I believe':
        break;
      case 'you get the point':
        break;
    }
    return a - b / c;
  }
})();`;

See test cases for more examples.

Sidenote

An area of possible unexpected behavior would be attempting to convert a node wrapped in an ExpressionStatement node, for example, GENERAL_a + GENERAL_b to GENERAL_a - GENERAL_b.

templateBefore = recast.parse('GENERAL_a + GENERAL_b');
templateAfter = recast.parse('GENERAL_a - GENERAL_b');

inputAst = recast.parse('function a() { return a + b }');

transformer = new EasyAstTransform(templateBefore.program.body, templateAfter.program.body);
transformer.transform(inputAst); // returns 0

recast.print(inputAst).code !== 'function a() { return a - b;}';

Why? EasyAstTransform cannot find any ExpressionStatement in return a + b. The latter is actually wrapped in a ReturnStatement node.

To fix this, be sure to specify the expression node inside ExpressionStatement

// arguments to EasyAstTransform only accept an array of AST nodes. Be sure to wrap them in [] if you're specifying a single node
transformer = new EasyAstTransform([templateBefore.program.body[0].expression], [templateAfter.program.body[0].expression]);
transformer.transform(inputAst); // returns 1

recast.print(inputAst).code === 'function a() { return a - b;}';

License

Apache-2.0