2.1.4 • Published 7 months ago

crucifix v2.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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 );

Other methods:

Other properties:

2.1.2

7 months ago

2.1.1

7 months ago

2.1.4

7 months ago

2.1.3

7 months ago

2.1.0

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

2.0.0

7 months ago

1.0.5

7 months ago

1.0.2

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago