node-source-walk
Advanced tools
Comparing version 4.3.0 to 5.0.0
35
index.js
@@ -0,3 +1,9 @@ | ||
'use strict'; | ||
const parser = require('@babel/parser'); | ||
function isObject(str) { | ||
return typeof str === 'object' && !Array.isArray(str) && str !== null; | ||
} | ||
/** | ||
@@ -15,3 +21,3 @@ * @param {Object} options - Options to configure parser | ||
this.options = Object.assign({ | ||
this.options = { | ||
plugins: [ | ||
@@ -38,4 +44,5 @@ 'jsx', | ||
allowHashBang: true, | ||
sourceType: 'module' | ||
}, options); | ||
sourceType: 'module', | ||
...options | ||
}; | ||
@@ -71,3 +78,3 @@ // We use global state to stop the recursive traversal of the AST | ||
for (const key of node) { | ||
if (key !== null) { | ||
if (isObject(key)) { | ||
// Mark that the node has been visited | ||
@@ -78,12 +85,14 @@ key.parent = node; | ||
} | ||
} else if (node && typeof node === 'object') { | ||
} else if (node && isObject(node)) { | ||
cb(node); | ||
// TODO switch to Object.entries | ||
for (const key of Object.keys(node)) { | ||
for (const [key, value] of Object.entries(node)) { | ||
// Avoid visited nodes | ||
if (key === 'parent' || !node[key]) continue; | ||
if (key === 'parent' || !value) continue; | ||
node[key].parent = node; | ||
this.traverse(node[key], cb); | ||
if (isObject(value)) { | ||
value.parent = node; | ||
} | ||
this.traverse(value, cb); | ||
} | ||
@@ -103,3 +112,3 @@ } | ||
const ast = typeof src === 'object' ? src : this.parse(src); | ||
const ast = isObject(src) ? src : this.parse(src); | ||
@@ -112,5 +121,3 @@ this.traverse(ast, cb); | ||
if (typeof node !== 'object') { | ||
throw new Error('node must be an object'); | ||
} | ||
if (!isObject(node)) throw new Error('node must be an object'); | ||
@@ -117,0 +124,0 @@ reverseTraverse.call(this, node, cb); |
{ | ||
"name": "node-source-walk", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"description": "Execute a callback on every node of a source code's AST and stop walking when you see fit", | ||
@@ -12,3 +12,4 @@ "main": "index.js", | ||
"mocha": "mocha", | ||
"test": "npm run lint && npm run mocha" | ||
"test": "npm run lint && npm run mocha", | ||
"test:ci": "c8 npm run mocha" | ||
}, | ||
@@ -35,3 +36,3 @@ "repository": { | ||
"engines": { | ||
"node": ">=6.0" | ||
"node": ">=12" | ||
}, | ||
@@ -42,6 +43,7 @@ "dependencies": { | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.2.3", | ||
"sinon": "^7.5.0" | ||
"c8": "^7.11.0", | ||
"eslint": "^8.10.0", | ||
"mocha": "^9.2.1", | ||
"sinon": "^13.0.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
### node-source-walk [![CI](https://github.com/dependents/node-source-walk/actions/workflows/ci.yml/badge.svg)](https://github.com/dependents/node-source-walk/actions/workflows/ci.yml) [![npm](https://img.shields.io/npm/v/node-source-walk)](https://www.npmjs.com/package/node-source-walk) [![npm](https://img.shields.io/npm/dm/node-source-walk)](https://www.npmjs.com/package/node-source-walk) | ||
### node-source-walk [![CI](https://img.shields.io/github/workflow/status/dependents/node-source-walk/CI/main?label=CI&logo=github)](https://github.com/dependents/node-source-walk/actions/workflows/ci.yml?query=branch%3Amain) [![npm](https://img.shields.io/npm/v/node-source-walk)](https://www.npmjs.com/package/node-source-walk) [![npm](https://img.shields.io/npm/dm/node-source-walk)](https://www.npmjs.com/package/node-source-walk) | ||
@@ -39,3 +39,3 @@ > Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit. | ||
* The supplied options are passed through to the parser, so you can configure it according to babylon's documentation: https://github.com/babel/babylon | ||
* The supplied options are passed through to the parser, so you can configure it according to @babel/parser's [documentation](https://babeljs.io/docs/en/babel-parser.html). | ||
@@ -57,30 +57,29 @@ ### Swap out the parser | ||
`walk(src, cb)` | ||
#### `walk(src, cb)` | ||
* Recursively walks the given `src` from top to bottom | ||
* `src`: the contents of a file **OR** its (already parsed) AST | ||
* `cb`: a function that is called for every visited node. | ||
* The argument passed to `cb` will be the currently visited node. | ||
* `cb`: a function that is called for every visited node | ||
* The argument passed to `cb` will be the currently visited node. | ||
`moonwalk(node, cb)` | ||
#### `moonwalk(node, cb)` | ||
* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse`. | ||
* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse` | ||
* `node`: a valid AST node | ||
* `cb`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively). | ||
* The argument passed to `cb` will be the currently visited node. | ||
* `cb`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively) | ||
* The argument passed to `cb` will be the currently visited node. | ||
`stopWalking()` | ||
#### `stopWalking()` | ||
* Halts further walking of the AST until another manual call of `walk` or `moonwalk`. | ||
* Halts further walking of the AST until another manual call of `walk` or `moonwalk` | ||
* This is super-beneficial when dealing with large source files (or ASTs) | ||
`traverse(node, cb)` | ||
#### `traverse(node, cb)` | ||
* Allows you to traverse an AST node and execute a callback on it | ||
* Callback should expect the first argument to be an AST node, similar to `walk`'s callback. | ||
* Callback should expect the first argument to be an AST node, similar to `walk`'s callback | ||
`parse(src)` | ||
#### `parse(src)` | ||
* Uses the options supplied to Walker to parse the given source code string and return its AST | ||
using the configured parser (or babylon by default). | ||
* Uses the options supplied to Walker to parse the given source code string and return its AST using the configured parser (or @babel/parser by default). | ||
@@ -87,0 +86,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8413
116
4
87