Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
117 lines (82 sloc) 2.62 KB


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.



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() + ')');


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


var falafel = require('falafel')

falafel(src, fn)

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 a pre-traversal, so children get called before their parents.

Performing a pre-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.

If typeof src === 'object', then src.source will be used for the source and the rest of the options will be passed directly along 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'.


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.


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


Transform the source for the present node to the string s.


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


With npm do:

npm install falafel