Skip to content

Commit cfeff2b

Browse files
committed
positioning the comments properly
1 parent 19882cf commit cfeff2b

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

src/slang-comments/handlers/handle-contract-definition-comments.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ export default function handleContractDefinitionComments({
5252
// If the last ContractSpecifier's an InheritanceSpecifier, the comment
5353
// is appended to the last InheritanceType.
5454
if (lastContractSpecifier.kind === NonterminalKind.InheritanceSpecifier) {
55-
addTrailingComment(
56-
lastContractSpecifier.types.items[
57-
lastContractSpecifier.types.items.length - 1
58-
],
59-
comment
60-
);
55+
addCollectionNodeLastComment(lastContractSpecifier.types, comment);
56+
return true;
57+
}
58+
if (
59+
lastContractSpecifier.kind === NonterminalKind.StorageLayoutSpecifier
60+
) {
61+
addTrailingComment(lastContractSpecifier.expression, comment);
6162
return true;
6263
}
63-
addTrailingComment(lastContractSpecifier, comment);
64-
return true;
6564
}
6665
}
6766

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
2+
import { util } from 'prettier';
3+
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
4+
5+
import type { HandlerParams } from './types.d.ts';
6+
7+
const { addTrailingComment } = util;
8+
9+
export default function handleContractSpecifiersComments({
10+
precedingNode,
11+
enclosingNode,
12+
comment
13+
}: HandlerParams): boolean {
14+
if (enclosingNode?.kind !== NonterminalKind.ContractSpecifiers) {
15+
return false;
16+
}
17+
18+
if (
19+
precedingNode &&
20+
precedingNode.kind === NonterminalKind.ContractSpecifier
21+
) {
22+
if (precedingNode.variant.kind === NonterminalKind.InheritanceSpecifier) {
23+
addCollectionNodeLastComment(precedingNode.variant.types, comment);
24+
return true;
25+
}
26+
if (precedingNode.variant.kind === NonterminalKind.StorageLayoutSpecifier) {
27+
addTrailingComment(precedingNode.variant.expression, comment);
28+
return true;
29+
}
30+
}
31+
32+
return false;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
2+
import { util } from 'prettier';
3+
4+
import type { HandlerParams } from './types.d.ts';
5+
6+
const { addLeadingComment } = util;
7+
8+
export default function handleStorageLayoutSpecifierComments({
9+
enclosingNode,
10+
followingNode,
11+
comment
12+
}: HandlerParams): boolean {
13+
if (enclosingNode?.kind !== NonterminalKind.StorageLayoutSpecifier) {
14+
return false;
15+
}
16+
17+
if (followingNode?.kind === NonterminalKind.Expression) {
18+
addLeadingComment(followingNode, comment);
19+
return true;
20+
}
21+
22+
return false;
23+
}

src/slang-comments/handlers/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import handleBlockComments from './handle-block-comments.js';
22
import handleContractDefinitionComments from './handle-contract-definition-comments.js';
3+
import handleContractSpecifiersComments from './handle-contract-specifiers-comments.js';
34
import handleElseBranchComments from './handle-else-branch-comments.js';
45
import handleIfStatementComments from './handle-if-statement-comments.js';
56
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
67
import handleLibraryDefinitionComments from './handle-library-definition-comments.js';
78
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js';
89
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
910
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
11+
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
1012
import handleWhileStatementComments from './handle-while-statement-comments.js';
1113
import handleYulBlockComments from './handle-yul-block-comments.js';
1214

1315
export default [
1416
handleBlockComments,
1517
handleContractDefinitionComments,
18+
handleContractSpecifiersComments,
1619
handleElseBranchComments,
1720
handleIfStatementComments,
1821
handleInterfaceDefinitionComments,
1922
handleLibraryDefinitionComments,
2023
handleModifierInvocationComments,
2124
handleParametersDeclarationComments,
2225
handlePositionalArgumentsDeclarationComments,
26+
handleStorageLayoutSpecifierComments,
2327
handleWhileStatementComments,
2428
handleYulBlockComments
2529
];

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ contract Comments4a
240240
Interface3,
241241
Interface4,
242242
Interface5,
243-
Interface6 /*why we used Interface6*/ /*where should this go?*/
244-
layout at 123 /*why we used this layout*/
243+
Interface6 /*why we used Interface6*/
244+
layout at /*where should this go?*/ 123 /*why we used this layout*/
245245
{
246246
// solhint-disable-previous-line no-empty-blocks
247247
}
@@ -253,8 +253,8 @@ contract Comments4b
253253
Interface3,
254254
Interface4,
255255
Interface5,
256-
Interface6 /*why we used Interface6*/ /*where should this go?*/
257-
layout at 123 + 456 /*why we used this layout*/
256+
Interface6 /*why we used Interface6*/
257+
layout at /*where should this go?*/ 123 + 456 /*why we used this layout*/
258258
{
259259
// solhint-disable-previous-line no-empty-blocks
260260
}
@@ -266,13 +266,15 @@ contract Comments4c
266266
Interface3,
267267
Interface4,
268268
Interface5,
269-
Interface6 /*why we used Interface6*/ /*where should this go?*/
270-
layout at f(123 + 456) /*why we used this layout*/
269+
Interface6 /*why we used Interface6*/
270+
layout at
271+
/*where should this go?*/ f(123 + 456) /*why we used this layout*/
272+
271273
{
272274
// solhint-disable-previous-line no-empty-blocks
273275
}
274276
275-
/*nice name*/ contract Comments5 {
277+
contract Comments5 /*nice name*/ {
276278
// solhint-disable-previous-line no-empty-blocks
277279
}
278280

0 commit comments

Comments
 (0)