ikorni
v1.0.0
Published
A wrapper around acorn exposing a replace function for updating the source code
Downloads
4
Maintainers
Readme
ikorni
A wrapper for acorn allowing for easy mutation of Node values.
Installation
npm install --save ikorni
Usage
var ikorni = require('./index.js');
var ast = ikorni.parse('var x = 0;');
ast.replace(ast.body[0].declarations[0].id, 'y');
console.log(ast.generate);
var y = 0;
API
var ast = ikorni.parse(source, opts)
Same as
acorn.parse(input, opts)
ast.replace(node, value)
Update the source code representing the provided node
in the AST with
value
.
A few notes:
- Strings must include quotes, example:
ast.replace(node, '\'foo.js\'')
. node
will be left unchanged. If you need to update the AST with your changes, you will need to generate the new source and re-parse. You can get the current value of the node as it would be written out viagenerate
withgetValue
.
var source = ast.getValue(node)
Returns the current source represented by node
, based on the changes made by
all of the replace
invocations.
var source = ast.generate()
Returns a new source file updated with everything that has been mutated with
ast.replace
.