Skip to content

Commit cb01e14

Browse files
authored
Revert how if-else comments are handled (#1036)
1 parent 2b27e68 commit cb01e14

File tree

7 files changed

+81
-70
lines changed

7 files changed

+81
-70
lines changed

src/nodes/ExpressionStatement.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
import { doc } from 'prettier';
2+
import { printComments } from '../common/printer-helpers.js';
3+
4+
const { hardline } = doc.builders;
5+
16
export const ExpressionStatement = {
2-
print: ({ node, path, print }) => [
3-
path.call(print, 'expression'),
4-
node.omitSemicolon ? '' : ';'
5-
]
7+
print: ({ node, options, path, print }) => {
8+
const parts = [];
9+
10+
const parent = path.getParentNode();
11+
12+
if (parent.type === 'IfStatement') {
13+
if (node.comments?.length) {
14+
const comments = printComments(node, path, options);
15+
if (comments?.length) {
16+
parts.push(comments);
17+
parts.push(hardline);
18+
}
19+
}
20+
}
21+
22+
parts.push(path.call(print, 'expression'));
23+
parts.push(node.omitSemicolon ? '' : ';');
24+
25+
return parts;
26+
}
627
};

src/nodes/IfStatement.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { doc } from 'prettier';
2-
import { printSeparatedItem } from '../common/printer-helpers.js';
2+
import {
3+
printComments,
4+
printSeparatedItem
5+
} from '../common/printer-helpers.js';
36

47
const { group, hardline, indent, line } = doc.builders;
58

@@ -19,9 +22,10 @@ const printFalseBody = (node, path, print) =>
1922
? [' ', path.call(print, 'falseBody')]
2023
: group(indent([line, path.call(print, 'falseBody')]));
2124

22-
const printElse = (node, path, print) => {
25+
const printElse = (node, path, print, commentsBetweenIfAndElse) => {
2326
if (node.falseBody) {
24-
const elseOnSameLine = node.trueBody.type === 'Block';
27+
const elseOnSameLine =
28+
node.trueBody.type === 'Block' && commentsBetweenIfAndElse.length === 0;
2529
return [
2630
elseOnSameLine ? ' ' : hardline,
2731
'else',
@@ -32,11 +36,22 @@ const printElse = (node, path, print) => {
3236
};
3337

3438
export const IfStatement = {
35-
print: ({ node, path, print }) => [
36-
'if (',
37-
printSeparatedItem(path.call(print, 'condition')),
38-
')',
39-
printTrueBody(node, path, print),
40-
printElse(node, path, print)
41-
]
39+
print: ({ node, options, path, print }) => {
40+
const comments = node.comments || [];
41+
const commentsBetweenIfAndElse = comments.filter(
42+
(comment) => !comment.leading && !comment.trailing
43+
);
44+
45+
const parts = [];
46+
47+
parts.push('if (', printSeparatedItem(path.call(print, 'condition')), ')');
48+
parts.push(printTrueBody(node, path, print));
49+
if (commentsBetweenIfAndElse.length && node.falseBody) {
50+
parts.push(hardline);
51+
parts.push(printComments(node, path, options));
52+
}
53+
parts.push(printElse(node, path, print, commentsBetweenIfAndElse));
54+
55+
return parts;
56+
}
4257
};

src/prettier-comments/language-js/comments.js

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -257,51 +257,21 @@ function handleIfStatementComments(
257257
precedingNode === enclosingNode.trueBody &&
258258
followingNode === enclosingNode.falseBody
259259
) {
260-
if (followingNode.type === "Block") {
261-
if(followingNode.statements.length === 0) {
262-
addDanglingComment(followingNode, comment);
263-
} else {
264-
addLeadingComment(followingNode.statements[followingNode.statements.length - 1], comment);
265-
}
266-
} else if (followingNode.type === "IfStatement") {
267-
if (followingNode.trueBody.type === "Block") {
268-
if(followingNode.trueBody.statements.length === 0) {
269-
addDanglingComment(followingNode.trueBody, comment);
270-
} else {
271-
addLeadingComment(followingNode.trueBody.statements[0], comment);
272-
}
273-
} else {
274-
addLeadingComment(followingNode.trueBody, comment);
275-
}
260+
if (precedingNode.type === "ExpressionStatement") {
261+
addTrailingComment(precedingNode, comment);
276262
} else {
277-
addLeadingComment(precedingNode, comment);
263+
addDanglingComment(enclosingNode, comment);
278264
}
279265
return true;
280266
}
281267

282-
if (enclosingNode.trueBody === followingNode) {
283-
if (followingNode.type === "Block") {
284-
if(followingNode.statements.length === 0) {
285-
addDanglingComment(followingNode, comment);
286-
} else {
287-
addLeadingComment(followingNode.statements[0], comment);
288-
}
289-
} else {
290-
addLeadingComment(followingNode, comment);
291-
}
268+
if (followingNode.type === "ExpressionStatement") {
269+
addBlockStatementFirstComment(followingNode, comment);
292270
return true;
293271
}
294272

295273
if (followingNode.type === "IfStatement") {
296-
if (followingNode.trueBody.type === "Block") {
297-
if(followingNode.trueBody.statements.length === 0) {
298-
addDanglingComment(followingNode.trueBody, comment);
299-
} else {
300-
addLeadingComment(followingNode.trueBody.statements[0], comment);
301-
}
302-
} else {
303-
addLeadingComment(followingNode.trueBody, comment);
304-
}
274+
addBlockOrNotComment(followingNode.trueBody, comment);
305275
return true;
306276
}
307277

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,18 @@ contract Comments7 {
260260
261261
contract Comments8 {
262262
function someFunction() {
263-
if (something) {} else {
264-
// comment
265-
}
263+
if (something) {}
264+
// comment
265+
else {}
266266
}
267267
}
268268
269269
contract Comments8 {
270270
function someFunction() {
271-
if (something) {} else {
272-
/* comment
273-
* comment */
274-
}
271+
if (something) {}
272+
/* comment
273+
* comment */
274+
else {}
275275
}
276276
}
277277

tests/format/Etc/__snapshots__/jsfmt.spec.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ contract Contract {
129129
}
130130
131131
function fun(uint256 a) returns (uint) {
132-
if (something) foo();
133-
else if (somethingElse)
132+
if (something)
133+
foo();
134134
// comment
135-
bar();
135+
else if (somethingElse) bar();
136136
else whatever();
137137
return;
138138
}

tests/format/IfStatements/__snapshots__/jsfmt.spec.js.snap

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,17 @@ contract IfStatements {
558558
// Case 0-9
559559
if (47 < c && c < 58) {
560560
return (true, c - 48);
561-
} else if (64 < c && c < 71) {
562-
// Case A-F
561+
}
562+
// Case A-F
563+
else if (64 < c && c < 71) {
563564
return (true, c - 55);
564-
} else if (96 < c && c < 103) {
565-
// Case a-f
565+
}
566+
// Case a-f
567+
else if (96 < c && c < 103) {
566568
return (true, c - 87);
567-
} else {
568-
// Else: not a hex char
569+
}
570+
// Else: not a hex char
571+
else {
569572
return (false, 0);
570573
}
571574
}

tests/format/IndexOf/__snapshots__/jsfmt.spec.js.snap

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ contract IndexOf {
9999
if (a.length < 1 || b.length < 1 || (b.length > a.length)) {
100100
whatwastheval = -1;
101101
return -1;
102-
} else if (a.length > (2 ** 128 - 1)) {
103-
// since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1)
102+
} else if (
103+
a.length > (2 ** 128 - 1)
104+
) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1)
105+
{
104106
whatwastheval = -1;
105107
return -1;
106108
} else {
107109
uint subindex = 0;
108110
for (uint i = 0; i < a.length; i++) {
109-
if (a[i] == b[0]) {
110-
// found the first char of b
111+
if (a[i] == b[0]) // found the first char of b
112+
{
111113
subindex = 1;
112114
while (
113115
subindex < b.length &&

0 commit comments

Comments
 (0)