goscript 0.0.33 → 0.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/compiler/analysis.go +2 -2
  2. package/compiler/assignment.go +26 -0
  3. package/compiler/builtin_test.go +2 -0
  4. package/compiler/compiler.go +12 -2
  5. package/compiler/compiler_test.go +0 -53
  6. package/compiler/expr-call.go +121 -2
  7. package/compiler/expr.go +66 -1
  8. package/compiler/lit.go +1 -1
  9. package/compiler/stmt-assign.go +106 -90
  10. package/compiler/stmt-for.go +78 -1
  11. package/compiler/stmt-range.go +333 -461
  12. package/compiler/stmt.go +20 -0
  13. package/compiler/type.go +11 -8
  14. package/dist/gs/builtin/builtin.d.ts +7 -0
  15. package/dist/gs/builtin/builtin.js +30 -0
  16. package/dist/gs/builtin/builtin.js.map +1 -1
  17. package/dist/gs/builtin/map.d.ts +4 -4
  18. package/dist/gs/builtin/map.js +12 -6
  19. package/dist/gs/builtin/map.js.map +1 -1
  20. package/dist/gs/builtin/slice.d.ts +7 -7
  21. package/dist/gs/builtin/slice.js +19 -9
  22. package/dist/gs/builtin/slice.js.map +1 -1
  23. package/dist/gs/maps/index.d.ts +2 -0
  24. package/dist/gs/maps/index.js +3 -0
  25. package/dist/gs/maps/index.js.map +1 -0
  26. package/dist/gs/maps/iter.gs.d.ts +7 -0
  27. package/dist/gs/maps/iter.gs.js +65 -0
  28. package/dist/gs/maps/iter.gs.js.map +1 -0
  29. package/dist/gs/maps/maps.gs.d.ts +7 -0
  30. package/dist/gs/maps/maps.gs.js +79 -0
  31. package/dist/gs/maps/maps.gs.js.map +1 -0
  32. package/dist/gs/slices/slices.d.ts +6 -0
  33. package/dist/gs/slices/slices.js +8 -0
  34. package/dist/gs/slices/slices.js.map +1 -1
  35. package/gs/builtin/builtin.ts +38 -0
  36. package/gs/builtin/map.ts +10 -9
  37. package/gs/builtin/slice.ts +23 -11
  38. package/gs/maps/index.ts +2 -0
  39. package/gs/maps/iter.gs.ts +71 -0
  40. package/gs/maps/maps.gs.ts +87 -0
  41. package/gs/slices/slices.ts +9 -0
  42. package/package.json +1 -1
@@ -4,6 +4,7 @@ import (
4
4
  "fmt"
5
5
  "go/ast"
6
6
  "go/token"
7
+ "go/types"
7
8
 
8
9
  "github.com/pkg/errors"
9
10
  )
@@ -64,7 +65,25 @@ func (c *GoToTSCompiler) WriteStmtForInit(stmt ast.Stmt) error {
64
65
  case *ast.AssignStmt:
65
66
  // Handle assignment in init (e.g., i := 0 or i = 0)
66
67
  // For TypeScript for-loop init, we need to handle multi-variable declarations differently
67
- if s.Tok == token.DEFINE && len(s.Lhs) > 1 && len(s.Rhs) > 0 {
68
+ if s.Tok == token.DEFINE && len(s.Lhs) > 1 && len(s.Rhs) == 1 {
69
+ // Handle the case where we have multiple LHS variables but only one RHS expression
70
+ // Use array destructuring for all cases including map comma-ok idiom
71
+ rhsExpr := s.Rhs[0]
72
+ c.tsw.WriteLiterally("let [")
73
+ for i, lhs := range s.Lhs {
74
+ if i > 0 {
75
+ c.tsw.WriteLiterally(", ")
76
+ }
77
+ if err := c.WriteValueExpr(lhs); err != nil {
78
+ return err
79
+ }
80
+ }
81
+ c.tsw.WriteLiterally("] = ")
82
+ if err := c.writeDestructuringValue(rhsExpr); err != nil {
83
+ return err
84
+ }
85
+ return nil
86
+ } else if s.Tok == token.DEFINE && len(s.Lhs) > 1 && len(s.Rhs) > 0 {
68
87
  // For loop initialization with multiple variables (e.g., let i = 0, j = 10)
69
88
  c.tsw.WriteLiterally("let ")
70
89
 
@@ -121,6 +140,64 @@ func (c *GoToTSCompiler) WriteStmtForInit(stmt ast.Stmt) error {
121
140
  }
122
141
  }
123
142
 
143
+ // writeDestructuringValue writes a value expression in a destructuring context.
144
+ // For map index expressions, it generates the full tuple without [0] indexing.
145
+ func (c *GoToTSCompiler) writeDestructuringValue(expr ast.Expr) error {
146
+ // Check if this is a map index expression that should return a tuple
147
+ if indexExpr, ok := expr.(*ast.IndexExpr); ok {
148
+ if tv, ok := c.pkg.TypesInfo.Types[indexExpr.X]; ok {
149
+ underlyingType := tv.Type.Underlying()
150
+ // Check if it's a map type
151
+ if mapType, isMap := underlyingType.(*types.Map); isMap {
152
+ c.tsw.WriteLiterally("$.mapGet(")
153
+ if err := c.WriteValueExpr(indexExpr.X); err != nil {
154
+ return err
155
+ }
156
+ c.tsw.WriteLiterally(", ")
157
+ if err := c.WriteValueExpr(indexExpr.Index); err != nil {
158
+ return err
159
+ }
160
+ c.tsw.WriteLiterally(", ")
161
+ // Write the zero value as the default value for mapGet
162
+ c.WriteZeroValueForType(mapType.Elem())
163
+ c.tsw.WriteLiterally(")") // Don't add [0] for destructuring
164
+ return nil
165
+ }
166
+ // Check if it's a type parameter constrained to be a map type
167
+ if typeParam, isTypeParam := tv.Type.(*types.TypeParam); isTypeParam {
168
+ constraint := typeParam.Constraint()
169
+ if constraint != nil {
170
+ underlying := constraint.Underlying()
171
+ if iface, isInterface := underlying.(*types.Interface); isInterface {
172
+ if hasMapConstraint(iface) {
173
+ c.tsw.WriteLiterally("$.mapGet(")
174
+ if err := c.WriteValueExpr(indexExpr.X); err != nil {
175
+ return err
176
+ }
177
+ c.tsw.WriteLiterally(", ")
178
+ if err := c.WriteValueExpr(indexExpr.Index); err != nil {
179
+ return err
180
+ }
181
+ c.tsw.WriteLiterally(", ")
182
+ // Generate the zero value as the default value for mapGet
183
+ mapValueType := getMapValueTypeFromConstraint(iface)
184
+ if mapValueType != nil {
185
+ c.WriteZeroValueForType(mapValueType)
186
+ } else {
187
+ c.tsw.WriteLiterally("null")
188
+ }
189
+ c.tsw.WriteLiterally(")") // Don't add [0] for destructuring
190
+ return nil
191
+ }
192
+ }
193
+ }
194
+ }
195
+ }
196
+ }
197
+ // For non-map expressions, use the regular WriteValueExpr
198
+ return c.WriteValueExpr(expr)
199
+ }
200
+
124
201
  // WriteStmtForPost translates the post-iteration part of a Go `for` loop header
125
202
  // (e.g., `i++` or `i, j = i+1, j-1` in `for ...; i++`) into its TypeScript
126
203
  // equivalent.