ast-replace
Replace each matched ast-node passing a test.
Usage
$ npm install --save ast-replace
var parse = parse;var generate = generate;var replace = ; //add rule to replace all `foo` assignments with `bar`.var ast = ; ; //'bar = 1;'
API
replace(Node, replacement) → Node
Replace node matching criterias. Replacement object defines rules to apply replacements:
var replacement = AssignmentExpression: { this === replacement; //true //returning `undefined` means test is passed } { this === replacement; //true //returning `null` replaces node return null; } //supertype, matched after specific types Expression: { //returning `undefined` keeps node the same }
Property | Type | Default | Description |
---|---|---|---|
test | Function | true |
Test whether node found should be replaced. Testing function should return boolean. If omitted, every node will pass the test. |
replace | Function, Node, undefined, null | null |
A replacement for a matched node. Replacing function should return a new node or null . undefined is considered as no change. |