From 6987f95e8f902961c32ce0e16965135440e7657e Mon Sep 17 00:00:00 2001 From: Klaus Date: Tue, 24 Jun 2025 12:31:13 +0100 Subject: [PATCH 1/2] adding tests for Comments in StructDefinition --- tests/format/Comments/Comments.sol | 13 ++++++++++ .../__snapshots__/format.test.js.snap | 24 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/format/Comments/Comments.sol b/tests/format/Comments/Comments.sol index e59f5e034..d2b9234b5 100644 --- a/tests/format/Comments/Comments.sol +++ b/tests/format/Comments/Comments.sol @@ -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 + } + } \ No newline at end of file diff --git a/tests/format/Comments/__snapshots__/format.test.js.snap b/tests/format/Comments/__snapshots__/format.test.js.snap index 1e9252a79..d38c10c41 100644 --- a/tests/format/Comments/__snapshots__/format.test.js.snap +++ b/tests/format/Comments/__snapshots__/format.test.js.snap @@ -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===================================== @@ -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 */ @@ -378,5 +391,14 @@ 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 +} + ================================================================================ `; From e9d398d0ebd57f5c308e6c5b695341e12423d0d4 Mon Sep 17 00:00:00 2001 From: Klaus Date: Tue, 24 Jun 2025 12:29:03 +0100 Subject: [PATCH 2/2] StructDefinitions were placing comments outside of of the definition. --- .../handlers/handle-struct-comments.ts | 39 +++++++++++++++++++ src/slang-comments/handlers/index.ts | 2 + .../__snapshots__/format.test.js.snap | 5 ++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/slang-comments/handlers/handle-struct-comments.ts diff --git a/src/slang-comments/handlers/handle-struct-comments.ts b/src/slang-comments/handlers/handle-struct-comments.ts new file mode 100644 index 000000000..d12204e17 --- /dev/null +++ b/src/slang-comments/handlers/handle-struct-comments.ts @@ -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; +} diff --git a/src/slang-comments/handlers/index.ts b/src/slang-comments/handlers/index.ts index 77508c2e0..dbc5f4c92 100644 --- a/src/slang-comments/handlers/index.ts +++ b/src/slang-comments/handlers/index.ts @@ -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'; @@ -24,6 +25,7 @@ export default [ handleParametersDeclarationComments, handlePositionalArgumentsDeclarationComments, handleStorageLayoutSpecifierComments, + handleStructComments, handleWhileStatementComments, handleYulBlockComments ]; diff --git a/tests/format/Comments/__snapshots__/format.test.js.snap b/tests/format/Comments/__snapshots__/format.test.js.snap index d38c10c41..b71b73df1 100644 --- a/tests/format/Comments/__snapshots__/format.test.js.snap +++ b/tests/format/Comments/__snapshots__/format.test.js.snap @@ -392,12 +392,13 @@ contract Comments13 { } contract Comments14 { - struct AssetStore { // use 0 for non-EVM chains + 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 } - //string shortName; // usually an asset shortName \`eth:\` or \`st:\` for a stealth address } ================================================================================