Skip to content

Commit c6ec971

Browse files
committed
using a reverse iterator instead of reversing the array
1 parent 5866304 commit c6ec971

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)) {
@@ -110,7 +124,7 @@ export class SlangNode {
110124

111125
const [leadingOffset, trailingOffset] = enclosePeripheralComments
112126
? [0, 0]
113-
: [getLeadingOffset(children), getLeadingOffset(children.reverse())];
127+
: [getOffset(children), getOffset(reversedIterator(children))];
114128

115129
this.loc = {
116130
start: initialOffset + leadingOffset,
@@ -143,7 +157,7 @@ export class SlangNode {
143157
}
144158

145159
if (loc.trailingOffset === 0) {
146-
for (const childNode of childNodes.reverse()) {
160+
for (const childNode of reversedIterator(childNodes)) {
147161
if (typeof childNode === 'undefined' || Array.isArray(childNode))
148162
continue;
149163
const { trailingOffset, end } = childNode.loc;

0 commit comments

Comments
 (0)