Skip to content

Commit 1952706

Browse files
committed
Placing comments in the middle of a member access chained expression in the correct place
1 parent 48599dd commit 1952706

5 files changed

Lines changed: 84 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
2+
import { util } from 'prettier';
3+
4+
import type { HandlerParams } from './types.d.ts';
5+
6+
const { addLeadingComment, addTrailingComment } = util;
7+
8+
export default function handleMemberAccessExpressionComments({
9+
precedingNode,
10+
enclosingNode,
11+
followingNode,
12+
comment
13+
}: HandlerParams): boolean {
14+
if (enclosingNode?.kind !== NonterminalKind.MemberAccessExpression) {
15+
return false;
16+
}
17+
18+
if (followingNode !== undefined && followingNode === enclosingNode.operand) {
19+
addLeadingComment(followingNode, comment);
20+
return true;
21+
}
22+
23+
if (
24+
precedingNode !== undefined &&
25+
followingNode !== undefined &&
26+
precedingNode === enclosingNode.operand &&
27+
followingNode === enclosingNode.member
28+
) {
29+
addTrailingComment(precedingNode, comment);
30+
return true;
31+
}
32+
33+
if (precedingNode !== undefined && precedingNode === enclosingNode.member) {
34+
addTrailingComment(precedingNode, comment);
35+
return true;
36+
}
37+
38+
return false;
39+
}

src/slang-comments/handlers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import handleElseBranchComments from './handle-else-branch-comments.js';
55
import handleIfStatementComments from './handle-if-statement-comments.js';
66
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
77
import handleLibraryDefinitionComments from './handle-library-definition-comments.js';
8+
import handleMemberAccessExpressionComments from './handle-member-access-expression-comments.js';
89
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js';
910
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
1011
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
@@ -22,6 +23,7 @@ export default [
2223
handleIfStatementComments,
2324
handleInterfaceDefinitionComments,
2425
handleLibraryDefinitionComments,
26+
handleMemberAccessExpressionComments,
2527
handleModifierInvocationComments,
2628
handleParametersDeclarationComments,
2729
handlePositionalArgumentsDeclarationComments,

src/slang-nodes/MemberAccessExpression.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ export class MemberAccessExpression extends SlangNode {
128128
}
129129

130130
print(path: AstPath<MemberAccessExpression>, print: PrintFunction): Doc {
131+
let operandDoc = path.call(print, 'operand');
132+
if (Array.isArray(operandDoc)) {
133+
operandDoc = operandDoc.flat();
134+
}
135+
131136
const document = [
132-
path.call(print, 'operand'),
137+
operandDoc,
133138
label('separator', [softline, '.']),
134139
path.call(print, 'member')
135140
].flat();

tests/format/MemberAccess/MemberAccess.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ contract MemberAccess {
6262
abi.encodeWithSelector(f.selector),
6363
abi.encode(returned1)
6464
);
65+
// Comments in between the chain
66+
game.
67+
// CONFIG
68+
// Do not touch
69+
config. // Window
70+
// Resolution 1000
71+
resolveWindow = 1000;
72+
config.defaultDelay = 0;
73+
begin
74+
.// Comment 1
75+
functionCall(/* Comment about parameter */ parameter)
76+
.// Comment 2
77+
end;
6578
}
6679
}
6780

tests/format/MemberAccess/__snapshots__/format.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ contract MemberAccess {
7070
abi.encodeWithSelector(f.selector),
7171
abi.encode(returned1)
7272
);
73+
// Comments in between the chain
74+
game.
75+
// CONFIG
76+
// Do not touch
77+
config. // Window
78+
// Resolution 1000
79+
resolveWindow = 1000;
80+
config.defaultDelay = 0;
81+
begin
82+
.// Comment 1
83+
functionCall(/* Comment about parameter */ parameter)
84+
.// Comment 2
85+
end;
7386
}
7487
}
7588
@@ -206,6 +219,17 @@ contract MemberAccess {
206219
abi.encodeWithSelector(f.selector),
207220
abi.encode(returned1)
208221
);
222+
// Comments in between the chain
223+
game
224+
// CONFIG
225+
// Do not touch
226+
.config // Window
227+
// Resolution 1000
228+
.resolveWindow = 1000;
229+
config.defaultDelay = 0;
230+
begin // Comment 1
231+
.functionCall(/* Comment about parameter */ parameter) // Comment 2
232+
.end;
209233
}
210234
}
211235

0 commit comments

Comments
 (0)