Skip to content

Commit

Permalink
visit: simplify handling of root node (#3461)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jan 23, 2022
1 parent a91fdc6 commit f2ecc28
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/language/visitor.ts
Expand Up @@ -192,12 +192,11 @@ export function visit(
let keys: any = [root];
let index = -1;
let edits = [];
let node: any = undefined;
let node: any = root;
let key: any = undefined;
let parent: any = undefined;
const path: any = [];
const ancestors = [];
let newRoot = root;
/* eslint-enable no-undef-init */

do {
Expand Down Expand Up @@ -237,15 +236,13 @@ export function visit(
edits = stack.edits;
inArray = stack.inArray;
stack = stack.prev;
} else {
key = parent ? (inArray ? index : keys[index]) : undefined;
node = parent ? parent[key] : newRoot;
} else if (parent) {
key = inArray ? index : keys[index];
node = parent[key];
if (node === null || node === undefined) {
continue;
}
if (parent) {
path.push(key);
}
path.push(key);
}

let result;
Expand Down Expand Up @@ -300,10 +297,11 @@ export function visit(
} while (stack !== undefined);

if (edits.length !== 0) {
newRoot = edits[edits.length - 1][1];
// New root
return edits[edits.length - 1][1];
}

return newRoot;
return root;
}

/**
Expand Down

0 comments on commit f2ecc28

Please sign in to comment.