Skip to content

Commit f4e3f1a

Browse files
committed
using Slang API to ignore trivia nodes
1 parent 6094176 commit f4e3f1a

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

src/slang-nodes/SlangNode.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
import {
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';
76
import { MultiLineComment } from '../slang-nodes/MultiLineComment.js';
87
import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js';
98
import { SingleLineComment } from '../slang-nodes/SingleLineComment.js';
109
import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js';
1110

12-
import type { Edge } from '@nomicfoundation/slang/cst';
11+
import type { Edge, Node } from '@nomicfoundation/slang/cst';
1312
import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
1413
import 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-
2515
const offsets = new Map<number, number>();
2616
export 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+
4440
function 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

Comments
 (0)