ast-node-builder
AST Node Building api
Last updated 6 months ago by rajasegar .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ast-node-builder 
SYNC missed versions from official npm registry.

ast-node-builder

Build Status Coverage Status Version

Checkout the api in this Playground

Read the introductory blog post to know more about the tool.

Build your Abstract Syntax Trees (AST) directly from code. You give the input in the form of code and get the builder API in jscodeshift.

Usage

const { buildAST } = require('ast-node-builder');
const { parse }  = require('recast');
const code = `
class MyComponent extends ReactComponent {
  constructor(a, b) {
    this.a = a;
    this.b = b;
  }

  hello(x, y) {
    console.log(x, y);
  }
}
`;

let ast = parse(code);

let pseudoAst =  buildAST(ast);
console.log(pseudoAst);

Output

j.classDeclaration(
  j.identifier('MyComponent'),
  j.classBody([j.methodDefinition(
    'constructor',
    j.identifier('constructor'),
    j.functionExpression(
      null,
      [j.identifier('a'),j.identifier('b')],
      j.blockStatement([j.expressionStatement(j.assignmentExpression(
        '=',
        j.memberExpression(
          j.thisExpression(),
          j.identifier('a'),
          false
        ),
        j.identifier('a')
      )),j.expressionStatement(j.assignmentExpression(
        '=',
        j.memberExpression(
          j.thisExpression(),
          j.identifier('b'),
          false
        ),
        j.identifier('b')
      ))])
    ),
    false
  ),j.methodDefinition(
    'method',
    j.identifier('hello'),
    j.functionExpression(
      null,
      [j.identifier('x'),j.identifier('y')],
      j.blockStatement([j.expressionStatement(j.callExpression(
        j.memberExpression(
          j.identifier('console'),
          j.identifier('log'),
          false
        ),
        [j.identifier('x'),j.identifier('y')]
      ))])
    ),
    false
  )]),
  j.identifier('ReactComponent')
)

Debugging

Place debugger statements in the code in appropriate places and run:

$ npm run debug

This will start mocha tests in debug mode and you can use Chrome Dev Tools to view the debugger.

Current Tags

  • 3.0.12                                ...           latest (6 months ago)

44 Versions

  • 3.0.12                                ...           6 months ago
  • 3.0.11                                ...           6 months ago
  • 3.0.10                                ...           8 months ago
  • 3.0.9                                ...           8 months ago
  • 3.0.8                                ...           9 months ago
  • 3.0.7                                ...           9 months ago
  • 3.0.6                                ...           9 months ago
  • 3.0.5                                ...           9 months ago
  • 3.0.4                                ...           9 months ago
  • 3.0.3                                ...           9 months ago
  • 3.0.2                                ...           9 months ago
  • 3.0.1                                ...           9 months ago
  • 3.0.0                                ...           9 months ago
  • 1.2.4                                ...           9 months ago
  • 1.2.3                                ...           9 months ago
  • 1.2.2                                ...           9 months ago
  • 1.2.1                                ...           9 months ago
  • 1.2.0                                ...           9 months ago
  • 1.1.4                                ...           9 months ago
  • 1.1.3                                ...           9 months ago
  • 1.1.2                                ...           9 months ago
  • 1.1.1                                ...           9 months ago
  • 1.1.0                                ...           9 months ago
  • 1.0.22                                ...           9 months ago
  • 1.0.21                                ...           9 months ago
  • 1.0.20                                ...           10 months ago
  • 1.0.19                                ...           10 months ago
  • 1.0.18                                ...           10 months ago
  • 1.0.17                                ...           10 months ago
  • 1.0.16                                ...           10 months ago
  • 1.0.15                                ...           10 months ago
  • 1.0.14                                ...           10 months ago
  • 1.0.13                                ...           10 months ago
  • 1.0.12                                ...           10 months ago
  • 1.0.11                                ...           10 months ago
  • 1.0.9                                ...           10 months ago
  • 1.0.8                                ...           10 months ago
  • 1.0.6                                ...           10 months ago
  • 1.0.5                                ...           10 months ago
  • 1.0.4                                ...           10 months ago
  • 1.0.3                                ...           10 months ago
  • 1.0.2                                ...           10 months ago
  • 1.0.1                                ...           10 months ago
  • 1.0.0                                ...           10 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 44
Last Day 0
Last Week 0
Last Month 44
Dependencies (1)
Dev Dependencies (10)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |