estree-to-babel
estree-to-babel copied to clipboard
convert estree ast to babel
Estree-to-babel

Convert ESTree-compatible JavaScript AST to Babel AST.
To use parsers like:
With babel tools like:
@babel/traverse@babel/types- etc...
The thing is @babel/parser has a little differences with estree standard:
PropertyofObjectExpressionandObjectPatterncalledObjectProperty;FunctionExpressionof aPropertylocated inObjectMethodnode;Filenode;StringLiteral,NumericLiteral,NullLiteral,RegExpLiteral,BooleanLiteralinstead ofLiteral;ClassMethodinstead ofMethodDefinition;ClassPrivateMethod;ClassPrivateNamestores name asIdentifierinidfield;ClassPrivatePropertyinstead ofFieldDefinition;OptionalMemberExpressionandOptionalCallExpressioninstead ofChainExpression;ImportDeclarationandExportNamedDeclarationhasattributes;- etc...
Also @babel/parser has differences with typescript-estree:
TSExpressionWithTypeArgumentsinstead ofTSClassImplements;ClassPrivatePropertyinstead ofPropertyDefinitionwhenkey.type=PrivateName;ClassePropertyinstead ofPropertyDefinitionwhenkey.type=Identifier;PrivateNameinstead ofPrivateIdentifier;TSInterfaceHeritageinstead ofTSExpressionWithTypeArguments;TSQualifiedNameinstead ofMemberExpressioninTSInterfaceHeritage;TSDeclaredMethodwithabstract=trueinstead ofTSAbstractMethodDefinition;- etc...
estree-to-babel aims to smooth this differences.
Install
npm i estree-to-babel
Example
const cherow = require('cherow');
const toBabel = require('estree-to-babel');
const traverse = require('@babel/traverse').default;
const ast = toBabel(cherow.parse(`
const f = ({a}) => a;
`));
traverse({
ObjectProperty(path) {
console.log(path.value.name);
// output
'a';
},
});
License
MIT