Skip to content

Commit 433e6f6

Browse files
Franco Victoriomattiaerre
authored andcommitted
Print comments in empty blocks (#85)
* Add function to fixture that makes prettier crash * Print comments in empty blocks
1 parent 440057b commit 433e6f6

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/printer.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,35 @@ function genericPrint(path, options, print) {
149149
doc = concat([doc, '(', join(', ', path.map(print, 'arguments')), ')']);
150150
}
151151
return doc;
152-
case 'Block':
153-
return concat([
152+
case 'Block': {
153+
const parts = [
154154
'{',
155155
indent(line),
156-
indent(printPreservingEmptyLines(path, 'statements', options, print)),
157-
line,
158-
'}'
159-
]);
156+
indent(printPreservingEmptyLines(path, 'statements', options, print))
157+
];
158+
159+
if (node.comments) {
160+
let first = true;
161+
path.each(commentPath => {
162+
if (first) {
163+
first = false;
164+
} else {
165+
parts.push(indent(line));
166+
}
167+
const comment = commentPath.getValue();
168+
if (comment.trailing || comment.leading) {
169+
return;
170+
}
171+
comment.printed = true;
172+
parts.push(options.printer.printComment(commentPath));
173+
}, 'comments');
174+
}
175+
176+
parts.push(line);
177+
parts.push('}');
178+
179+
return concat(parts);
180+
}
160181
case 'EventDefinition':
161182
return concat([
162183
'event ',

tests/Inbox/Inbox.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,15 @@ contract Inbox {
1919
function setMessage(string newMessage) public {
2020
message = newMessage;
2121
}
22+
23+
function nothingHere() public {
24+
// to be defined
25+
}
26+
27+
function nothingHereMultipleComments() public {
28+
// to be defined
29+
// to be defined 2
30+
/* to be defined 3 */
31+
}
2232
}
2333

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ contract Inbox {
2222
function setMessage(string newMessage) public {
2323
message = newMessage;
2424
}
25+
26+
function nothingHere() public {
27+
// to be defined
28+
}
29+
30+
function nothingHereMultipleComments() public {
31+
// to be defined
32+
// to be defined 2
33+
/* to be defined 3 */
34+
}
2535
}
2636
2737
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -46,6 +56,16 @@ contract Inbox {
4656
function setMessage(string newMessage) public {
4757
message = newMessage;
4858
}
59+
60+
function nothingHere() public {
61+
// to be defined
62+
}
63+
64+
function nothingHereMultipleComments() public {
65+
// to be defined
66+
// to be defined 2
67+
/* to be defined 3 */
68+
}
4969
}
5070
5171
`;

0 commit comments

Comments
 (0)