2.1.4 • Published 7 months ago
crucifix v2.1.4
Crucifix
Mozilla Specification JavaScript/ES AST Traversal and Modification Unit, as god intended.
Installation:
Just run this command inside your project directory:
npm i crucifix
Usage (CommonJS):
// Importing the library
const crucifix = require( 'crucifix' );
// Importing our AST data from somewhere
const data = require( './ast.json' );
// Use the library
crucifix.traverse( data, {
// We are going to list all the
// identifiers
Identifier ( node, manager ) {
console.log( node.name );
}
} );
Usage (ES Module):
// Importing the library
import crucifix from 'crucifix';
// Importing our AST data from somewhere
const data = await import( './ast.json', { assert: { type: 'json' } } );
// Use the library
crucifix.traverse( data, {
// We are going to list all the
// identifiers
Identifier ( node, manager ) {
console.log( node.name );
}
} );
Modification methods:
After importing the library and doing necessary things, you can modify the AST using manager methods like so:
// Assuming you've imported your data
// and the crucifix library
const output = crucifix.traverse( data, {
VariableDeclaration ( node, manager ) {
// Clone the node
const newNode = manager.clone();
// After cloning the node, we can
// safely replace the data and
// even append new nodes to parent
// object's own property
manager.replace( [
newNode,
newNode.declarations.map( decl => (
{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'print'
},
arguments: [ decl.id ]
}
}
) )
] )
}
} );
// Input data below is converted to AST
// and saved inside "ast.json" ->
// const hey = 10;
// const test = 20, test2 = 30;
// Output AST will be converted to code
// and the output would be:
// const hey = 10;
// print( hey );
// const test = 20, test2 = 30;
// print( test );
// print( test2 );