10.5.0 • Published 5 months ago

estree-to-babel v10.5.0

Weekly downloads
6,581
License
MIT
Repository
github
Last release
5 months ago

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

10.4.0

5 months ago

10.5.0

5 months ago

10.2.0

6 months ago

10.3.0

6 months ago

10.1.0

6 months ago

10.0.1

8 months ago

10.0.0

9 months ago

9.1.0

12 months ago

9.0.0

2 years ago

8.1.0

2 years ago

8.1.1

2 years ago

7.0.0

2 years ago

8.0.0

2 years ago

5.2.0

2 years ago

5.1.0

2 years ago

6.0.0

2 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.9.0

3 years ago

4.8.0

4 years ago

4.5.0

4 years ago

4.4.0

4 years ago

4.7.0

4 years ago

4.6.0

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.1.1

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.1.0

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago