Skip to content

Commit f6cc475

Browse files
committed
storing comments in a singleton
1 parent 7708d37 commit f6cc475

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
@@ -12,10 +12,16 @@ import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
1212
import type { AstLocation, SlangAstNode } from '../types.d.ts';
1313

1414
const offsets = new Map<number, number>();
15+
const comments: Comment[] = [];
16+
1517
export function clearOffsets(): void {
1618
offsets.clear();
1719
}
1820

21+
export function clearComments(): Comment[] {
22+
return comments.splice(0);
23+
}
24+
1925
function reversedIterator<T>(children: T[]): Iterable<T> {
2026
return {
2127
[Symbol.iterator](): Iterator<T> {
@@ -30,21 +36,6 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
3036
};
3137
}
3238

33-
function collectComments(
34-
comments: Comment[],
35-
node: StrictAstNode | StrictAstNode[] | undefined
36-
): Comment[] {
37-
if (node) {
38-
if (Array.isArray(node)) {
39-
return node.reduce(collectComments, comments);
40-
}
41-
if (node.comments.length > 0) {
42-
comments.push(...node.comments.splice(0));
43-
}
44-
}
45-
return comments;
46-
}
47-
4839
export class SlangNode {
4940
comments: Comment[] = [];
5041

@@ -98,16 +89,16 @@ export class SlangNode {
9889
// offset, it's hard to separate these responsibilities into different
9990
// functions without doing the iteration twice.
10091
case TerminalKind.MultiLineComment:
101-
this.comments.push(new MultiLineComment(node, offset));
92+
comments.push(new MultiLineComment(node, offset));
10293
break;
10394
case TerminalKind.MultiLineNatSpecComment:
104-
this.comments.push(new MultiLineNatSpecComment(node, offset));
95+
comments.push(new MultiLineNatSpecComment(node, offset));
10596
break;
10697
case TerminalKind.SingleLineComment:
107-
this.comments.push(new SingleLineComment(node, offset));
98+
comments.push(new SingleLineComment(node, offset));
10899
break;
109100
case TerminalKind.SingleLineNatSpecComment:
110-
this.comments.push(new SingleLineNatSpecComment(node, offset));
101+
comments.push(new SingleLineNatSpecComment(node, offset));
111102
break;
112103
}
113104
// We accumulate the trivia length
@@ -133,10 +124,7 @@ export class SlangNode {
133124
updateMetadata(
134125
...childNodes: (StrictAstNode | StrictAstNode[] | undefined)[]
135126
): void {
136-
const { comments, loc } = this;
137-
// Collect comments
138-
this.comments = childNodes.reduce(collectComments, comments);
139-
127+
const { loc } = this;
140128
// calculate correct loc object
141129
if (loc.leadingOffset === 0) {
142130
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)