Skip to content

Commit 9b5eaa1

Browse files
committed
avoiding an extra iteration over all children
1 parent 1a59312 commit 9b5eaa1

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/slang-nodes/SlangNode.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.
55
import { SingleLineComment } from '../slang-nodes/SingleLineComment.js';
66
import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js';
77

8-
import type { Node } from '@nomicfoundation/slang/cst';
8+
import type { Edge } from '@nomicfoundation/slang/cst';
99
import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
1010
import type { AstLocation, SlangAstNode } from '../types.d.ts';
1111

@@ -23,15 +23,15 @@ export function clearOffsets(): void {
2323
offsets.clear();
2424
}
2525

26-
function getLeadingOffset(children: Node[]): number {
26+
function getLeadingOffset(children: Edge[]): number {
2727
let offset = 0;
28-
for (const child of children) {
29-
if (child.isNonterminalNode() || !isCommentOrWhiteSpace(child)) {
28+
for (const { node } of children) {
29+
if (node.isNonterminalNode() || !isCommentOrWhiteSpace(node)) {
3030
// The node's content starts when we find the first non-terminal token,
3131
// or if we find a non-comment, non-whitespace token.
3232
return offset;
3333
}
34-
offset += child.textLength.utf16;
34+
offset += node.textLength.utf16;
3535
}
3636
return offset;
3737
}
@@ -71,14 +71,14 @@ export class SlangNode {
7171
return;
7272
}
7373
const parent = ast.cst;
74-
const children = parent.children().map((child) => child.node);
74+
const children = parent.children();
7575

7676
const initialOffset = offsets.get(parent.id) || 0;
7777
let offset = initialOffset;
7878

79-
for (const child of children) {
80-
const { id, kind, textLength } = child;
81-
if (child.isNonterminalNode()) {
79+
for (const { node } of children) {
80+
const { id, kind, textLength } = node;
81+
if (node.isNonterminalNode()) {
8282
offsets.set(id, offset);
8383
} else {
8484
switch (kind) {
@@ -87,16 +87,16 @@ export class SlangNode {
8787
// offset, it's hard to separate these responsibilities into different
8888
// functions without doing the iteration twice.
8989
case TerminalKind.MultiLineComment:
90-
this.comments.push(new MultiLineComment(child, offset));
90+
this.comments.push(new MultiLineComment(node, offset));
9191
break;
9292
case TerminalKind.MultiLineNatSpecComment:
93-
this.comments.push(new MultiLineNatSpecComment(child, offset));
93+
this.comments.push(new MultiLineNatSpecComment(node, offset));
9494
break;
9595
case TerminalKind.SingleLineComment:
96-
this.comments.push(new SingleLineComment(child, offset));
96+
this.comments.push(new SingleLineComment(node, offset));
9797
break;
9898
case TerminalKind.SingleLineNatSpecComment:
99-
this.comments.push(new SingleLineNatSpecComment(child, offset));
99+
this.comments.push(new SingleLineNatSpecComment(node, offset));
100100
break;
101101
case TerminalKind.Identifier:
102102
case TerminalKind.YulIdentifier:

0 commit comments

Comments
 (0)