estraverse
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Estraverse is a popular JavaScript library used for traversing ECMAScript syntax trees (ASTs). It originates from the esmangle project. This library provides functions for effectively navigating and manipulating ECMAScript ASTs, aiding developers in software tasks like code analysis and transformation.
Estraverse boasts an intuitive and flexible interface that developers can easily use in their code. A common use case is traversing an AST and acting upon certain node types:
estraverse.traverse(ast, {
enter: function (node, parent) {
if (node.type == 'FunctionExpression' || node.type == 'FunctionDeclaration')
return estraverse.VisitorOption.Skip;
},
leave: function (node, parent) {
if (node.type == 'VariableDeclarator')
console.log(node.id.name);
}
});
In the code block above, the traverse
method is used to navigate through the nodes of the AST (represented by ast
). The enter
function is executed when entering each node, while the leave
function is invoked when exiting. By returning the VisitorOption.Skip
value in enter
when a FunctionExpression
or FunctionDeclaration
is encountered, the traversal skips over those nodes and their children.
Estraverse also has a replace
function for easy tree manipulation:
result = estraverse.replace(tree, {
enter: function (node) {
if (node.type === 'Literal')
return replaced;
}
});
In this example, all nodes of type Literal
are replaced with the value of the replaced
variable.
Lastly, comprehensive documentation for Estraverse is available on the wiki page of the project's GitHub repository. Here you can find detailed usage guides and further examples on how to harness the functionality of the library in your projects.