1.0.0 • Published 6 years ago

ikorni v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

ikorni

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 via generate with getValue.

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.