Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/babel/acorn-to-esprima
(unmaintained) Converts acorn tokens to esprima tokens
https://github.com/babel/acorn-to-esprima
unmaintained
Last synced: 3 months ago
JSON representation
(unmaintained) Converts acorn tokens to esprima tokens
- Host: GitHub
- URL: https://github.com/babel/acorn-to-esprima
- Owner: babel
- License: mit
- Archived: true
- Created: 2015-07-10T22:28:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-07-13T23:39:37.000Z (over 8 years ago)
- Last Synced: 2024-04-14T01:39:09.424Z (9 months ago)
- Topics: unmaintained
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 33
- Watchers: 18
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# acorn-to-esprima
Some functions to help transform an acorn/babel ast to esprima format.
Primarily for use in [babel-eslint](https://github.com/babel/babel-eslint), [babel-jscs](https://github.com/jscs-dev/babel-jscs), and [ast explorer](https://github.com/fkling/esprima_ast_explorer)
**There are no dependencies** (the methods were changed to pass in dependencies instead)
The current functions exposed are:
- `function attachComments(ast, comments, tokens)`
- This modifies the comments passed in.
- `function toTokens(tokens, tt)`
- `tt` is `require("babel-core").acorn.tokTypes`
- Converts template string tokens (`convertTemplateType`)
- filters out comment tokens
- runs `toToken` over each token
- `function toToken(token, tt)`
- Sets `token.type`, `token.range`, and `token.value`
- `function toAST(ast, traverse)`
- `traverse` is `require("babel-core").traverse;`
- traverses over the ast and makes any necessary changes (usually es6+)
- `function convertComments(comments)`
- Modifies `comment.type`How to use:
Check out the parse method of https://github.com/babel/babel-eslint/blob/master/index.js
```js
// example
exports.parse = function (code) {
var comments = opts.onComment = [];
var tokens = opts.onToken = [];var ast;
try {
ast = parse(code, {
locations: true,
ranges: true
});
} catch (err) { throw err; }tokens.pop();
ast.tokens = acornToEsprima.toTokens(tokens, tt);acornToEsprima.convertComments(comments);
ast.comments = comments;
acornToEsprima.attachComments(ast, comments, ast.tokens);acornToEsprima.toAST(ast, traverse);
return ast;
}
```