Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/slang-comments/handlers/handle-struct-comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';

import type { HandlerParams } from './types.d.ts';

export default function handleStructComments({
text,
precedingNode,
enclosingNode,
followingNode,
comment
}: HandlerParams): boolean {
if (enclosingNode?.kind !== NonterminalKind.StructDefinition) {
return false;
}

const nextCharacter = util.getNextNonSpaceNonCommentCharacter(
text,
locEnd(comment)
);

if (
precedingNode?.kind === NonterminalKind.StructMembers &&
nextCharacter === '}'
) {
addCollectionNodeLastComment(precedingNode, comment);
return true;
}

if (followingNode?.kind === NonterminalKind.StructMembers) {
addCollectionNodeFirstComment(followingNode, comment);
return true;
}

return false;
}
2 changes: 2 additions & 0 deletions src/slang-comments/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import handleModifierInvocationComments from './handle-modifier-invocation-comme
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
import handleStructComments from './handle-struct-comments.js';
import handleWhileStatementComments from './handle-while-statement-comments.js';
import handleYulBlockComments from './handle-yul-block-comments.js';

Expand All @@ -24,6 +25,7 @@ export default [
handleParametersDeclarationComments,
handlePositionalArgumentsDeclarationComments,
handleStorageLayoutSpecifierComments,
handleStructComments,
handleWhileStatementComments,
handleYulBlockComments
];
13 changes: 13 additions & 0 deletions tests/format/Comments/Comments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,17 @@ contract Comments13 {
) // comment 13
{
}
}



contract Comments14 {
struct AssetStore // use 0 for non-EVM chains
{
uint chainId;
uint assetStoreIndex;
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
//string shortName; // usually an asset shortName `eth:` or `st:` for a stealth address
}

}
25 changes: 24 additions & 1 deletion tests/format/Comments/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`Comments.sol format 1`] = `
====================================options=====================================
Expand Down Expand Up @@ -174,6 +174,19 @@ contract Comments13 {
{
}
}



contract Comments14 {
struct AssetStore // use 0 for non-EVM chains
{
uint chainId;
uint assetStoreIndex;
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
//string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address
}

}
=====================================output=====================================
contract Comments1 {
/* solhint-disable var-name-mixedcase */
Expand Down Expand Up @@ -378,5 +391,15 @@ contract Comments13 {
{}
}

contract Comments14 {
struct AssetStore {
// use 0 for non-EVM chains
uint chainId;
uint assetStoreIndex;
string assetStoreAddress; // we assume all addresses are strings and the front-end will cast correctly
//string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address
}
}

================================================================================
`;