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/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..b71b73df1 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,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 + } +} + ================================================================================ `;