Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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
;CallExpression
whose callee is an Import
node instead of ImportExpression
;OptionalMemberExpression
and OptionalCallExpression
instead of ChainExpression
;ImportDeclaration
and ExportNamedDeclaration
has attributes and
phase`;Also @babel/parser
has differences with typescript-estree
:
TSExpressionWithTypeArguments
instead of TSClassImplements
;ClassPrivateProperty
instead of PropertyDefinition
when key.type=PrivateName
;ClasseProperty
instead of PropertyDefinition
when key.type=Identifier
;PrivateName
instead of PrivateIdentifier
;TSInterfaceHeritage
instead of TSExpressionWithTypeArguments
;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 1,113,038 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 1 open source maintainer 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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.