# free-falafel

> transform the ast on a recursive walk

Latest version **0.2.1** (published 2013-05-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install free-falafel
pnpm add free-falafel
yarn add free-falafel
bun add free-falafel
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2013-05-12 |
| First published | 2012-12-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.4.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | freethenation |
| Keywords | ast, source, traversal, syntax, tree, burrito |

## Links

- npm: https://www.npmjs.com/package/free-falafel
- Repository: https://github.com/freethenation/node-falafel
- npm.io page: https://npm.io/package/free-falafel

## Dependencies (1)

- [esprima](https://npm.io/package/esprima.md) substack/esprima#is-keyword

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2013-05-12
- 0.1.6 — 2012-12-22
- 0.1.5 — 2012-12-22

## README

# free-falafel

Transform the [ast](http://en.wikipedia.org/wiki/Abstract_syntax_tree) on a recursive walk.

[![Build Status](https://travis-ci.org/freethenation/node-falafel.png?branch=master)](https://travis-ci.org/freethenation/node-falafel)

This module is like [burrito](https://github.com/substack/node-burrito),
except that it uses [esprima](http://esprima.org) instead of
[uglify](https://github.com/mishoo/UglifyJS) for friendlier-looking ast nodes.

# Example

## array.js

Put a function wrapper around all array literals.

``` js
var falafel = require('free-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

``` js
var falafel = require('free-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](http://esprima.org) 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.

``` js
var falafel = require('free-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 && 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](http://jsbin.com/free-falafel/4/edit)

# Install

With [npm](http://npmjs.org) do:

```
npm install free-falafel
```

# License

MIT

---
_Source: https://npm.io/package/free-falafel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
