Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rksm/estree-to-js
Parser / JSON + visitor generator of the markdown estree spec (https://github.com/estree/estree)
https://github.com/rksm/estree-to-js
Last synced: about 1 month ago
JSON representation
Parser / JSON + visitor generator of the markdown estree spec (https://github.com/estree/estree)
- Host: GitHub
- URL: https://github.com/rksm/estree-to-js
- Owner: rksm
- License: mit
- Created: 2016-02-19T21:28:33.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-10-11T14:07:15.000Z (3 months ago)
- Last Synced: 2024-10-13T10:25:22.810Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 57.6 KB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# estree-to-js
Parser / JSON + visitor generator of the markdown [estree spec](https://github.com/estree/estree);
# Usage
## npdejs API
### estree-to-js interface:
```js
var estree = require("estree-to-js");
var visitorSource = estree.fetch("es6")
.then(estree.parse)
.then(spec => estree.createVisitor(spec, []/*exceptions*/, "MyVisitor"))
.catch(console.error)
```### Visitors
Creation of visitor:
```js
var visitor;
visitorSource
.then(source => eval(source + "\n" + "MyVisitor"))
.then(MyVisitor => visitor = new MyVisitor());
```#### Print path example
visitors have an `accept(node, state, path)` method and visitNodeType methods
like `visitVariableDeclaration(node, state, path)`. You can customize it to
your needs, for example:```js
var lang = require("lively.lang"), ast = require("lively.ast");
visitor.accept = lang.fun.wrap(visitor.accept, (proceed, node, state, path) => {
state.push(path.join(".") + " - " + node.type);
proceed(node, state, path);
});
var state = [];
visitor.accept(ast.parse("var x = 1+3"), state, []);
console.log(state.join("\n")); // =>
// - Program
// body.0 - VariableDeclaration
// body.0.declarations.0 - VariableDeclarator
// ...
```#### Rewriting example
```js
var replacer = (node) => lang.obj.merge(node, {id: {type: "Identifier", name: "foo_" + node.id.name}})
var visitor = new MyVisitor();
visitor.accept = lang.fun.wrap(visitor.accept, (proceed, node, state, path) => {
if (node.type === "VariableDeclarator") node = replacer(node);
return proceed(node, state, path);
});
var rewritten = visitor.accept(ast.parse("var x = 1 + 3"), null, []);
ast.stringify(rewritten); // => var foo_x = 1 + 3;
```## command line
--out file output file to write
--generate-json-spec fetch and parse markdown source and generate JSON spec from it (default)
--generate-visitor generate source code for AST visitor class
--json-spec file to json spec. If specified it is used instead of fetching + parsing### Examples:
- `./bin/estree-to-js.js es6` prints JSON spec of es6 estree
- `./bin/estree-to-js.js es6 --generate-visitor` prints JS source es6 estree visitor class# License
[MIT](LICENSE)