Skip to content

Commit

Permalink
fix(pretty-printer): format empty blocks without extra empty line
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Jan 16, 2025
1 parent 5aaa18d commit 07fc449
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/prettyPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ const createContext = (spaces: number): Context<ContextModel> => {
const indent = (rows: readonly ContextModel[]) =>
block(rows).map((f) => (level: number) => f(level + 1));
const braced = (rows: readonly ContextModel[]) =>
block([row(`{`), indent(rows), row(`}`)]);
block(
rows.length > 0 ? [row(`{`), indent(rows), row(`}`)] : [row("{}")],
);
const list = <T>(items: readonly T[], print: Printer<T>) =>
items.map((node) => print(node)(ctx));
const grouped = <T, V>({
Expand Down Expand Up @@ -695,7 +697,7 @@ export const ppAstFuncId = (func: A.AstFuncId): string => func.text;
//

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

export const ppAsmInstructionsBlock: Printer<A.AstAsmInstruction[]> =
(instructions) => (c) =>
Expand Down
6 changes: 2 additions & 4 deletions src/test/contracts/case-bin-ops.tact
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ contract SampleContract {

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

}
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) {}
}
}
}
3 changes: 1 addition & 2 deletions src/test/contracts/case-message-opcode.tact
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ message(DEADBEEF + 1) MyMessageWithExprOpcode {
a: Int;
}

contract TestContract {
}
contract TestContract {}
9 changes: 3 additions & 6 deletions src/test/contracts/case-traits.tact
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@interface("") trait B {
}
@interface("") trait B {}

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

receive("message") {

}
receive("message") {}

fun requireOwner() {
nativeThrowUnless(132, context().sender == self.owner);
Expand All @@ -35,4 +32,4 @@ trait Ownable with B {
self.owner = owner;
self.value = 1;
}
}
}
4 changes: 1 addition & 3 deletions src/test/contracts/renamer-expected/case-bin-ops.tact
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ contract contract_0 {

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

}
if (1 > 2 || 3 == 0 && (5 - 3) * 10 > 0) {}
}
}
3 changes: 1 addition & 2 deletions src/test/contracts/renamer-expected/case-message-opcode.tact
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ message(constant_def_5 + 1) message_decl_6 {

const constant_def_5: Int = 0xdeadbeef;

contract contract_7 {
}
contract contract_7 {}
7 changes: 2 additions & 5 deletions src/test/contracts/renamer-expected/case-traits.tact
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@interface("") trait trait_0 {
}
@interface("") trait trait_0 {}

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

receive("message") {

}
receive("message") {}

fun function_def_6() {
nativeThrowUnless(132, context().sender == self.owner);
Expand Down

0 comments on commit 07fc449

Please sign in to comment.