Skip to content

coderaiser/estree-to-babel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

97279aa · Feb 9, 2025
Nov 6, 2024
Feb 9, 2025
Feb 9, 2025
Nov 26, 2021
Dec 11, 2023
Jul 27, 2024
Jul 27, 2024
Jun 19, 2023
Feb 9, 2025
Dec 29, 2018
Feb 9, 2025
Jul 27, 2024
Jan 11, 2025
Feb 9, 2025
Nov 6, 2024

Repository files navigation

Estree-to-babel NPM version Build Status Coverage Status

Convert ESTree-compatible JavaScript AST to Babel AST.

To use parsers like:

With babel tools like:

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.

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';
    },
});

You can provide options:

const cherow = require('cherow');
const toBabel = require('estree-to-babel');
const traverse = require('@babel/traverse').default;

const options = {
    convertParens: false,
};

const ast = toBabel(cherow.parse(`
    (a = b)
`), options);

traverse({
    AssignmentExpression(path) {
        console.log(path.parentPath.type);
        // output
        'ParenthesizedExpression';
    },
});

License

MIT