Skip to content

Commit 4461a37

Browse files
committed
using a reverse iterator instead of reversing the array
1 parent 9343225 commit 4461a37

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/slang-nodes/SlangNode.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ export function clearOffsets(): void {
2323
offsets.clear();
2424
}
2525

26-
function getLeadingOffset(children: Edge[]): number {
26+
function reversedIterator<T>(children: T[]): Iterable<T> {
27+
return {
28+
[Symbol.iterator](): Iterator<T> {
29+
let index = children.length;
30+
return {
31+
next: function (): IteratorResult<T, T> {
32+
index--;
33+
return { done: index < 0, value: children[index] };
34+
}
35+
};
36+
}
37+
};
38+
}
39+
40+
function getOffset(children: Edge[] | Iterable<Edge>): number {
2741
let offset = 0;
2842
for (const { node } of children) {
2943
if (node.isNonterminalNode() || !isCommentOrWhiteSpace(node)) {
@@ -114,7 +128,7 @@ export class SlangNode {
114128

115129
const [leadingOffset, trailingOffset] = enclosePeripheralComments
116130
? [0, 0]
117-
: [getLeadingOffset(children), getLeadingOffset(children.reverse())];
131+
: [getOffset(children), getOffset(reversedIterator(children))];
118132

119133
this.loc = {
120134
start: initialOffset + leadingOffset,
@@ -147,7 +161,7 @@ export class SlangNode {
147161
}
148162

149163
if (loc.trailingOffset === 0) {
150-
for (const childNode of childNodes.reverse()) {
164+
for (const childNode of reversedIterator(childNodes)) {
151165
if (typeof childNode === 'undefined' || Array.isArray(childNode))
152166
continue;
153167
const { trailingOffset, end } = childNode.loc;

0 commit comments

Comments
 (0)