11import {
22 TerminalKind ,
3- TerminalNode ,
4- TerminalKindExtensions
3+ TerminalKindExtensions ,
4+ TerminalNode
55} from '@nomicfoundation/slang/cst' ;
6- import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js' ;
76import { MultiLineComment } from '../slang-nodes/MultiLineComment.js' ;
87import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js' ;
98import { SingleLineComment } from '../slang-nodes/SingleLineComment.js' ;
109import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js' ;
1110
12- import type { Edge } from '@nomicfoundation/slang/cst' ;
11+ import type { Edge , Node } from '@nomicfoundation/slang/cst' ;
1312import type { Comment , StrictAstNode } from '../slang-nodes/types.d.ts' ;
1413import type { AstLocation , SlangAstNode } from '../types.d.ts' ;
1514
16- const isCommentOrWhiteSpace = createKindCheckFunction ( [
17- TerminalKind . MultiLineComment ,
18- TerminalKind . MultiLineNatSpecComment ,
19- TerminalKind . SingleLineComment ,
20- TerminalKind . SingleLineNatSpecComment ,
21- TerminalKind . EndOfLine ,
22- TerminalKind . Whitespace
23- ] ) ;
24-
2515const offsets = new Map < number , number > ( ) ;
2616export function clearOffsets ( ) : void {
2717 offsets . clear ( ) ;
@@ -41,10 +31,16 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
4131 } ;
4232}
4333
34+ function isNonTriviaNode ( node : Node ) : boolean {
35+ return (
36+ node . isNonterminalNode ( ) || ! TerminalKindExtensions . isTrivia ( node . kind )
37+ ) ;
38+ }
39+
4440function getOffset ( children : Edge [ ] | Iterable < Edge > ) : number {
4541 let offset = 0 ;
4642 for ( const { node } of children ) {
47- if ( node . isNonterminalNode ( ) || ! isCommentOrWhiteSpace ( node ) ) {
43+ if ( isNonTriviaNode ( node ) ) {
4844 // The node's content starts when we find the first non-terminal token,
4945 // or if we find a non-comment, non-whitespace token.
5046 return offset ;
@@ -95,10 +91,7 @@ export class SlangNode {
9591 let offset = initialOffset ;
9692
9793 for ( const { node } of children ) {
98- if (
99- node . isNonterminalNode ( ) ||
100- ! TerminalKindExtensions . isTrivia ( node . kind )
101- ) {
94+ if ( isNonTriviaNode ( node ) ) {
10295 // Also tracking TerminalNodes since some variants that were not
10396 // Identifier or YulIdentifier but were upgraded to TerminalNode
10497 offsets . set ( node . id , offset ) ;
0 commit comments