@@ -23,7 +23,21 @@ export function clearOffsets(): void {
23
23
offsets . clear ( ) ;
24
24
}
25
25
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 {
27
41
let offset = 0 ;
28
42
for ( const { node } of children ) {
29
43
if ( node . isNonterminalNode ( ) || ! isCommentOrWhiteSpace ( node ) ) {
@@ -114,7 +128,7 @@ export class SlangNode {
114
128
115
129
const [ leadingOffset , trailingOffset ] = enclosePeripheralComments
116
130
? [ 0 , 0 ]
117
- : [ getLeadingOffset ( children ) , getLeadingOffset ( children . reverse ( ) ) ] ;
131
+ : [ getOffset ( children ) , getOffset ( reversedIterator ( children ) ) ] ;
118
132
119
133
this . loc = {
120
134
start : initialOffset + leadingOffset ,
@@ -147,7 +161,7 @@ export class SlangNode {
147
161
}
148
162
149
163
if ( loc . trailingOffset === 0 ) {
150
- for ( const childNode of childNodes . reverse ( ) ) {
164
+ for ( const childNode of reversedIterator ( childNodes ) ) {
151
165
if ( typeof childNode === 'undefined' || Array . isArray ( childNode ) )
152
166
continue ;
153
167
const { trailingOffset, end } = childNode . loc ;
0 commit comments