Skip to content

Commit 2a140f3

Browse files
authored
Comments in struct definition (#1181)
* adding tests for Comments in StructDefinition * StructDefinitions were placing comments outside of of the definition.
1 parent e3f15c8 commit 2a140f3

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed
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+
import { locEnd } from '../../slang-utils/loc.js';
4+
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
5+
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
6+
7+
import type { HandlerParams } from './types.d.ts';
8+
9+
export default function handleStructComments({
10+
text,
11+
precedingNode,
12+
enclosingNode,
13+
followingNode,
14+
comment
15+
}: HandlerParams): boolean {
16+
if (enclosingNode?.kind !== NonterminalKind.StructDefinition) {
17+
return false;
18+
}
19+
20+
const nextCharacter = util.getNextNonSpaceNonCommentCharacter(
21+
text,
22+
locEnd(comment)
23+
);
24+
25+
if (
26+
precedingNode?.kind === NonterminalKind.StructMembers &&
27+
nextCharacter === '}'
28+
) {
29+
addCollectionNodeLastComment(precedingNode, comment);
30+
return true;
31+
}
32+
33+
if (followingNode?.kind === NonterminalKind.StructMembers) {
34+
addCollectionNodeFirstComment(followingNode, 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
@@ -9,6 +9,7 @@ import handleModifierInvocationComments from './handle-modifier-invocation-comme
99
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
1010
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
1111
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
12+
import handleStructComments from './handle-struct-comments.js';
1213
import handleWhileStatementComments from './handle-while-statement-comments.js';
1314
import handleYulBlockComments from './handle-yul-block-comments.js';
1415

@@ -24,6 +25,7 @@ export default [
2425
handleParametersDeclarationComments,
2526
handlePositionalArgumentsDeclarationComments,
2627
handleStorageLayoutSpecifierComments,
28+
handleStructComments,
2729
handleWhileStatementComments,
2830
handleYulBlockComments
2931
];

tests/format/Comments/Comments.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,17 @@ contract Comments13 {
165165
) // comment 13
166166
{
167167
}
168+
}
169+
170+
171+
172+
contract Comments14 {
173+
struct AssetStore // use 0 for non-EVM chains
174+
{
175+
uint chainId;
176+
uint assetStoreIndex;
177+
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
178+
//string shortName; // usually an asset shortName `eth:` or `st:` for a stealth address
179+
}
180+
168181
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`Comments.sol format 1`] = `
44
====================================options=====================================
@@ -174,6 +174,19 @@ contract Comments13 {
174174
{
175175
}
176176
}
177+
178+
179+
180+
contract Comments14 {
181+
struct AssetStore // use 0 for non-EVM chains
182+
{
183+
uint chainId;
184+
uint assetStoreIndex;
185+
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
186+
//string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address
187+
}
188+
189+
}
177190
=====================================output=====================================
178191
contract Comments1 {
179192
/* solhint-disable var-name-mixedcase */
@@ -378,5 +391,15 @@ contract Comments13 {
378391
{}
379392
}
380393
394+
contract Comments14 {
395+
struct AssetStore {
396+
// use 0 for non-EVM chains
397+
uint chainId;
398+
uint assetStoreIndex;
399+
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
400+
//string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address
401+
}
402+
}
403+
381404
================================================================================
382405
`;

0 commit comments

Comments
 (0)