Skip to content

Commit eff507c

Browse files
committed
Adding comments between trueBody and elseBranch at the beginning of the else branch
1 parent d8f58bf commit eff507c

7 files changed

Lines changed: 123 additions & 23 deletions

File tree

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { util } from 'prettier';
33
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
44
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
5-
import addHubNodeLastComment from './add-hub-node-last-comment.js';
65

76
import type { HandlerParams } from './types';
7+
import type { StatementVariant } from '../../slang-nodes/index.js';
8+
import type { Comment } from '../../types.js';
89

910
const { addLeadingComment, addTrailingComment } = util;
1011

12+
function addIfStatementBodyFirstComment(
13+
node: StatementVariant,
14+
comment: Comment
15+
): void {
16+
if (node.kind === NonterminalKind.Block) {
17+
addHubNodeFirstComment(node.statements, comment);
18+
} else {
19+
addLeadingComment(node, comment);
20+
}
21+
}
22+
1123
export default function handleIfStatementComments({
1224
text,
1325
precedingNode,
@@ -31,26 +43,29 @@ export default function handleIfStatementComments({
3143
}
3244

3345
// Comments before `else`:
34-
// - treat as trailing comments of the consequent, if it's a BlockStatement
46+
// - treat as leading comments of the elseBranch, if it's a BlockStatement
3547
// - treat as a dangling comment otherwise
3648
if (
3749
precedingNode === enclosingNode.body &&
3850
followingNode === enclosingNode.elseBranch
3951
) {
40-
if (precedingNode.variant.kind === NonterminalKind.Block) {
41-
addHubNodeLastComment(precedingNode.variant.statements, comment);
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+
);
4261
} else {
43-
addTrailingComment(precedingNode, comment);
62+
addLeadingComment(followingNode.body.variant, comment);
4463
}
4564
return true;
4665
}
4766

4867
if (followingNode.kind === NonterminalKind.IfStatement) {
49-
if (followingNode.body.variant.kind === NonterminalKind.Block) {
50-
addHubNodeFirstComment(followingNode.body.variant.statements, comment);
51-
} else {
52-
addLeadingComment(followingNode.body.variant, comment);
53-
}
68+
addIfStatementBodyFirstComment(followingNode.body.variant, comment);
5469
return true;
5570
}
5671

src/slang-nodes/YulForStatement.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { doc } from 'prettier';
23
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
34
import { YulBlock } from './YulBlock.js';
45
import { YulExpression } from './YulExpression.js';
@@ -8,6 +9,8 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
89
import type { AstNode } from '../slang-nodes';
910
import type { PrintFunction, SlangNode } from '../types';
1011

12+
const { join } = doc.builders;
13+
1114
export class YulForStatement implements SlangNode {
1215
readonly kind = NonterminalKind.YulForStatement;
1316

@@ -48,15 +51,12 @@ export class YulForStatement implements SlangNode {
4851
}
4952

5053
print(path: AstPath<YulForStatement>, print: PrintFunction): Doc {
51-
return [
52-
'for ',
54+
return join(' ', [
55+
'for',
5356
path.call(print, 'initialization'),
54-
' ',
5557
path.call(print, 'condition'),
56-
' ',
5758
path.call(print, 'iterator'),
58-
' ',
5959
path.call(print, 'body')
60-
];
60+
]);
6161
}
6262
}

src/slang-nodes/YulVariableAssignmentStatement.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { doc } from 'prettier';
23
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
34
import { YulPaths } from './YulPaths.js';
45
import { YulAssignmentOperator } from './YulAssignmentOperator.js';
@@ -9,6 +10,8 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
910
import type { AstNode } from '../slang-nodes';
1011
import type { PrintFunction, SlangNode } from '../types';
1112

13+
const { join } = doc.builders;
14+
1215
export class YulVariableAssignmentStatement implements SlangNode {
1316
readonly kind = NonterminalKind.YulVariableAssignmentStatement;
1417

@@ -48,12 +51,10 @@ export class YulVariableAssignmentStatement implements SlangNode {
4851
path: AstPath<YulVariableAssignmentStatement>,
4952
print: PrintFunction
5053
): Doc {
51-
return [
54+
return join(' ', [
5255
path.call(print, 'variables'),
53-
' ',
5456
path.call(print, 'assignment'),
55-
' ',
5657
path.call(print, 'expression')
57-
];
58+
]);
5859
}
5960
}

src/slang-nodes/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ export type HubNode =
286286
| YulSwitchCases
287287
| YulVariableNames;
288288

289+
export type StatementVariant =
290+
| ExpressionStatement
291+
| VariableDeclarationStatement
292+
| TupleDeconstructionStatement
293+
| IfStatement
294+
| ForStatement
295+
| WhileStatement
296+
| DoWhileStatement
297+
| ContinueStatement
298+
| BreakStatement
299+
| ReturnStatement
300+
| ThrowStatement
301+
| EmitStatement
302+
| TryStatement
303+
| RevertStatement
304+
| AssemblyStatement
305+
| Block
306+
| UncheckedBlock;
307+
289308
export type AstNode =
290309
| Identifier
291310
| YulIdentifier

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)
133-
foo();
132+
if (something) foo();
133+
else if (somethingElse)
134134
// comment
135-
else if (somethingElse) bar();
135+
bar();
136136
else whatever();
137137
return;
138138
}

