Skip to content

Commit 59aca33

Browse files
committed
Base class is initialised with a cst Node instead of an ast. making it easier to query for it being a TerminalNode or NonTerminalNode and making the destructuring easier.
1 parent 76056f3 commit 59aca33

4 files changed

Lines changed: 12 additions & 17 deletions

File tree

src/slang-nodes/CommentNode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { TerminalNode } from '@nomicfoundation/slang/cst';
2-
1+
import type { TerminalNode } from '@nomicfoundation/slang/cst';
32
import type { Location } from '../types.d.ts';
43
import type { StrictAstNode } from './types.d.ts';
54

src/slang-nodes/Identifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Identifier extends SlangNode {
99
value: string;
1010

1111
constructor(ast: TerminalNode) {
12-
super(ast);
12+
super({ cst: ast });
1313

1414
this.value = ast.unparse();
1515
}

src/slang-nodes/SlangNode.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TerminalKind, TerminalNode } from '@nomicfoundation/slang/cst';
1+
import { TerminalKind } from '@nomicfoundation/slang/cst';
22
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
33
import { MultiLineComment } from '../slang-nodes/MultiLineComment.js';
44
import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js';
@@ -7,7 +7,7 @@ import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecCommen
77

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

1212
const isCommentOrWhiteSpace = createKindCheckFunction([
1313
TerminalKind.MultiLineComment,
@@ -29,7 +29,7 @@ function getLeadingOffset(children: Node[]): number {
2929
if (child.isNonterminalNode() || !isCommentOrWhiteSpace(child)) {
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.
32-
return offset;
32+
break;
3333
}
3434
offset += child.textLength.utf16;
3535
}
@@ -56,24 +56,20 @@ export class SlangNode {
5656

5757
loc: AstLocation;
5858

59-
constructor(
60-
ast: SlangAstNode | TerminalNode,
61-
enclosePeripheralComments = false
62-
) {
63-
if (ast instanceof TerminalNode) {
64-
const offset = offsets.get(ast.id) || 0;
59+
constructor({ cst }: { cst: Node }, enclosePeripheralComments = false) {
60+
if (cst.isTerminalNode()) {
61+
const offset = offsets.get(cst.id) || 0;
6562
this.loc = {
6663
start: offset,
67-
end: offset + ast.textLength.utf16,
64+
end: offset + cst.textLength.utf16,
6865
leadingOffset: 0,
6966
trailingOffset: 0
7067
};
7168
return;
7269
}
73-
const parent = ast.cst;
74-
const children = parent.children().map((child) => child.node);
70+
const children = cst.children().map((child) => child.node);
7571

76-
const initialOffset = offsets.get(parent.id) || 0;
72+
const initialOffset = offsets.get(cst.id) || 0;
7773
let offset = initialOffset;
7874

7975
for (const child of children) {

src/slang-nodes/YulIdentifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class YulIdentifier extends SlangNode {
99
value: string;
1010

1111
constructor(ast: TerminalNode) {
12-
super(ast);
12+
super({ cst: ast });
1313

1414
this.value = ast.unparse();
1515
}

0 commit comments

Comments
 (0)