Skip to content

Commit cdd9b1b

Browse files
committed
perf(linter/plugins): speed up SourceCode#getAncestors (#14747)
Optimize `SourceCode#getAncestors`: * Remove temp var `ancestor`. * Reduce code repetition. * Check for end of loop with `=== null` (instead of truthiness test).
1 parent 4283fd8 commit cdd9b1b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,10 @@ export type SourceCode = typeof SOURCE_CODE;
520520
function getAncestors(node: Node): Node[] {
521521
const ancestors = [];
522522

523-
for (
524-
let ancestor = (node as unknown as { parent: Node }).parent;
525-
ancestor;
526-
ancestor = (ancestor as unknown as { parent: Node }).parent
527-
) {
528-
ancestors.push(ancestor);
523+
while (true) {
524+
node = (node as unknown as { parent: Node }).parent;
525+
if (node === null) break;
526+
ancestors.push(node);
529527
}
530528

531529
return ancestors.reverse();

0 commit comments

Comments
 (0)