tests/format/IfStatements/IfStatements.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,27 @@ contract IfStatements {
109109

110110
if (simpleIf) {if (simpleIfWithinIf) {return true;} else {return false;}} else return false;
111111
}
112+
113+
function _tryHexToUint(bytes1 char) private pure returns (bool, uint8) {
114+
uint8 c = uint8(char);
115+
unchecked {
116+
// Case 0-9
117+
if (47 < c && c < 58) {
118+
return (true, c - 48);
119+
}
120+
// Case A-F
121+
else if (64 < c && c < 71) {
122+
return (true, c - 55);
123+
}
124+
// Case a-f
125+
else if (96 < c && c < 103) {
126+
return (true, c - 87);
127+
}
128+
// Else: not a hex char
129+
else {
130+
return (false, 0);
131+
}
132+
}
133+
}
134+
112135
}

tests/format/IfStatements/__snapshots__/format.test.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ contract IfStatements {
117117
118118
if (simpleIf) {if (simpleIfWithinIf) {return true;} else {return false;}} else return false;
119119
}
120+
121+
function _tryHexToUint(bytes1 char) private pure returns (bool, uint8) {
122+
uint8 c = uint8(char);
123+
unchecked {
124+
// Case 0-9
125+
if (47 < c && c < 58) {
126+
return (true, c - 48);
127+
}
128+
// Case A-F
129+
else if (64 < c && c < 71) {
130+
return (true, c - 55);
131+
}
132+
// Case a-f
133+
else if (96 < c && c < 103) {
134+
return (true, c - 87);
135+
}
136+
// Else: not a hex char
137+
else {
138+
return (false, 0);
139+
}
140+
}
141+
}
142+
120143
}
121144
122145
=====================================output=====================================
@@ -528,6 +551,25 @@ contract IfStatements {
528551
}
529552
} else return false;
530553
}
554+
555+
function _tryHexToUint(bytes1 char) private pure returns (bool, uint8) {
556+
uint8 c = uint8(char);
557+
unchecked {
558+
// Case 0-9
559+
if (47 < c && c < 58) {
560+
return (true, c - 48);
561+
} else if (64 < c && c < 71) {
562+
// Case A-F
563+
return (true, c - 55);
564+
} else if (96 < c && c < 103) {
565+
// Case a-f
566+
return (true, c - 87);
567+
} else {
568+
// Else: not a hex char
569+
return (false, 0);
570+
}
571+
}
572+
}
531573
}
532574
533575
================================================================================

0 commit comments

Comments
 (0)