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 🙏

© 2025 – Pkg Stats / Ryan Hefner

free-falafel

v0.2.1

Published

transform the ast on a recursive walk

Readme

free-falafel

Transform the ast on a recursive walk.

Build Status

This module is like burrito, except that it uses esprima instead of uglify for friendlier-looking ast nodes.

Example

array.js

Put a function wrapper around all array literals.

var falafel = require('falafel');

var src = '(' + function () {
    var xs = [ 1, 2, [ 3, 4 ] ];
    var ys = [ 5, 6 ];
    console.dir([ xs, ys ]);
} + ')()';

var output = falafel(src, function (node) {
    if (node.type === 'ArrayExpression') {
        node.update('fn(' + node.source() + ')');
    }
});
console.log(output);

output:

(function () {
    var xs = fn([ 1, 2, fn([ 3, 4 ]) ]);
    var ys = fn([ 5, 6 ]);
    console.dir(fn([ xs, ys ]));
})()

Methods

var falafel = require('falafel')

falafel(src, opts={}, fn, breadthFirstFn)

Transform the string source src with the function fn, returning a string-like transformed output object.

For every node in the ast, fn(node) fires. The recursive walk is depth first, so children get called before their parents.

Performing the transforms during a depth first traversal makes it easier to write nested transforms since transforming parents often requires transforming all its children first.

The return value is string-like (it defines .toString() and .inspect()) so that you can call node.update() asynchronously after the function has returned and still capture the output.

Instead of passing a src you can also pass opts.source or, if the source code has already been parsed into an ast, you can pass opts.ast.

All of the opts will be passed directly to esprima except for 'range' which is always turned on because falafel needs it.

Some of the options you might want from esprima includes: 'loc', 'raw', 'comments', 'tokens', and 'tolerant'.

You can optionally provide the function breadthFirstFn. This function will be called before fn during a breadth first traversal of the ast. This function allows you to add additional properties to the node parameter so that you can easily do things like not transforming any code inside of a function definition. There is an example of this below.

Nodes

Aside from the regular esprima data, you can also call some inserted methods on nodes.

Aside from updating the current node, you can also reach into sub-nodes to call update functions on children from parent nodes.

node.source()

Return the source for the given node, including any modifications made to children nodes.

node.update(s)

Transform the source for the present node to the string s. This function is not available during the breadth first traversal of the ast.

Note that in 'ForStatement' node types, there is an existing subnode called update. For those nodes all the properties are copied over onto the node.update() function.

node.parent

Reference to the parent element or null at the root element.

More Examples

breadthFirstFn example

Put a function wrapper around all array literals that are not inside of a function definition.

var falafel = require('falafel');

var src = '(' + function () {
    var xs = [ 1, 2, [ 3, 4 ] ];
    var ys = [ 5, 6 ];
    somefunc([ xs, ys ]);
} + ')();\n';
src += 'var g = [ 5, 6 ];';

var output = falafel(src, 
    function (node) {
        if (node.type === 'ArrayExpression' && !node.inFunc) {
            node.update('fn(' + node.source() + ')');
        }
    },
    function (node) {
        if (node.type === 'FunctionExpression') {
            node.inFunc = true;
        }
        else if (node.parent.inFunc) {
            //inherit from parent
            node.inFunc = node.parent.inFunc;
        }
        else { node.inFunc = false; }
    });
console.log(output.toString());

output:

(function () {
    var xs = [ 1, 2, [ 3, 4 ] ];
    var ys = [ 5, 6 ];
    somefunc([ xs, ys ]);
})();
var g = fn([ 5, 6 ]);

You can play with this example at JS Bin here

Install

With npm do:

npm install free-falafel

License

MIT