Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pretty-printer): format empty blocks without extra empty line #1346

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 'The "remainder" field can only be the last field:' inspection now shows location: PR [#1300](https://github.com/tact-lang/tact/pull/1300)
- Forbid "remainder" field at the middle of a contract storage: PR [#1301](https://github.com/tact-lang/tact/pull/1301)
- Forbid the `override` modifier for functions without the corresponding super-function: PR [#1302](https://github.com/tact-lang/tact/pull/1302)
- Format empty blocks without extra empty line: PR [#1346](https://github.com/tact-lang/tact/pull/1346)

### Docs

Expand Down
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) { }
}
}
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
Loading