Skip to content

Commit 9df4bd6

Browse files
committed
storing comments in a singleton
1 parent 6094176 commit 9df4bd6

4 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/slang-nodes/PositionalArgumentsDeclaration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ export class PositionalArgumentsDeclaration extends SlangNode {
2727

2828
// We need to check the comments at this point because they will be removed
2929
// from this node into the root node.
30+
// Since we are collecting comments in a different array, we have to query
31+
// the ast directly for possible block comments.
3032
this.isEmpty =
3133
this.arguments.items.length === 0 && // no arguments
32-
!this.comments.some((comment) => isBlockComment(comment)); // no block comments
34+
!ast.cst.children().some(({ node }) => isBlockComment(node)); // no block comments
3335
}
3436

3537
print(

src/slang-nodes/SlangNode.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ const isCommentOrWhiteSpace = createKindCheckFunction([
2323
]);
2424

2525
const offsets = new Map<number, number>();
26+
const comments: Comment[] = [];
27+
2628
export function clearOffsets(): void {
2729
offsets.clear();
2830
}
2931

32+
export function clearComments(): Comment[] {
33+
return comments.splice(0);
34+
}
35+
3036
function reversedIterator<T>(children: T[]): Iterable<T> {
3137
return {
3238
[Symbol.iterator](): Iterator<T> {
@@ -54,21 +60,6 @@ function getOffset(children: Edge[] | Iterable<Edge>): number {
5460
return offset;
5561
}
5662

57-
function collectComments(
58-
comments: Comment[],
59-
node: StrictAstNode | StrictAstNode[] | undefined
60-
): Comment[] {
61-
if (node) {
62-
if (Array.isArray(node)) {
63-
return node.reduce(collectComments, comments);
64-
}
65-
if (node.comments.length > 0) {
66-
comments.push(...node.comments.splice(0));
67-
}
68-
}
69-
return comments;
70-
}
71-
7263
export class SlangNode {
7364
comments: Comment[] = [];
7465

@@ -109,16 +100,16 @@ export class SlangNode {
109100
// offset, it's hard to separate these responsibilities into different
110101
// functions without doing the iteration twice.
111102
case TerminalKind.MultiLineComment:
112-
this.comments.push(new MultiLineComment(node, offset));
103+
comments.push(new MultiLineComment(node, offset));
113104
break;
114105
case TerminalKind.MultiLineNatSpecComment:
115-
this.comments.push(new MultiLineNatSpecComment(node, offset));
106+
comments.push(new MultiLineNatSpecComment(node, offset));
116107
break;
117108
case TerminalKind.SingleLineComment:
118-
this.comments.push(new SingleLineComment(node, offset));
109+
comments.push(new SingleLineComment(node, offset));
119110
break;
120111
case TerminalKind.SingleLineNatSpecComment:
121-
this.comments.push(new SingleLineNatSpecComment(node, offset));
112+
comments.push(new SingleLineNatSpecComment(node, offset));
122113
break;
123114
}
124115
}
@@ -141,10 +132,7 @@ export class SlangNode {
141132
updateMetadata(
142133
...childNodes: (StrictAstNode | StrictAstNode[] | undefined)[]
143134
): void {
144-
const { comments, loc } = this;
145-
// Collect comments
146-
this.comments = childNodes.reduce(collectComments, comments);
147-
135+
const { loc } = this;
148136
// calculate correct loc object
149137
if (loc.leadingOffset === 0) {
150138
for (const childNode of childNodes) {

src/slang-nodes/SourceUnit.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ export class SourceUnit extends SlangNode {
2121
this.members = new SourceUnitMembers(ast.members, options);
2222

2323
this.updateMetadata(this.members);
24-
25-
// Because of comments being extracted like a russian doll, the order needs
26-
// to be fixed at the end.
27-
this.comments = this.comments.sort((a, b) => a.loc.start - b.loc.start);
2824
}
2925

3026
print(

src/slangSolidityParser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://prettier.io/docs/en/plugins.html#parsers
22
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast';
3-
import { clearOffsets } from './slang-nodes/SlangNode.js';
3+
import { clearOffsets, clearComments } from './slang-nodes/SlangNode.js';
44
import { createParser } from './slang-utils/create-parser.js';
55
import { SourceUnit } from './slang-nodes/SourceUnit.js';
66

@@ -19,6 +19,10 @@ export default function parse(
1919
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
2020
options
2121
);
22+
23+
// Because of comments being extracted like a russian doll, the order needs
24+
// to be fixed at the end.
25+
parsed.comments = clearComments().sort((a, b) => a.loc.start - b.loc.start);
2226
clearOffsets();
2327
return parsed;
2428
}

0 commit comments

Comments
 (0)