Skip to content

Commit f2ecc28

Browse files
visit: simplify handling of root node (#3461)
1 parent a91fdc6 commit f2ecc28

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/language/visitor.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,11 @@ export function visit(
192192
let keys: any = [root];
193193
let index = -1;
194194
let edits = [];
195-
let node: any = undefined;
195+
let node: any = root;
196196
let key: any = undefined;
197197
let parent: any = undefined;
198198
const path: any = [];
199199
const ancestors = [];
200-
let newRoot = root;
201200
/* eslint-enable no-undef-init */
202201

203202
do {
@@ -237,15 +236,13 @@ export function visit(
237236
edits = stack.edits;
238237
inArray = stack.inArray;
239238
stack = stack.prev;
240-
} else {
241-
key = parent ? (inArray ? index : keys[index]) : undefined;
242-
node = parent ? parent[key] : newRoot;
239+
} else if (parent) {
240+
key = inArray ? index : keys[index];
241+
node = parent[key];
243242
if (node === null || node === undefined) {
244243
continue;
245244
}
246-
if (parent) {
247-
path.push(key);
248-
}
245+
path.push(key);
249246
}
250247

251248
let result;
@@ -300,10 +297,11 @@ export function visit(
300297
} while (stack !== undefined);
301298

302299
if (edits.length !== 0) {
303-
newRoot = edits[edits.length - 1][1];
300+
// New root
301+
return edits[edits.length - 1][1];
304302
}
305303

306-
return newRoot;
304+
return root;
307305
}
308306

309307
/**

0 commit comments

Comments
 (0)