Skip to content

Commit 4747594

Browse files
authored
fix(pretty-printer): format empty blocks without extra empty line (#1346)
1 parent 5aaa18d commit 4747594

8 files changed

Lines changed: 15 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5151
- 'The "remainder" field can only be the last field:' inspection now shows location: PR [#1300](https://github.com/tact-lang/tact/pull/1300)
5252
- Forbid "remainder" field at the middle of a contract storage: PR [#1301](https://github.com/tact-lang/tact/pull/1301)
5353
- Forbid the `override` modifier for functions without the corresponding super-function: PR [#1302](https://github.com/tact-lang/tact/pull/1302)
54+
- Format empty blocks without extra empty line: PR [#1346](https://github.com/tact-lang/tact/pull/1346)
5455

5556
### Docs
5657

src/prettyPrinter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,9 @@ const createContext = (spaces: number): Context<ContextModel> => {
380380
const indent = (rows: readonly ContextModel[]) =>
381381
block(rows).map((f) => (level: number) => f(level + 1));
382382
const braced = (rows: readonly ContextModel[]) =>
383-
block([row(`{`), indent(rows), row(`}`)]);
383+
block(
384+
rows.length > 0 ? [row(`{`), indent(rows), row(`}`)] : [row("{ }")],
385+
);
384386
const list = <T>(items: readonly T[], print: Printer<T>) =>
385387
items.map((node) => print(node)(ctx));
386388
const grouped = <T, V>({
@@ -695,7 +697,7 @@ export const ppAstFuncId = (func: A.AstFuncId): string => func.text;
695697
//
696698

697699
export const ppStatementBlock: Printer<A.AstStatement[]> = (stmts) => (c) =>
698-
c.braced(stmts.length === 0 ? [c.row("")] : c.list(stmts, ppAstStatement));
700+
c.braced(stmts.length === 0 ? [] : c.list(stmts, ppAstStatement));
699701

700702
export const ppAsmInstructionsBlock: Printer<A.AstAsmInstruction[]> =
701703
(instructions) => (c) =>

src/test/contracts/case-bin-ops.tact

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ contract SampleContract {
33

44
init() {
55
self.a = (1 + 2 - 3) / 4 % 5 | 255 & 53 ^ 2;
6-
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) {
7-
8-
}
6+
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) { }
97
}
10-
}
8+
}

src/test/contracts/case-message-opcode.tact

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ message(DEADBEEF + 1) MyMessageWithExprOpcode {
3030
a: Int;
3131
}
3232

33-
contract TestContract {
34-
}
33+
contract TestContract { }

src/test/contracts/case-traits.tact

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
@interface("") trait B {
2-
}
1+
@interface("") trait B { }
32

43
trait C {
54
abstract get fun d(e: String): String;
@@ -12,9 +11,7 @@ trait Ownable with B {
1211
const someNum: Int = 2;
1312
abstract const something: Int;
1413

15-
receive("message") {
16-
17-
}
14+
receive("message") { }
1815

1916
fun requireOwner() {
2017
nativeThrowUnless(132, context().sender == self.owner);
@@ -35,4 +32,4 @@ trait Ownable with B {
3532
self.owner = owner;
3633
self.value = 1;
3734
}
38-
}
35+
}

src/test/contracts/renamer-expected/case-bin-ops.tact

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ contract contract_0 {
33

44
init() {
55
self.a = (1 + 2 - 3) / 4 % 5 | 255 & 53 ^ 2;
6-
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) {
7-
8-
}
6+
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) { }
97
}
108
}

src/test/contracts/renamer-expected/case-message-opcode.tact

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ message(constant_def_5 + 1) message_decl_6 {
3030

3131
const constant_def_5: Int = 0xdeadbeef;
3232

33-
contract contract_7 {
34-
}
33+
contract contract_7 { }

src/test/contracts/renamer-expected/case-traits.tact

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
@interface("") trait trait_0 {
2-
}
1+
@interface("") trait trait_0 { }
32

43
trait trait_1 {
54
abstract get fun function_decl_2(e: String): String;
@@ -12,9 +11,7 @@ trait trait_3 with B {
1211
const constant_def_4: Int = 2;
1312
abstract const constant_decl_5: Int;
1413

15-
receive("message") {
16-
17-
}
14+
receive("message") { }
1815

1916
fun function_def_6() {
2017
nativeThrowUnless(132, context().sender == self.owner);

0 commit comments

Comments
 (0)