falafel

WebJar for falafel

License

License

MIT
Categories

Categories

Github Development Tools Version Controls
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

github-com-substack-node-falafel
Last Version

Last Version

2.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

falafel
WebJar for falafel
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/substack/node-falafel

Download github-com-substack-node-falafel

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-falafel/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>github-com-substack-node-falafel</artifactId>
    <version>2.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-falafel/
implementation 'org.webjars.npm:github-com-substack-node-falafel:2.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-falafel/
implementation ("org.webjars.npm:github-com-substack-node-falafel:2.1.0")
'org.webjars.npm:github-com-substack-node-falafel:jar:2.1.0'
<dependency org="org.webjars.npm" name="github-com-substack-node-falafel" rev="2.1.0">
  <artifact name="github-com-substack-node-falafel" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='github-com-substack-node-falafel', version='2.1.0')
)
libraryDependencies += "org.webjars.npm" % "github-com-substack-node-falafel" % "2.1.0"
[org.webjars.npm/github-com-substack-node-falafel "2.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.webjars.npm : acorn jar [5.0.0,6)
org.webjars.npm : foreach jar [2.0.5,3)
org.webjars.npm : isarray jar [0.0.1]
org.webjars.npm : object-keys jar [1.0.6,2)

Project Modules

There are no modules declared in this project.

falafel

Transform the ast on a recursive walk.

browser support

build status

This modules uses acorn to create an AST from source code.

falafel döner

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)

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

Performing a post-order 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 use opts.source.

All of the opts will be passed directly to acorn.

custom parser

You may pass in an instance of acorn to the opts as opts.parser to use that version instead of the version of acorn packaged with this library.

var acorn = require('acorn-jsx');

falafel(src, {parser: acorn, plugins: { jsx: true }}, function(node) {
  // this will parse jsx
});

nodes

Aside from the regular acorn 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.

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.

install

With npm do:

npm install falafel

license

MIT

Versions

Version
2.1.0