Skip to content

Commit c461dcb

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

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/slang-nodes/SlangNode.ts

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

26-
function getLeadingOffset(children: Edge[]): number {
26+
function reversedIterator(children: Edge[]): Iterable<Edge> {
27+
return {
28+
[Symbol.iterator](): Iterator<Edge> {
29+
let index = children.length;
30+
return {
31+
next: function (): IteratorResult<Edge, Edge> {
32+
index--;
33+
return {
34+
done: index < 0,
35+
value: children[index]
36+
};
37+
}
38+
};
39+
}
40+
};
41+
}
42+
43+
function getLeadingOffset(children: Edge[] | Iterable<Edge>): number {
2744
let offset = 0;
2845
for (const { node } of children) {
2946
if (node.isNonterminalNode() || !isCommentOrWhiteSpace(node)) {
@@ -110,7 +127,10 @@ export class SlangNode {
110127

111128
const [leadingOffset, trailingOffset] = enclosePeripheralComments
112129
? [0, 0]
113-
: [getLeadingOffset(children), getLeadingOffset(children.reverse())];
130+
: [
131+
getLeadingOffset(children),
132+
getLeadingOffset(reversedIterator(children))
133+
];
114134

115135
this.loc = {
116136
start: initialOffset + leadingOffset,

0 commit comments

Comments
 (0)