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

astronaut

v0.1.4

Published

A library for transforming Esprima/SpiderMonkey ASTs.

Downloads

14

Readme

Astronaut

Astronaut is a library for traversing and manipulating the javascript AST. Build Status

Examples

Let's replace all instances of 5 with 4:

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
            node.value(4);
        }
    })
    .deparse()
//'1 + 4'

Let's replace all instances of 5 with a call to the function "f":

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
            node.replace('f()');
        }
    })
    .deparse()
//'1 + f()'

Or how about we wrap them in a call to the function f:

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
            node.wrap('f(<%= node %>)');
        }
    })
    .deparse()
//'1 + f(5)'

Let's prefix all calls to f where the second argument is an array expression with a call to g and the same array:

var code = "f(); f(1, [1,2,3]);";
astronaut(code).walk(function(node) {
    if (node.isCallExpression() 
            && node.calleeName() === "f"
            && node.arguments().length > 1
            && node.arguments()[1].isArrayExpression()) {
        node.prefix("g(" + node.arguments()[1].deparse() + ')');
    }
}).deparse()
//"f();g([1,2,3]);f(1,[1,2,3]);",

Let's wrap a function body in a try/catch:

var code = "function f(a) { return a; }";
astronaut(code).walk(function(node) {
    if (node.isBlockStatement() && node.parent.isFunctionDeclaration()) {
        node.wrap("try { <%= node %> } catch(e) {} }");
    }
}).deparse()
//"function f(a) { try { return a; } catch(e) {} }";

Api

astronaut

var astronaut = require('astronaut')

Astronaut takes a string to be parsed (using Esprima), or an already-parsed Esprima/SpiderMonkey AST, and returns an AstNode.

AstNode

AstNode.data

The SpiderMonkey AST data for this node, with sub-nodes wrapped in AstNodes. Updates to this mutate the tree, but for non-primitive values it's recommended to use the replace/wrap functions.

AstNode.parent

A pointer to the parent of this node

AstNode.parentKey

The key in the parent that points to this node.

AstNode.parentArrayIndex

If parent[key] is an array, the index of this node in that array. This enables affix functions.

AstNode.is <NodeType>()

Returns a boolean indicating whether or note the node is of the specified type.

AstNode.ast()

Returns the SpiderMonkey AST for this node.

AstNode.walk(callback)

Walk the AST starting at this node, calling the callback along the way.

AstNode.map(callback)

Walk the tree, replacing nodes with new nodes produced by callback(node). To avoid thrashing the tree, if the callback returns null/undefined, no replacement occurs.

AstNode.reduce(accumulator, callback)

Reduce the tree down to a single value. callback should be a function that expects the accumulator as the first argument, the current node as the second, and returns the new accumulator.

AstNode.deparse(options)

A shortcut for escodegen.generate(AstNode.ast(), options)

AstNode.wrap(codeOrTemplate)

Wrap the current node in the code specified by an underscore template. The template can be a string to be parsed or a compiled template. The current node should be represented as node in the template. For example: node.wrap("f(<%= node =>)")

AstNode.replace(code)

Replace the current node with the results of parsing the input code

AstNode.prefix(code)

Insert the statement specified in code prior to the statement encoded by the current node.

AstNode.suffix(code)

Insert the statement specified in code after the statement encoded by the current node.

Installation

npm install astronaut

Acknowledgements

I was inspired by burrito, but was dissapointed by the lack of child pointers and wanted something that supported the Esprima AST. Falafel seems like it may have solved both problems.