Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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
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
;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
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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.