Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
estree-to-babel
Advanced tools
The estree-to-babel npm package is a utility that converts ESTree-compatible AST (Abstract Syntax Tree) into a format that is compatible with Babel. This is particularly useful for developers working on tools that manipulate JavaScript code, such as compilers or code formatters, where they need to integrate with Babel's ecosystem.
Convert ESTree AST to Babel-compatible AST
This feature allows the conversion of an AST in ESTree format to a Babel-compatible format. The code sample demonstrates how to convert a simple ESTree AST representing the number 42 into a Babel AST.
const estreeToBabel = require('estree-to-babel');
const estreeAst = {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 42,
raw: '42'
}
}]
};
const babelAst = estreeToBabel(estreeAst);
console.log(babelAst);
This package contains all of the Babel types constructors used for creating ASTs. It is similar to estree-to-babel in that it deals with AST manipulations, but it is more about creation rather than conversion.
Recast provides the ability to parse JavaScript code, manipulate the resulting AST, and generate new code. It uses a different AST format by default but can be configured to use the ESTree format, making it somewhat similar to estree-to-babel in terms of manipulating ASTs but with broader functionality including parsing and code generation.
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;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
;estree-to-babel
aims to smooth this differences.
npm i estree-to-babel
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';
},
});
MIT
FAQs
convert estree ast to babel
The npm package estree-to-babel receives a total of 904,743 weekly downloads. As such, estree-to-babel popularity was classified as popular.
We found that estree-to-babel demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.