Installations
npm install @wojtekmaj/babylon-walk
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 6.0.0
Score
69.2
Supply Chain
98.4
Quality
74.4
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
wojtekmaj
Download Statistics
Total Downloads
201,423
Last Day
4
Last Week
16
Last Month
53
Last Year
3,621
GitHub Statistics
17 Commits
2 Watching
2 Branches
1 Contributors
Package Meta Information
Latest Version
2.0.0
Package Id
@wojtekmaj/babylon-walk@2.0.0
Unpacked Size
30.94 kB
Size
7.48 kB
File Count
11
Total Downloads
Cumulative downloads
Total Downloads
201,423
Last day
0%
4
Compared to previous day
Last week
33.3%
16
Compared to previous week
Last month
82.8%
53
Compared to previous month
Last year
-96.5%
3,621
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
babylon-walk
Lightweight AST traversal tools for @babel/parser ASTs.
@babel/parser is the parser used by the Babel project, which supplies the wonderful babel-traverse module for walking Babylon ASTs. Problem is, babel-traverse is very heavyweight, as it is designed to supply utilities to make all sorts of AST transformations possible. For simple AST walking without transformation, babel-traverse brings a lot of overhead.
This module loosely implements the API of Acorn parser's walk module, which is a lightweight AST walker for the ESTree AST format.
In my tests, babylon-walk's ancestor walker (the most complex walker provided by this module) is about 8 times faster than babel-traverse, if the visitors are cached and the same AST is used for all runs. It is about 16 times faster if a fresh AST is used every run.
Getting started
Compatibility
Your project needs to use Node 6 or later.
Installation
Add babylon-walk to your project by executing npm install @wojtekmaj/babylon-walk
or yarn add @wojtekmaj/babylon-walk
.
Usage
Here's an example of basic usage:
1const walk = require('@wojtekmaj/babylon-walk');
User guide
walk.simple(node, visitors, state)
Do a simple walk over the AST.
When walk.simple
is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {}
to Visitor() { enter(node, state) {} }
). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state
parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state
variable, never as this
.)
All @babel/types aliases (e.g. Expression
) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) work.
Arguments
Argument name | Description |
---|---|
node | The AST node to walk. |
visitors | An object containing Babel visitors. Each visitor function will be called as (node, state) , where node is the AST node, and state is the same state passed to walk.simple . |
state | State. |
walk.ancestor(node, visitors, state)
Do a simple walk over the AST, but memoizing the ancestors of the node and making them available to the visitors.
When walk.ancestor
is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {}
to Visitor() { enter(node, state) {} }
). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state
parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state
variable, never as this
.)
All @babel/types aliases (e.g. Expression
) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) work.
Arguments
Argument name | Description |
---|---|
node | The AST node to walk. |
visitors | An object containing Babel visitors. Each visitor function will be called as (node, state, ancestors) , where node is the AST node, state is the same state passed to walk.ancestor , and ancestors is an array of ancestors to the node (with the outermost node being [0] and the current node being [ancestors.length - 1] ). If state is not specified in the call to walk.ancestor , the state parameter will be set to ancestors . |
state | State. |
walk.recursive(node, visitors, state)
Do a recursive walk over the AST, where the visitors are responsible for continuing the walk on the child nodes of their target node.
When walk.recursive
is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {}
to Visitor() { enter(node, state) {} }
). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state
parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state
variable, never as this
.)
Unlike other babylon-walk walkers, walk.recursive
does not call the exit
visitor, only the enter
(the default) visitor, of a specific node type.
All @babel/types aliases (e.g. Expression
) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) work.
Arguments
Argument name | Description |
---|---|
node | The AST node to walk. |
visitors | An object containing Babel visitors. Each visitor function will be called as (node, state, c) , where node is the AST node, state is the same state passed to walk.recursive , and c is a function that takes a single node as argument and continues walking that node. If no visitor for a node is provided, the default walker algorithm will still be used. |
state | State. |
Example
In the following example, we are trying to count the number of functions in the outermost scope. This means, that we can simply walk all the statements and increment a counter if it is a function declaration or expression, and then stop walking. Note that we do not specify a visitor for the Program
node, and the default algorithm for walking Program
nodes is used (which is what we want). Also of note is how I bring the visitors
object outside of countFunctions
so that the object can be cached to improve performance.
1import * as t from 'babel-types'; 2import { parse } from 'babylon'; 3import * as walk from '@wojtekmaj/babylon-walk'; 4 5const visitors = { 6 Statement(node, state, c) { 7 if (t.isVariableDeclaration(node)) { 8 for (let declarator of node.declarations) { 9 // Continue walking the declarator 10 c(declarator); 11 } 12 } else if (t.isFunctionDeclaration(node)) { 13 state.counter++; 14 } 15 }, 16 17 VariableDeclarator(node, state) { 18 if (t.isFunction(node.init)) { 19 state.counter++; 20 } 21 } 22}; 23 24function countFunctions(node) { 25 const state = { 26 counter: 0 27 }; 28 walk.recursive(node, visitors, state); 29 return state.counter; 30} 31 32const ast = parse(` 33 // Counts 34 var a = () => {}; 35 36 // Counts 37 function b() { 38 // Doesn't count 39 function c() { 40 } 41 } 42 43 // Counts 44 const c = function d() {}; 45`); 46 47countFunctions(ast); 48// = 3
Caveats
For those of you migrating from Acorn to Babylon, there are a few things to be aware of.
-
The visitor caching suggestions do not apply to Acorn's walk module, but do for babylon-walk.
-
babylon-walk does not provide any of the other functions Acorn's walk module provides (e.g.
make
,findNode*
). -
babylon-walk does not use a
base
variable. The walker algorithm is the same as what babel-traverse uses.- That means certain nodes that are not walked by Acorn, such as the
property
property of a non-computedMemberExpression
, are walked by babylon-walk.
- That means certain nodes that are not walked by Acorn, such as the
License
The MIT License.
Authors
Timothy Gu timothygu99@gmail.com | |
Wojciech Maj kontakt@wojtekmaj.pl http://wojtekmaj.pl |
Thank you
This project wouldn't be possible without awesome work of Timothy Gu timothygu99@gmail.com who created its initial version. Thank you!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
30 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-3gx7-xhv7-5mx3
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More