Gathering detailed insights and metrics for babel-walk
Gathering detailed insights and metrics for babel-walk
Gathering detailed insights and metrics for babel-walk
Gathering detailed insights and metrics for babel-walk
npm install babel-walk
90.1
Supply Chain
99.5
Quality
79.4
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
44 Stars
15 Commits
7 Forks
4 Watching
6 Branches
7 Contributors
Updated on 29 Oct 2024
TypeScript (99.15%)
JavaScript (0.85%)
Cumulative downloads
Total Downloads
Last day
-4%
233,837
Compared to previous day
Last week
0.7%
1,297,367
Compared to previous week
Last month
5.2%
5,467,602
Compared to previous month
Last year
19.1%
64,350,705
Compared to previous year
1
6
Lightweight AST traversal tools for Babel ASTs.
Babel supplies the wonderful babel-traverse module for walking Babel 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, babel-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.
1$ npm install babel-walk
1var walk = require('babel-walk');
Do a simple walk over the AST. node
should be the AST node to walk, and 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
.
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 the result of calling walk.simple(visitors)
and communicate state leveraging the state
parameter.
All babel-types aliases (e.g. Expression
) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) does not.
Do a simple walk over the AST, but memoizing the ancestors of the node and making them available to the visitors. node
should be the AST node to walk, and 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
.
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 the result of calling walk.ancestor(visitors)
and communicate state leveraging the state
parameter.
All babel-types aliases (e.g. Expression
) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) does not.
Do a recursive walk over the AST, where the visitors are responsible for continuing the walk on the child nodes of their target node. node
should be the AST node to walk, and 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.
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 the result of calling walk.recursive(visitors)
and communicate state leveraging the state
parameter.
Unlike other babel-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
) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}
) does not.
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 'babel'; 3import * as walk from 'babel-walk'; 4 5const visitors = walk.recursive({ 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 visitors(node, 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
For those of you migrating from Acorn to Babel, there are a few things to be aware of.
The visitor caching suggestions do not apply to Acorn's walk module, but do for babel-walk.
babel-walk does not provide any of the other functions Acorn's walk module provides (e.g. make
, findNode*
).
babel-walk does not use a base
variable. The walker algorithm is the same as what babel-traverse uses.
property
property of a non-computed MemberExpression
, are walked by babel-walk.MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
7 existing vulnerabilities detected
Details
Reason
Found 2/15 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
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