Skip to content

Commit

Permalink
Convert shouldStop from public to private field (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored May 4, 2023
1 parent f513c50 commit c65acde
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function isObject(str) {
}

module.exports = class NodeSourceWalk {
// We use global state to stop the recursive traversal of the AST
#shouldStop = false;

/**
* @param {Object} options - Options to configure parser
* @param {Object} options.parser - An object with a parse method that returns an AST
Expand Down Expand Up @@ -44,9 +47,6 @@ module.exports = class NodeSourceWalk {
sourceType: 'module',
...options
};

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

/**
Expand All @@ -70,7 +70,7 @@ module.exports = class NodeSourceWalk {
* Executes callback on a non-array AST node
*/
traverse(node, callback) {
if (this.shouldStop) return;
if (this.#shouldStop) return;

if (Array.isArray(node)) {
for (const key of node) {
Expand Down Expand Up @@ -104,15 +104,15 @@ module.exports = class NodeSourceWalk {
* @param {Function} callback - Called for every node
*/
walk(src, callback) {
this.shouldStop = false;
this.#shouldStop = false;

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

this.traverse(ast, callback);
}

moonwalk(node, callback) {
this.shouldStop = false;
this.#shouldStop = false;

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

Expand All @@ -123,11 +123,11 @@ module.exports = class NodeSourceWalk {
* Halts further traversal of the AST
*/
stopWalking() {
this.shouldStop = true;
this.#shouldStop = true;
}

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

if (Array.isArray(node.parent)) {
for (const parent of node.parent) {
Expand Down

0 comments on commit c65acde

Please sign in to comment.