Gathering detailed insights and metrics for sl-esprima-ast-utils
Gathering detailed insights and metrics for sl-esprima-ast-utils
Gathering detailed insights and metrics for sl-esprima-ast-utils
Gathering detailed insights and metrics for sl-esprima-ast-utils
Utility library to manipulate, transform, query and debug esprima ASTs.
npm install sl-esprima-ast-utils
Typescript
Module System
Node Version
NPM Version
90
Supply Chain
99.6
Quality
74.2
Maintenance
100
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
4,698,331
Last Day
484
Last Week
14,984
Last Month
99,376
Last Year
3,777,677
NOASSERTION License
30 Stars
16 Commits
8 Forks
3 Watchers
1 Branches
2 Contributors
Updated on Sep 16, 2021
Minified
Minified + Gzipped
Latest Version
0.0.7
Package Id
sl-esprima-ast-utils@0.0.7
Size
15.99 kB
NPM Version
3.10.10
Node Version
6.10.3
Published on
Dec 12, 2017
Cumulative downloads
Total Downloads
Last Day
51.2%
484
Compared to previous day
Last Week
8.1%
14,984
Compared to previous week
Last Month
-30.4%
99,376
Compared to previous month
Last Year
516.1%
3,777,677
Compared to previous year
3
4
Node module to manipulate, transform, query and debug esprima ASTs.
When you edit esprima AST and go back to code with escodegen you lose too much information because primary you don't keep track of ranges, tokens, comments etc. esprima-ast-utils do this for you, so no escodegen is needed, you can edit the AST directly and code everything is in sync.
parse
(String:str [, Boolean:debug]) -> ObjectParse given str
Parameters:
str
debug
: display $id, $parent and $code in console.log (enumerable=true)
Returns:
Object
Note: location it's not supported, and won't sync with changes, range/rokens do.
parseWrap
(String:str [, Boolean:debug]) -> ObjectWrap your code into a function and parse given str.
Needed if your code contains a ReturnStatement
at Program level.
Parameters:
str
debug
: display $id, $parent and $code in console.log (enumerable=true)
Returns:
Object
Note: location it's not supported, and won't sync with changes, range/rokens do.
parseFile
(String:file [, Boolean:debug]) -> ObjectParse given file
Parameters:
file
: Path
debug
: display $id, $parent and $code in console.log (enumerable=true)
Returns:
Object
Note: : NodeJS only
encode
(Object:tree) -> StringReturn tree.$code, just for API completeness.
Parameters:
tree
Returns:
String
traverse
(Object:node, Function:callback [, Number:depth] [, Boolean:recursive])traverse AST
Parameters:
node
callback
: function(node, parent, property, index, depth)
You can return false
to stop traverse
depth
: (0) current depth
recursive
: (true) recursively traverse
parentize
(Object:root [, Boolean:debug])traverse
AST and set $parent node
Parameters:
root
debug
: display $parent in console.log (enumerable=true)
idze
(Object:node [, Boolean:debug])traverse
AST and set an unique $id
to every node
Parameters:
node
debug
: display $id in console.log (enumerable=true)
attachComments
(Object:root)Traverse the AST and add comments as nodes, so you can query them. Loop thought comments and find a proper place to inject (BlockStament or alike)
Parameters:
root
filter
(Object:node, Function:callback [, Function:traverse_fn]) -> Arraytraverse
and filter
given AST based on given callback
Parameters:
node
callback
traverse_fn
Returns:
Array
: Every match of the callback
getParent
(Object:node, Function:callback) -> Object|NULLGet parent node based on given callback, stops on true
Parameters:
node
callback
Returns:
Object|NULL
getRoot
(Object:node) -> Objectget the root of the AST
Parameters:
node
Returns:
Object
clone
(Object:node) -> ObjectRecursive clone a node. Do no include "$" properties like $parent or $id
If you want those, call parentize
- idze
after cloning
Parameters:
node
Returns:
Object
debug_tree
(Object:tree [, Number:max_width] [, Boolean:display_code_in_tree])Show your tree in various ways to easy debug Big trees will be always a pain, so keep it small if possible
Parameters:
tree
: Any node, if root tokens & source will be displayed
max_width
: max tokens per line
display_code_in_tree
: when display the tree attach the code on the right
getFunction
(Object:node, String:fn_name) -> Object|NULLfilter
the AST and return the function with given name, null otherwise.
Parameters:
node
fn_name
Returns:
Object|NULL
getFunctionBlock
(Object:node, String:fn_name) -> Object|NULLfilter
the AST and return the function > block with given name, null otherwise.
Parameters:
node
fn_name
Returns:
Object|NULL
isFunctionDeclared
(Object:node, String:fn_name) -> Booleanshortcut
Parameters:
node
fn_name
Returns:
Boolean
hasVarDeclaration
(Object:node, String:var_name) -> Booleanshortcut
Parameters:
node
var_name
Returns:
Boolean
isVarDeclared
(Object:node, String:var_name) -> Booleanreverse from node to root and look for a Variable declaration
Parameters:
node
var_name
Returns:
Boolean
Note: It's not perfect because VariableDeclaration
it's not hoisted
contains
(Object:node, Object:subnode) -> Booleannode
constains subnode
Parameters:
node
subnode
Returns:
Boolean
hasBody
(Object:node) -> BooleanHas a body property, use to freely attach/detach
Parameters:
node
Returns:
Boolean
isComment
(Object:node) -> Booleanshortcut: Is a comment (Line or Block) and has text
Parameters:
node
Returns:
Boolean
getComment
(Object:node, String:comment) -> Objectshortcut: search for a comment (trim it's content for maximum compatibility)
Parameters:
node
comment
Returns:
Object
getCode
(Object:node) -> Stringshortcut: Return node code
Parameters:
node
Returns:
String
getArgumentList
(Object:node) -> ArrayReturn FunctionDeclaration
arguments name as a list
Parameters:
node
Returns:
Array
getDefaultProperty
(Object:node)Parameters:
node
attach
(Object:node, String:property, Number|NULL:position, String|Object:str)Attach Code/Program to given node.
Parameters:
node
: node to attach
property
: Where attach, could be an array or an object
position
: index if an array is used as target property
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
Note: tokens are updated
Note: range is updated
Note: comments are not attached to root.comments (invalid-comments)
attachPunctuator
(Object:tree, String:punctuator, Number:position) -> StringAttach a punctuator and keep the tree ranges sane. The Punctuator can be anything... be careful!
Parameters:
tree
punctuator
position
Returns:
String
: detached code stringNote: The Punctuator is not parsed and could be assigned to nearest literal or alike.
detach
(Object:node, String:property) -> StringDetach given node from it's parent
Parameters:
node
property
Returns:
String
: detached code stringNote: node.$parent
is set to null
, remember to save it first if you need it.
attachAfter
(Object:node, String|Object:str [, String:property])Attach after node, that means node.$parent.type
is a BockStament
Parameters:
node
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
property
: where to search node in the parent
attachBefore
(Object:node, String|Object:str)Attach before node, that means node.$parent.type
is a BockStament
Parameters:
node
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
attachAfterComment
(Object:node, String:comment, String|Object:str) -> BooleanShortcut: Search for given comment, and attachAfter
Parameters:
node
comment
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
Returns:
Boolean
: successreplace
(Object:node, String|Object:str)Shortcut: detach/attach
Parameters:
node
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
replaceComment
(Object:node, String:comment, String|Object:str)Shortcut: Search for a comment and replace
Parameters:
node
comment
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
injectCode
(Object:tree, Array:range, String|Object:str, Boolean:debug)Inject code directly intro the given range.
After the injection the code will be parsed again so original $id
will be lost
Parameters:
tree
range
str
: String is preferred if not possible remember that only Program can be attached, you may consider using toProgram
debug
: display $id, $parent and $code in console.log (enumerable=true)
Note: this is dangerous and powerful
setIdentifier
(Object:node, String:new_name)rename Identifier
Parameters:
node
new_name
renameProperty
(Object:node, Object:replacements)traverse
and apply given replacements
Parameters:
node
replacements
Example:
1renameProperty(node, {"old_var": "new_var", "much_older": "shinnig_new"})
renameVariable
(Object:node, Object:replacements)traverse
and apply given replacements
Parameters:
node
replacements
Example:
1renameVariable(node, {"old_var": "new_var", "much_older": "shinnig_new"})
renameFunction
(Object:node, Object:replacements)traverse and apply given replacements
Parameters:
node
replacements
Example:
1renameFunction(node, {"old_var": "new_var", "much_older": "shinnig_new"})
toProgram
(Object|Array:node)Clone given node(s) and extract tokens & code from root to given you a Program-like attachable node
Parameters:
node
: if array is provided will add all nodes to program.bodygetToken
(Object:tree, Number:start, Number:end) -> Object|NULLGet token based on given range
Parameters:
tree
start
end
Returns:
Object|NULL
getTokens
(Object:tree, Number:start, Number:end) -> Array|NULLGet tokens in range
Parameters:
tree
start
end
Returns:
Array|NULL
pushTokens
(Object:tree, Number:start, Number:amount)Push tokens range from start
Parameters:
tree
start
amount
Note: Update nodes range
growTokens
(Object:tree, Number:start, Number:end, Number:amount)Grow tokens in given range
Parameters:
tree
start
end
amount
Note: Update nodes range
tokenAt
(Object:tree, Number:start) -> ObjectGet the first token
Parameters:
tree
start
Returns:
Object
addTokens
(Object:dst_tree, Object|Array:src, Number:start)Add src
tokens to dst
since start
(so keep the order)
Parameters:
dst_tree
src
start
Note: Remember to push src
tokens before addTokens
otherwise won't be synced
replaceCodeRange
(Object:tree, Array:range, String:new_text)Replace code range with given text.
Parameters:
tree
range
new_text
removeTokens
(Object:tree, Number:start, Number:end)Remove tokens in range and update ranges
Parameters:
tree
start
end
Note: Do not remove nodes.
(The MIT License)
Copyright (c) 2014 Luis Lafuente llafuente@noboxout.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/15 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-30
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