@@ -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 ) ) {
@@ -110,7 +124,7 @@ export class SlangNode {
110
124
111
125
const [ leadingOffset , trailingOffset ] = enclosePeripheralComments
112
126
? [ 0 , 0 ]
113
- : [ getLeadingOffset ( children ) , getLeadingOffset ( children . reverse ( ) ) ] ;
127
+ : [ getOffset ( children ) , getOffset ( reversedIterator ( children ) ) ] ;
114
128
115
129
this . loc = {
116
130
start : initialOffset + leadingOffset ,
@@ -143,7 +157,7 @@ export class SlangNode {
143
157
}
144
158
145
159
if ( loc . trailingOffset === 0 ) {
146
- for ( const childNode of childNodes . reverse ( ) ) {
160
+ for ( const childNode of reversedIterator ( childNodes ) ) {
147
161
if ( typeof childNode === 'undefined' || Array . isArray ( childNode ) )
148
162
continue ;
149
163
const { trailingOffset, end } = childNode . loc ;
0 commit comments