Skip to content

Commit ea738be

Browse files
authored
Comments for an empty parameter list (#452)
1 parent 70fedd4 commit ea738be

4 files changed

Lines changed: 77 additions & 19 deletions

File tree

src/nodes/FunctionDefinition.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const {
44
}
55
} = require('prettier/standalone');
66

7+
const privateUtil = require('../prettier-comments/common/util');
78
const printSeparatedList = require('./print-separated-list');
9+
const printSeparatedItem = require('./print-separated-item');
10+
const printComments = require('./print-comments');
811

912
const functionName = (node, options) => {
1013
if (node.isConstructor && !node.name) return 'constructor';
@@ -23,18 +26,37 @@ const functionName = (node, options) => {
2326
return names[name];
2427
};
2528

26-
const parameters = (parametersType, node, path, print) =>
27-
node[parametersType] && node[parametersType].length > 0
28-
? printSeparatedList(path.map(print, parametersType), {
29-
separator: concat([
30-
',',
31-
// To keep consistency any list of parameters will split if it's longer than 2.
32-
// For more information see:
33-
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
34-
node[parametersType].length > 2 ? hardline : line
35-
])
36-
})
37-
: '';
29+
const parameters = (parametersType, node, path, print, options) => {
30+
if (node[parametersType] && node[parametersType].length > 0) {
31+
return printSeparatedList(path.map(print, parametersType), {
32+
separator: concat([
33+
',',
34+
// To keep consistency any list of parameters will split if it's longer than 2.
35+
// For more information see:
36+
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
37+
node[parametersType].length > 2 ? hardline : line
38+
])
39+
});
40+
}
41+
if (node.comments && node.comments.length > 0) {
42+
// we add a check to see if the comment is inside the parentheses
43+
const paremeterComments = printComments(
44+
node,
45+
path,
46+
options,
47+
(comment) =>
48+
privateUtil.getNextNonSpaceNonCommentCharacter(
49+
options.originalText,
50+
comment,
51+
options.locEnd
52+
) === ')'
53+
);
54+
return paremeterComments.parts.length > 0
55+
? printSeparatedItem(paremeterComments)
56+
: '';
57+
}
58+
return '';
59+
};
3860

3961
const visibility = (node) =>
4062
node.visibility && node.visibility !== 'default'
@@ -64,12 +86,12 @@ const modifiers = (node, path, print) =>
6486
? concat([line, join(line, path.map(print, 'modifiers'))])
6587
: '';
6688

67-
const returnParameters = (node, path, print) =>
89+
const returnParameters = (node, path, print, options) =>
6890
node.returnParameters
6991
? concat([
7092
line,
7193
'returns (',
72-
parameters('returnParameters', node, path, print),
94+
parameters('returnParameters', node, path, print, options),
7395
')'
7496
])
7597
: '';
@@ -83,17 +105,19 @@ const FunctionDefinition = {
83105
concat([
84106
functionName(node, options),
85107
'(',
86-
parameters('parameters', node, path, print),
108+
parameters('parameters', node, path, print, options),
87109
')',
88110
indent(
89111
group(
90112
concat([
113+
// TODO: sort comments for modifiers and return parameters
114+
printComments(node, path, options),
91115
visibility(node),
92116
stateMutability(node),
93117
virtual(node),
94118
override(node, path, print),
95119
modifiers(node, path, print),
96-
returnParameters(node, path, print),
120+
returnParameters(node, path, print, options),
97121
signatureEnd(node)
98122
])
99123
)

src/nodes/print-comments.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ const {
44
}
55
} = require('prettier');
66

7-
const printComments = (node, path, options) =>
7+
const printComments = (node, path, options, filter = () => true) =>
88
node.comments
99
? join(
1010
line,
1111
path
1212
.map((commentPath) => {
1313
const comment = commentPath.getValue();
14-
if (comment.trailing || comment.leading) {
14+
if (comment.trailing || comment.leading || comment.printed) {
15+
return null;
16+
}
17+
if (!filter(comment)) {
1518
return null;
1619
}
1720
comment.printed = true;

tests/Comments/Comments.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,13 @@ contract Comments9 {
8383
}
8484
}
8585
}
86+
87+
interface Comments10 {
88+
function someFunction(
89+
// the first value
90+
// the second value
91+
// the lats value
92+
) /* comment outside the parameters */ external;
93+
94+
function someOtherFunction(/* checking for Block comment */) external;
95+
}

tests/Comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ contract Comments9 {
8686
}
8787
}
8888
}
89-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
interface Comments10 {
91+
function someFunction(
92+
// the first value
93+
// the second value
94+
// the lats value
95+
) /* comment outside the parameters */ external;
96+
97+
function someOtherFunction(/* checking for Block comment */) external;
98+
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9099
pragma solidity ^0.4.24;
91100
92101
contract Comments1 {
@@ -194,4 +203,16 @@ contract Comments9 {
194203
}
195204
}
196205
206+
interface Comments10 {
207+
function someFunction(
208+
// the first value
209+
// the second value
210+
// the lats value
211+
)/* comment outside the parameters */ external;
212+
213+
function someOtherFunction(
214+
/* checking for Block comment */
215+
) external;
216+
}
217+
197218
`;

0 commit comments

Comments
 (0)