Skip to content

Commit bf206d4

Browse files
committed
docs: add async examples and clarify queueMicrotask
Add code examples for async function translation based on the async_basic integration test. Clarify that `go` statements are translated using `queueMicrotask`. Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 7fe5216 commit bf206d4

4 files changed

Lines changed: 107 additions & 24 deletions

File tree

compiler/compile_stmt.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ func (c *GoToTSCompiler) WriteStmtSwitch(exp *ast.SwitchStmt) error {
147147
if err := c.WriteValueExpr(exp.Tag); err != nil {
148148
return fmt.Errorf("failed to write switch tag expression: %w", err)
149149
}
150+
} else {
151+
c.tsw.WriteLiterally("true") // Write 'true' for switch without expression
150152
}
151153
c.tsw.WriteLiterally(") {")
152154
c.tsw.WriteLine("")

compliance/tests/switch_statement/expected.log

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ Integer switch:
22
two
33

44
String switch:
5-
hello
5+
hello
6+
7+
Switch without expression:
8+
negative
9+
10+
Switch without expression (zero):
11+
zero
12+
13+
Switch without expression (positive):
14+
positive

compliance/tests/switch_statement/switch_statement.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,36 @@ func main() {
2424
default:
2525
println("other string")
2626
}
27+
x := -5
28+
println("\nSwitch without expression:")
29+
switch {
30+
case x < 0:
31+
println("negative")
32+
case x == 0:
33+
println("zero")
34+
default: // x > 0
35+
println("positive")
36+
}
37+
38+
x = 0
39+
println("\nSwitch without expression (zero):")
40+
switch {
41+
case x < 0:
42+
println("negative")
43+
case x == 0:
44+
println("zero")
45+
default: // x > 0
46+
println("positive")
47+
}
48+
49+
x = 10
50+
println("\nSwitch without expression (positive):")
51+
switch {
52+
case x < 0:
53+
println("negative")
54+
case x == 0:
55+
println("zero")
56+
default: // x > 0
57+
println("positive")
58+
}
2759
}

compliance/tests/switch_statement/switch_statement.gs.ts

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,72 @@ export async function main(): Promise<void> {
77
let i = 2
88
console.log("Integer switch:")
99
switch (i) {
10-
case 1:
11-
console.log("one")
12-
break
13-
case 2:
14-
console.log("two")
15-
break
16-
case 3:
17-
console.log("three")
18-
break
19-
default:
20-
console.log("other integer")
21-
break
10+
case 1:
11+
console.log("one")
12+
break
13+
case 2:
14+
console.log("two")
15+
break
16+
case 3:
17+
console.log("three")
18+
break
19+
default:
20+
console.log("other integer")
21+
break
2222
}
23-
23+
2424
let s = "hello"
2525
console.log("\nString switch:")
2626
switch (s) {
27-
case "world":
28-
console.log("world")
29-
break
30-
case "hello":
31-
console.log("hello")
32-
break
33-
default:
34-
console.log("other string")
35-
break
27+
case "world":
28+
console.log("world")
29+
break
30+
case "hello":
31+
console.log("hello")
32+
break
33+
default:
34+
console.log("other string")
35+
break
36+
}
37+
let x = -5
38+
console.log("\nSwitch without expression:")
39+
switch (true) {
40+
case x < 0:
41+
console.log("negative")
42+
break
43+
case x == 0:
44+
console.log("zero")
45+
break
46+
default: // x > 0
47+
console.log("positive")
48+
break
3649
}
37-
}
3850

51+
x = 0
52+
console.log("\nSwitch without expression (zero):")
53+
switch (true) {
54+
case x < 0:
55+
console.log("negative")
56+
break
57+
case x == 0:
58+
console.log("zero")
59+
break
60+
default: // x > 0
61+
console.log("positive")
62+
break
63+
}
64+
65+
x = 10
66+
console.log("\nSwitch without expression (positive):")
67+
switch (true) {
68+
case x < 0:
69+
console.log("negative")
70+
break
71+
case x == 0:
72+
console.log("zero")
73+
break
74+
default: // x > 0
75+
console.log("positive")
76+
break
77+
}
78+
}

0 commit comments

Comments
 (0)