Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rajasegar/ast-node-builder
AST Node building api for jscodeshift
https://github.com/rajasegar/ast-node-builder
abstract-syntax-tree ast codemod codemods jscodeshiift
Last synced: 3 months ago
JSON representation
AST Node building api for jscodeshift
- Host: GitHub
- URL: https://github.com/rajasegar/ast-node-builder
- Owner: rajasegar
- Archived: true
- Created: 2019-11-19T11:40:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-17T09:16:07.000Z (over 2 years ago)
- Last Synced: 2024-05-16T16:07:07.787Z (6 months ago)
- Topics: abstract-syntax-tree, ast, codemod, codemods, jscodeshiift
- Language: JavaScript
- Homepage: https://rajasegar.github.io/ast-builder/
- Size: 698 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# ast-node-builder
WARNING: This package is moved here
https://github.com/rajasegar/ast-tooling/tree/master/packages/ast-node-builderCheckout the api in this [Playground](https://rajasegar.github.io/ast-builder/)
Read the [introductory blog post](https://dev.to/rajasegar/building-ast-nodes-from-source-code-3p49) 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](https://github.com/facebook/jscodeshift).## Usage
```js
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
```js
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.