Skip to content

Commit e9d398d

Browse files
committed
StructDefinitions were placing comments outside of of the definition.
1 parent 6987f95 commit e9d398d

3 files changed

Lines changed: 44 additions & 2 deletions

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+
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/__snapshots__/format.test.js.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,13 @@ contract Comments13 {
392392
}
393393
394394
contract Comments14 {
395-
struct AssetStore { // use 0 for non-EVM chains
395+
struct AssetStore {
396+
// use 0 for non-EVM chains
396397
uint chainId;
397398
uint assetStoreIndex;
398399
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
399401
}
400-
//string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address
401402
}
402403
403404
================================================================================

0 commit comments

Comments
 (0)