Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
node-source-walk
Advanced tools
Execute a callback on every node of a source code's AST and stop walking when you see fit
The node-source-walk package is a tool for traversing the abstract syntax tree (AST) of JavaScript code. It allows developers to analyze and manipulate the source code by providing an easy way to walk through the tree structure generated from the code.
Walking the AST of JavaScript code
This feature allows you to walk through the AST of a given JavaScript code snippet and perform actions based on the type of node encountered. In the code sample, the function logs a message whenever it encounters a variable declaration.
const walk = require('node-source-walk');
const sourceCode = 'var x = 1; console.log(x);';
walk(sourceCode, function(node) {
if (node.type === 'VariableDeclaration') {
console.log('Found a variable declaration!');
}
});
Support for different parsers
node-source-walk supports various parsers like Esprima, Acorn, and Babel. This allows for flexibility in handling different JavaScript syntaxes, including experimental features and JSX. The code sample demonstrates how to specify a custom parser and its options.
const walk = require('node-source-walk');
const babelParser = require('@babel/parser');
const sourceCode = 'let x = 1;';
walk(sourceCode, {
parser: babelParser,
parserOptions: {
sourceType: 'module',
plugins: ['jsx']
}
}, function(node) {
// Analyze the node
});
Estraverse is a simple and flexible library for traversing and manipulating the AST of ECMAScript 5.1. It is similar to node-source-walk but focuses on ECMAScript 5.1 and does not have built-in support for JSX or experimental JavaScript features.
Acorn-walk is a tiny, fast JavaScript parser, built on Acorn. It provides a simple interface for walking the AST and is similar to node-source-walk but is tightly coupled with the Acorn parser.
Babel-traverse is part of the Babel toolchain and provides the ability to traverse and update the AST. It is more powerful and complex than node-source-walk, with a focus on transforming code to work with Babel's plugin system.
Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit.
npm install node-source-walk
const Walker = require('node-source-walk');
const walker = new Walker();
// Assume src is the string contents of myfile.js
// or the AST of an outside parse of myfile.js
walker.walk(src, node => {
if (node.type === whateverImLookingFor) {
// No need to keep traversing since we found what we wanted
walker.stopWalking();
}
});
By default, Walker will use @babel/parser
(supporting ES6, JSX, Flow, and all other available @babel/parser
plugins) and the sourceType: module
, but you can change any of the defaults as follows:
const walker = new Walker({
sourceType: 'script',
// If you don't like experimental plugins
plugins: [
'jsx',
'flow'
]
});
@babel/parser
's documentation.If you want to supply your own parser, you can do:
const walker = new Walker({
parser: mySweetParser
});
.parse
method that takes in a string and returns an object/AST.walk(src, callback)
src
from top to bottomsrc
: the contents of a file or its (already parsed) ASTcallback
: a function that is called for every visited node
callback
will be the currently visited node.moonwalk(node, callback)
walk
and traverse
node
: a valid AST nodecallback
: a function that is called for every node (specifically via visiting the parent(s) of every node recursively)
callback
will be the currently visited node.stopWalking()
walk
or moonwalk
traverse(node, callback)
walk
's callbackparse(src)
@babel/parser
by default).FAQs
Execute a callback on every node of a source code's AST and stop walking when you see fit
The npm package node-source-walk receives a total of 884,515 weekly downloads. As such, node-source-walk popularity was classified as popular.
We found that node-source-walk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.