Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
ast-traverse
Advanced tools
Simple but flexible AST traversal with pre and post visitors. Works in node and browsers.
// ast is a Mozilla Parser API compatible structure
// generated by Esprima or another parser
var ast = require("esprima").parse("f(1, x) + 2");
var traverse = require("ast-traverse");
// print AST node types, pre-order (node first, then its children)
traverse(ast, {pre: function(node, parent, prop, idx) {
console.log(node.type + (parent ? " from parent " + parent.type +
" via " + prop + (idx !== undefined ? "[" + idx + "]" : "") : ""));
}});
console.log();
/*
=>
Program
ExpressionStatement from parent Program via body[0]
BinaryExpression from parent ExpressionStatement via expression
CallExpression from parent BinaryExpression via left
Identifier from parent CallExpression via callee
Literal from parent CallExpression via arguments[0]
Identifier from parent CallExpression via arguments[1]
Literal from parent BinaryExpression via right
*/
// you can also visit post-order, or both
// all four arguments are provided to both visitors (left out unused below)
var indent = 0;
traverse(ast, {
pre: function(node) {
console.log(Array(indent + 1).join(" ") + node.type);
indent += 4;
},
post: function() {
indent -= 4;
}
});
console.log();
/*
=>
Program
ExpressionStatement
BinaryExpression
CallExpression
Identifier
Literal
Identifier
Literal
*/
// return false from the pre-visitor to skip traversing its children
// throw an exception to abort traversal
// by default node property names beginning with $ are skipped
// but you can supply your own skipProperty function instead
traverse(ast, {
pre: function(node) {
console.log(node.type);
},
skipProperty: function(prop, node) {
return prop === "parent" || prop === "expression";
}
});
/*
=>
Program
ExpressionStatement
*/
Install using npm
npm install ast-traverse
var traverse = require("ast-traverse");
Clone the repo and include it in a script tag
git clone https://github.com/olov/ast-traverse.git
<script src="ast-traverse/ast-traverse.js"></script>
FAQs
simple but flexible AST traversal with pre and post visitors
The npm package ast-traverse receives a total of 201,712 weekly downloads. As such, ast-traverse popularity was classified as popular.
We found that ast-traverse demonstrated a not healthy version release cadence and project activity because the last version was released 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.