What is astw?
The astw package is a tool for walking through the abstract syntax tree (AST) of JavaScript code. It allows developers to analyze and manipulate code by traversing its AST, which is a tree representation of the code structure.
What are astw's main functionalities?
AST Walking
This feature allows you to walk through the AST of a JavaScript code snippet. The code sample demonstrates how to parse a JavaScript function into an AST using Esprima, and then walk through each node of the AST using astw, logging the type of each node.
const astw = require('astw');
const parse = require('esprima').parse;
const src = 'function add(a, b) { return a + b; }';
const ast = parse(src);
const walk = astw(ast);
walk(function (node) {
console.log(node.type);
});
Other packages similar to astw
estraverse
Estraverse is a package for ECMAScript/JavaScript traversal and manipulation. It provides more comprehensive traversal and manipulation capabilities compared to astw, including the ability to replace nodes and manage traversal state.
acorn-walk
Acorn-walk is a utility for walking through the ASTs produced by the Acorn parser. It is similar to astw but is specifically designed to work with Acorn's AST format, offering more detailed control over the traversal process.
babel-traverse
Babel-traverse is part of the Babel toolchain and is used for traversing and transforming ASTs. It offers a rich API for AST manipulation, making it more powerful and flexible than astw, especially in the context of Babel's ecosystem.
astw
walk the ast
This module is a faster version of
falafel
that only does ast walking and .parent
tracking, not source transforms.
example
var astw = require('astw');
var deparse = require('escodegen').generate;
var walk = astw('4 + beep(5 * 2)');
walk(function (node) {
var src = deparse(node);
console.log(node.type + ' :: ' + JSON.stringify(src));
});
methods
var astw = require('astw')
var walk = astw(src)
Return a walk()
function from the source string or ast object src
.
walk(cb)
Walk the nodes in the ast with cb(node)
where node
is each element in the
ast from esprima but with an additional .parent
reference to the parent node.
install
With npm do:
npm install astw
license
MIT