Skip to content

Commit ef897e5

Browse files
committed
print comments inside modifier parameters
1 parent 5ff26e2 commit ef897e5

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/slang-comments/handlers/handle-block-comments.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2-
import { util } from 'prettier';
32
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
3+
import addHubNodeLastComment from './add-hub-node-last-comment.js';
44

5-
import type { HandlerParams } from './types.js';
6-
7-
const { addDanglingComment, addTrailingComment } = util;
5+
import type { HandlerParams } from './types';
86

97
export default function handleBlockComments({
108
precedingNode,
@@ -17,14 +15,7 @@ export default function handleBlockComments({
1715
}
1816

1917
if (precedingNode?.kind === NonterminalKind.Statements) {
20-
if (precedingNode.items.length === 0) {
21-
addDanglingComment(precedingNode, comment, false);
22-
} else {
23-
addTrailingComment(
24-
precedingNode.items[precedingNode.items.length - 1],
25-
comment
26-
);
27-
}
18+
addHubNodeLastComment(precedingNode, comment);
2819
return true;
2920
}
3021

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
3+
import addHubNodeLastComment from './add-hub-node-last-comment.js';
4+
5+
import type { HandlerParams } from './types';
6+
7+
export default function handlePositionalArgumentsDeclarationComments({
8+
precedingNode,
9+
enclosingNode,
10+
followingNode,
11+
comment
12+
}: HandlerParams): boolean {
13+
if (comment.value.startsWith('// test')) {
14+
console.log(comment);
15+
}
16+
if (enclosingNode?.kind !== NonterminalKind.PositionalArgumentsDeclaration) {
17+
return false;
18+
}
19+
20+
if (precedingNode?.kind === NonterminalKind.PositionalArguments) {
21+
addHubNodeLastComment(precedingNode, comment);
22+
return true;
23+
}
24+
25+
if (followingNode?.kind === NonterminalKind.PositionalArguments) {
26+
addHubNodeFirstComment(followingNode, comment);
27+
return true;
28+
}
29+
30+
return false;
31+
}

src/slang-comments/handlers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import handleContractDefinitionComments from './handle-contract-definition-comme
33
import handleElseBranchComments from './handle-else-branch-comments.js';
44
import handleIfStatementComments from './handle-if-statement-comments.js';
55
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
6+
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
67
import handleWhileStatementComments from './handle-while-statement-comments.js';
78
import handleYulBlockComments from './handle-yul-block-comments.js';
89

@@ -12,6 +13,7 @@ export default [
1213
handleElseBranchComments,
1314
handleIfStatementComments,
1415
handleParametersDeclarationComments,
16+
handlePositionalArgumentsDeclarationComments,
1517
handleWhileStatementComments,
1618
handleYulBlockComments
1719
];

src/slang-printers/print-comments.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { doc } from 'prettier';
22
import { printComment } from '../slang-comments/printer.js';
33
import { isPrettier2 } from '../slang-utils/backward-compatibility.js';
4+
import { isLineComment } from '../slang-utils/is-comment.js';
45

56
import type { AstPath, Doc } from 'prettier';
67
import type { AstNode } from '../slang-nodes';
78
import type { DocV2 } from './types';
89

9-
const { join, line } = doc.builders;
10+
const { breakParent, join, line } = doc.builders;
1011

1112
export function printComments(path: AstPath<AstNode>): Doc[] {
1213
const document = join(
@@ -18,7 +19,8 @@ export function printComments(path: AstPath<AstNode>): Doc[] {
1819
return '';
1920
}
2021
comment.printed = true;
21-
return printComment(commentPath);
22+
const printed = printComment(commentPath);
23+
return isLineComment(comment) ? [printed, breakParent] : printed;
2224
}, 'comments')
2325
.filter(Boolean)
2426
);

0 commit comments

Comments
 (0)