Skip to content

Commit 77f5c56

Browse files
committed
Merge remote-tracking branch 'origin/main' into slang
# Conflicts: # package-lock.json # package.json
2 parents d9a1be2 + 889a5bb commit 77f5c56

9 files changed

Lines changed: 84 additions & 80 deletions

File tree

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

src/slang-comments/handlers/handle-if-statement-comments.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,7 @@ export default function handleIfStatementComments({
4949
precedingNode === enclosingNode.body &&
5050
followingNode === enclosingNode.elseBranch
5151
) {
52-
if (followingNode.body.variant.kind === NonterminalKind.Block) {
53-
addHubNodeFirstComment(followingNode.body.variant.statements, comment);
54-
} else if (
55-
followingNode.body.variant.kind === NonterminalKind.IfStatement
56-
) {
57-
addIfStatementBodyFirstComment(
58-
followingNode.body.variant.body.variant,
59-
comment
60-
);
61-
} else {
62-
addLeadingComment(followingNode.body.variant, comment);
63-
}
52+
addTrailingComment(precedingNode.variant, comment);
6453
return true;
6554
}
6655

src/slang-nodes/IfStatement.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { doc } from 'prettier';
22
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
33
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
44
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
5+
import { isLineComment } from '../slang-utils/is-comment.js';
56
import { Expression } from './Expression.js';
67
import { Statement } from './Statement.js';
78
import { ElseBranch } from './ElseBranch.js';
@@ -62,8 +63,12 @@ export class IfStatement implements SlangNode {
6263
}),
6364
this.elseBranch
6465
? [
65-
this.body.variant.kind !== NonterminalKind.Block
66-
? hardline // else on a new line if body is not a block
66+
this.body.variant.kind !== NonterminalKind.Block || // else on a new line if body is not a block
67+
this.body.variant.comments.some(
68+
(comment) =>
69+
isLineComment(comment) || comment.placement === 'ownLine'
70+
) // or if body has trailing single line comments or a block comment on a new line
71+
? hardline
6772
: ' ',
6873
path.call(print, 'elseBranch')
6974
]

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface BaseComment {
3131
leading?: boolean;
3232
trailing?: boolean;
3333
printed?: boolean;
34+
placement?: 'endOfLine' | 'ownLine' | 'remaining';
3435
precedingNode?: StrictAstNode;
3536
enclosingNode?: StrictAstNode;
3637
followingNode?: StrictAstNode;

tests/format/Comments/__snapshots__/format.test.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__/format.test.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__/format.test.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
}

0 commit comments

Comments
 (0)