@@ -12,10 +12,16 @@ import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
1212import type { AstLocation , SlangAstNode } from '../types.d.ts' ;
1313
1414const offsets = new Map < number , number > ( ) ;
15+ const comments : Comment [ ] = [ ] ;
16+
1517export function clearOffsets ( ) : void {
1618 offsets . clear ( ) ;
1719}
1820
21+ export function clearComments ( ) : Comment [ ] {
22+ return comments . splice ( 0 ) ;
23+ }
24+
1925function reversedIterator < T > ( children : T [ ] ) : Iterable < T > {
2026 return {
2127 [ Symbol . iterator ] ( ) : Iterator < T > {
@@ -30,21 +36,6 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
3036 } ;
3137}
3238
33- function collectComments (
34- comments : Comment [ ] ,
35- node : StrictAstNode | StrictAstNode [ ] | undefined
36- ) : Comment [ ] {
37- if ( node ) {
38- if ( Array . isArray ( node ) ) {
39- return node . reduce ( collectComments , comments ) ;
40- }
41- if ( node . comments . length > 0 ) {
42- comments . push ( ...node . comments . splice ( 0 ) ) ;
43- }
44- }
45- return comments ;
46- }
47-
4839export class SlangNode {
4940 comments : Comment [ ] = [ ] ;
5041
@@ -98,16 +89,16 @@ export class SlangNode {
9889 // offset, it's hard to separate these responsibilities into different
9990 // functions without doing the iteration twice.
10091 case TerminalKind . MultiLineComment :
101- this . comments . push ( new MultiLineComment ( node , offset ) ) ;
92+ comments . push ( new MultiLineComment ( node , offset ) ) ;
10293 break ;
10394 case TerminalKind . MultiLineNatSpecComment :
104- this . comments . push ( new MultiLineNatSpecComment ( node , offset ) ) ;
95+ comments . push ( new MultiLineNatSpecComment ( node , offset ) ) ;
10596 break ;
10697 case TerminalKind . SingleLineComment :
107- this . comments . push ( new SingleLineComment ( node , offset ) ) ;
98+ comments . push ( new SingleLineComment ( node , offset ) ) ;
10899 break ;
109100 case TerminalKind . SingleLineNatSpecComment :
110- this . comments . push ( new SingleLineNatSpecComment ( node , offset ) ) ;
101+ comments . push ( new SingleLineNatSpecComment ( node , offset ) ) ;
111102 break ;
112103 }
113104 // We accumulate the trivia length
@@ -133,10 +124,7 @@ export class SlangNode {
133124 updateMetadata (
134125 ...childNodes : ( StrictAstNode | StrictAstNode [ ] | undefined ) [ ]
135126 ) : void {
136- const { comments, loc } = this ;
137- // Collect comments
138- this . comments = childNodes . reduce ( collectComments , comments ) ;
139-
127+ const { loc } = this ;
140128 // calculate correct loc object
141129 if ( loc . leadingOffset === 0 ) {
142130 for ( const childNode of childNodes ) {
0 commit comments