node-falafel

transform the ast on a recursive walk

  • 所有者: substack/node-falafel
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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
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.

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 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.

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

主要指標

概覽
名稱與所有者substack/node-falafel
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2012-04-27 05:03:11
推送於2022-05-27 10:44:27
最后一次提交2022-05-11 09:49:04
發布數27
最新版本名稱v2.2.5 (發布於 2022-05-11 09:49:04)
第一版名稱0.0.0 (發布於 2012-04-26 22:03:58)
用户参与
星數570
關注者數14
派生數91
提交數141
已啟用問題?
問題數33
打開的問題數9
拉請求數17
打開的拉請求數9
關閉的拉請求數22
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?