convert estree ast to babel
npm install estree-to-babel[NPMIMGURL]: https://img.shields.io/npm/v/estree-to-babel.svg?style=flat&longCache=true
[BuildStatusURL]: https://github.com/coderaiser/estree-to-babel/actions?query=workflow%3A%22Node+CI%22 "Build Status"
[BuildStatusIMGURL]: https://github.com/coderaiser/estree-to-babel/workflows/Node%20CI/badge.svg
[NPMURL]: https://npmjs.org/package/estree-to-babel "npm"
[BuildStatusURL]: https://travis-ci.org/coderaiser/estree-to-babel "Build Status"
[CoverageURL]: https://coveralls.io/github/coderaiser/estree-to-babel?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/estree-to-babel/badge.svg?branch=master&service=github
Convert ESTree-compatible JavaScript AST to Babel AST.
To use parsers like:
- acorn
- cherow
- espree
- etc...
With babel tools like:
- @babel/traverse
- @babel/types
- etc...
The thing is @babel/parser has a little differences with estree standard:
- Property of ObjectExpression and ObjectPattern called ObjectProperty;
- FunctionExpression of a Property located in ObjectMethod node;
- File node;
- StringLiteral, NumericLiteral, NullLiteral, RegExpLiteral, BooleanLiteral instead of Literal;
- ClassMethod instead of MethodDefinition;
- ClassPrivateMethod;
- ClassPrivateName stores name as Identifier in id field;
- ClassPrivateProperty instead of FieldDefinition;
- OptionalMemberExpression and OptionalCallExpression instead of ChainExpression;
- ImportDeclaration and ExportNamedDeclaration has attributes;
- JSXText has extra field;
- extra.parenthesized=true instead of ParenthesizedExpression;
- etc...
Also @babel/parser has differences with typescript-estree:
- ClassPrivateProperty instead of PropertyDefinition when key.type=PrivateName;
- ClasseProperty instead of PropertyDefinition when key.type=Identifier;
- PrivateName instead of PrivateIdentifier;
- TSQualifiedName instead of MemberExpression in TSInterfaceHeritage;
- TSDeclaredMethod with abstract=true instead of TSAbstractMethodDefinition;
- extra.parenthesized=true instead of TSParenthesizedType;
- etc...
estree-to-babel aims to smooth this differences.
```
npm i estree-to-babel
`js
import {parse} from 'cherow';
import {estreeToBabel} from 'estree-to-babel';
import {traverse} from '@babel/traverse';
const ast = estreeToBabel(parse(
const f = ({a}) => a;));
traverse({
ObjectProperty(path) {
console.log(path.value.name);
// output
'a';
},
});
`
You can provide options:
`js
import * as cherow from 'cherow';
import {estreeToBabel} from 'estree-to-babel';
import traverse from '@babel/traverse';
const options = {
convertParens: false,
};
const ast = estreeToBabel(cherow.parse(
(a = b)), options);
traverse({
AssignmentExpression(path) {
console.log(path.parentPath.type);
// output
'ParenthesizedExpression';
},
});
``
MIT