From f4e3f1ababc6cd78309ff465dbf4111bf6de0034 Mon Sep 17 00:00:00 2001 From: Klaus Date: Thu, 24 Jul 2025 11:51:44 +0100 Subject: [PATCH 1/2] using Slang API to ignore trivia nodes --- src/slang-nodes/SlangNode.ts | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/slang-nodes/SlangNode.ts b/src/slang-nodes/SlangNode.ts index 93a42550a..b95b60e44 100644 --- a/src/slang-nodes/SlangNode.ts +++ b/src/slang-nodes/SlangNode.ts @@ -1,27 +1,17 @@ import { TerminalKind, - TerminalNode, - TerminalKindExtensions + TerminalKindExtensions, + TerminalNode } from '@nomicfoundation/slang/cst'; -import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js'; import { MultiLineComment } from '../slang-nodes/MultiLineComment.js'; import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js'; import { SingleLineComment } from '../slang-nodes/SingleLineComment.js'; import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js'; -import type { Edge } from '@nomicfoundation/slang/cst'; +import type { Edge, Node } from '@nomicfoundation/slang/cst'; import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts'; import type { AstLocation, SlangAstNode } from '../types.d.ts'; -const isCommentOrWhiteSpace = createKindCheckFunction([ - TerminalKind.MultiLineComment, - TerminalKind.MultiLineNatSpecComment, - TerminalKind.SingleLineComment, - TerminalKind.SingleLineNatSpecComment, - TerminalKind.EndOfLine, - TerminalKind.Whitespace -]); - const offsets = new Map(); export function clearOffsets(): void { offsets.clear(); @@ -41,10 +31,16 @@ function reversedIterator(children: T[]): Iterable { }; } +function isNonTriviaNode(node: Node): boolean { + return ( + node.isNonterminalNode() || !TerminalKindExtensions.isTrivia(node.kind) + ); +} + function getOffset(children: Edge[] | Iterable): number { let offset = 0; for (const { node } of children) { - if (node.isNonterminalNode() || !isCommentOrWhiteSpace(node)) { + if (isNonTriviaNode(node)) { // The node's content starts when we find the first non-terminal token, // or if we find a non-comment, non-whitespace token. return offset; @@ -95,10 +91,7 @@ export class SlangNode { let offset = initialOffset; for (const { node } of children) { - if ( - node.isNonterminalNode() || - !TerminalKindExtensions.isTrivia(node.kind) - ) { + if (isNonTriviaNode(node)) { // Also tracking TerminalNodes since some variants that were not // Identifier or YulIdentifier but were upgraded to TerminalNode offsets.set(node.id, offset); From 07e74d302b0ac6db828ffba28d925329a8d6aaba Mon Sep 17 00:00:00 2001 From: Klaus Date: Tue, 12 Aug 2025 12:19:33 +0100 Subject: [PATCH 2/2] `Edge[]` is already `Iterable` --- src/slang-nodes/SlangNode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slang-nodes/SlangNode.ts b/src/slang-nodes/SlangNode.ts index b95b60e44..8d54202b2 100644 --- a/src/slang-nodes/SlangNode.ts +++ b/src/slang-nodes/SlangNode.ts @@ -37,7 +37,7 @@ function isNonTriviaNode(node: Node): boolean { ); } -function getOffset(children: Edge[] | Iterable): number { +function getOffset(children: Iterable): number { let offset = 0; for (const { node } of children) { if (isNonTriviaNode(node)) {