Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to a class #33

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ function isObject(str) {
return typeof str === 'object' && !Array.isArray(str) && str !== null;
}

module.exports = class NodeSourceWalk {
/**
* @param {Object} options - Options to configure parser
* @param {Object} options.parser - An object with a parse method that returns an AST
*/
module.exports = function(options = {}) {
constructor(options = {}) {
this.parser = options.parser || parser;

if (options.parser) {
Expand Down Expand Up @@ -46,14 +47,14 @@ module.exports = function(options = {}) {

// We use global state to stop the recursive traversal of the AST
this.shouldStop = false;
};
}

/**
* @param {String} src
* @param {Object} [options] - Parser options
* @return {Object} The AST of the given src
*/
module.exports.prototype.parse = function(src, options) {
parse(src, options) {
options = options || this.options;

// Keep around for consumers of parse that supply their own options
Expand All @@ -62,13 +63,13 @@ module.exports.prototype.parse = function(src, options) {
}

return this.parser.parse(src, options);
};
}

/**
* Adapted from substack/node-detective
* Executes cb on a non-array AST node
*/
module.exports.prototype.traverse = function(node, cb) {
traverse(node, cb) {
if (this.shouldStop) return;

if (Array.isArray(node)) {
Expand All @@ -93,7 +94,7 @@ module.exports.prototype.traverse = function(node, cb) {
this.traverse(value, cb);
}
}
};
}

/**
* Executes the passed callback for every traversed node of
Expand All @@ -102,23 +103,30 @@ module.exports.prototype.traverse = function(node, cb) {
* @param {String|Object} src - The source code or AST to traverse
* @param {Function} cb - Called for every node
*/
module.exports.prototype.walk = function(src, cb) {
walk(src, cb) {
this.shouldStop = false;

const ast = isObject(src) ? src : this.parse(src);

this.traverse(ast, cb);
};
}

module.exports.prototype.moonwalk = function(node, cb) {
moonwalk(node, cb) {
this.shouldStop = false;

if (!isObject(node)) throw new Error('node must be an object');

reverseTraverse.call(this, node, cb);
};
this._reverseTraverse(node, cb);
}

function reverseTraverse(node, cb) {
/**
* Halts further traversal of the AST
*/
stopWalking() {
this.shouldStop = true;
}

_reverseTraverse(node, cb) {
if (this.shouldStop || !node.parent) return;

if (Array.isArray(node.parent)) {
Expand All @@ -129,12 +137,6 @@ function reverseTraverse(node, cb) {
cb(node.parent);
}

reverseTraverse.call(this, node.parent, cb);
this._reverseTraverse(node.parent, cb);
}

/**
* Halts further traversal of the AST
*/
module.exports.prototype.stopWalking = function() {
this.shouldStop = true;
};