Skip to content

Commit 81bac4a

Browse files
committed
fix: statement formatting tweaks
Signed-off-by: Christian Stewart <[email protected]>
1 parent c01d0f7 commit 81bac4a

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

compiler/stmt.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,19 @@ func (c *GoToTSCompiler) WriteStmtSend(exp *ast.SendStmt) error {
346346
// The function aims to produce idiomatic TypeScript `if/else if/else` structures.
347347
func (s *GoToTSCompiler) WriteStmtIf(exp *ast.IfStmt) error {
348348
if exp.Init != nil {
349-
s.tsw.WriteLiterally("{")
350-
s.tsw.Indent(1)
349+
s.tsw.WriteLiterally("{") // Write opening brace
350+
s.tsw.WriteLine("") // Add newline immediately after opening brace
351+
s.tsw.Indent(1) // Indent for the initializer
351352

352-
if err := s.WriteStmt(exp.Init); err != nil {
353+
if err := s.WriteStmt(exp.Init); err != nil { // Write the initializer
353354
return err
354355
}
355356

357+
// This defer handles closing the synthetic block for the initializer
356358
defer func() {
357359
s.tsw.Indent(-1)
358-
s.tsw.WriteLiterally("}")
360+
s.tsw.WriteLiterally("}") // Write the closing brace at the now-correct indent level
361+
s.tsw.WriteLine("") // Ensure a newline *after* this '}', critical for preventing '}}'
359362
}()
360363
}
361364

@@ -696,6 +699,7 @@ func (c *GoToTSCompiler) WriteStmtDefer(exp *ast.DeferStmt) error {
696699
if err := c.WriteValueExpr(exp.Call); err != nil {
697700
return fmt.Errorf("failed to write deferred call: %w", err)
698701
}
702+
c.tsw.WriteLine("")
699703
}
700704

701705
c.tsw.Indent(-1)

compliance/tests/chan_type_assertion/chan_type_assertion.gs.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,70 +10,88 @@ export function main(): void {
1010
let ch4 = $.makeChannel<{ }>(0, {}, 'both')
1111

1212
let i: null | any = ch1
13-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'both'})
13+
{
14+
let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'both'})
1415
if (ok) {
1516
console.log("i is chan int: ok")
1617
} else {
1718
console.log("i is chan int: failed")
1819
}
1920
}
21+
2022
let s: null | any = ch2
21-
{let { ok: ok } = $.typeAssert<$.Channel<string>>(s, {kind: $.TypeKind.Channel, elemType: 'string', direction: 'send'})
23+
{
24+
let { ok: ok } = $.typeAssert<$.Channel<string>>(s, {kind: $.TypeKind.Channel, elemType: 'string', direction: 'send'})
2225
if (ok) {
2326
console.log("s is chan<- string: ok")
2427
} else {
2528
console.log("s is chan<- string: failed")
2629
}
2730
}
31+
2832
let r: null | any = ch3
29-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(r, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
33+
{
34+
let { ok: ok } = $.typeAssert<$.Channel<number>>(r, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
3035
if (ok) {
3136
console.log("r is <-chan float64: ok")
3237
} else {
3338
console.log("r is <-chan float64: failed")
3439
}
3540
}
41+
3642
let e: null | any = ch4
37-
{let { ok: ok } = $.typeAssert<$.Channel<{ }>>(e, {kind: $.TypeKind.Channel, elemType: {kind: $.TypeKind.Struct, fields: {}, methods: []}, direction: 'both'})
43+
{
44+
let { ok: ok } = $.typeAssert<$.Channel<{ }>>(e, {kind: $.TypeKind.Channel, elemType: {kind: $.TypeKind.Struct, fields: {}, methods: []}, direction: 'both'})
3845
if (ok) {
3946
console.log("e is chan struct{}: ok")
4047
} else {
4148
console.log("e is chan struct{}: failed")
4249
}
4350
}
44-
{let { ok: ok } = $.typeAssert<$.Channel<string>>(i, {kind: $.TypeKind.Channel, elemType: 'string', direction: 'both'})
51+
52+
{
53+
let { ok: ok } = $.typeAssert<$.Channel<string>>(i, {kind: $.TypeKind.Channel, elemType: 'string', direction: 'both'})
4554
if (ok) {
4655
console.log("i is chan string: incorrect")
4756
} else {
4857
console.log("i is chan string: correctly failed")
4958
}
5059
}
51-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'send'})
60+
61+
{
62+
let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'send'})
5263
if (ok) {
5364
console.log("i is chan<- int: incorrect")
5465
} else {
5566
console.log("i is chan<- int: correctly failed")
5667
}
5768
}
58-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
69+
70+
{
71+
let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
5972
if (ok) {
6073
console.log("i is <-chan int: incorrect")
6174
} else {
6275
console.log("i is <-chan int: correctly failed")
6376
}
6477
}
65-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'send'})
78+
79+
{
80+
let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'send'})
6681
if (ok) {
6782
console.log("bidirectional can be used as send-only: ok")
6883
} else {
6984
console.log("bidirectional can be used as send-only: failed")
7085
}
7186
}
72-
{let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
87+
88+
{
89+
let { ok: ok } = $.typeAssert<$.Channel<number>>(i, {kind: $.TypeKind.Channel, elemType: 'number', direction: 'receive'})
7390
if (ok) {
7491
console.log("bidirectional can be used as receive-only: ok")
7592
} else {
7693
console.log("bidirectional can be used as receive-only: failed")
7794
}
78-
}}
95+
}
96+
}
7997

compliance/tests/defer_statement/defer_statement.gs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as $ from "@goscript/builtin";
66
export function main(): void {
77
using __defer = new $.DisposableStack();
88
__defer.defer(() => {
9-
console.log("deferred")});
9+
console.log("deferred")
10+
});
1011
console.log("main")
1112
}
1213

0 commit comments

Comments
 (0)