goscript 0.0.33 → 0.0.35
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.
- package/compiler/analysis.go +30 -22
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +45 -7
- package/compiler/builtin_test.go +2 -0
- package/compiler/compiler.go +15 -89
- package/compiler/compiler_test.go +0 -53
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +347 -30
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +79 -38
- package/compiler/lit.go +112 -3
- package/compiler/primitive.go +6 -6
- package/compiler/protobuf.go +0 -5
- package/compiler/sanitize.go +101 -0
- package/compiler/spec-value.go +25 -18
- package/compiler/stmt-assign.go +128 -91
- package/compiler/stmt-for.go +78 -1
- package/compiler/stmt-range.go +333 -461
- package/compiler/stmt.go +46 -9
- package/compiler/type.go +14 -11
- package/dist/gs/builtin/builtin.d.ts +8 -0
- package/dist/gs/builtin/builtin.js +31 -0
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.d.ts +4 -4
- package/dist/gs/builtin/map.js +12 -6
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +14 -8
- package/dist/gs/builtin/slice.js +131 -31
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
- package/dist/gs/github.com/pkg/errors/errors.js +232 -0
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
- package/dist/gs/github.com/pkg/errors/go113.js +34 -0
- package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
- package/dist/gs/github.com/pkg/errors/index.js +4 -0
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
- package/dist/gs/github.com/pkg/errors/stack.js +111 -0
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
- package/dist/gs/maps/index.d.ts +2 -0
- package/dist/gs/maps/index.js +3 -0
- package/dist/gs/maps/index.js.map +1 -0
- package/dist/gs/maps/iter.d.ts +7 -0
- package/dist/gs/maps/iter.gs.d.ts +7 -0
- package/dist/gs/maps/iter.gs.js +65 -0
- package/dist/gs/maps/iter.gs.js.map +1 -0
- package/dist/gs/maps/iter.js +57 -0
- package/dist/gs/maps/iter.js.map +1 -0
- package/dist/gs/maps/maps.d.ts +7 -0
- package/dist/gs/maps/maps.gs.d.ts +7 -0
- package/dist/gs/maps/maps.gs.js +79 -0
- package/dist/gs/maps/maps.gs.js.map +1 -0
- package/dist/gs/maps/maps.js +67 -0
- package/dist/gs/maps/maps.js.map +1 -0
- package/dist/gs/math/abs.gs.d.ts +1 -0
- package/dist/gs/math/abs.gs.js +10 -0
- package/dist/gs/math/abs.gs.js.map +1 -0
- package/dist/gs/math/acosh.gs.d.ts +2 -0
- package/dist/gs/math/acosh.gs.js +14 -0
- package/dist/gs/math/acosh.gs.js.map +1 -0
- package/dist/gs/math/asin.gs.d.ts +4 -0
- package/dist/gs/math/asin.gs.js +24 -0
- package/dist/gs/math/asin.gs.js.map +1 -0
- package/dist/gs/math/asinh.gs.d.ts +2 -0
- package/dist/gs/math/asinh.gs.js +14 -0
- package/dist/gs/math/asinh.gs.js.map +1 -0
- package/dist/gs/math/atan.gs.d.ts +4 -0
- package/dist/gs/math/atan.gs.js +22 -0
- package/dist/gs/math/atan.gs.js.map +1 -0
- package/dist/gs/math/atan2.gs.d.ts +2 -0
- package/dist/gs/math/atan2.gs.js +30 -0
- package/dist/gs/math/atan2.gs.js.map +1 -0
- package/dist/gs/math/atanh.gs.d.ts +2 -0
- package/dist/gs/math/atanh.gs.js +16 -0
- package/dist/gs/math/atanh.gs.js.map +1 -0
- package/dist/gs/math/bits.gs.d.ts +5 -0
- package/dist/gs/math/bits.gs.js +46 -0
- package/dist/gs/math/bits.gs.js.map +1 -0
- package/dist/gs/math/cbrt.gs.d.ts +2 -0
- package/dist/gs/math/cbrt.gs.js +14 -0
- package/dist/gs/math/cbrt.gs.js.map +1 -0
- package/dist/gs/math/const.gs.d.ts +30 -0
- package/dist/gs/math/const.gs.js +61 -0
- package/dist/gs/math/const.gs.js.map +1 -0
- package/dist/gs/math/copysign.gs.d.ts +1 -0
- package/dist/gs/math/copysign.gs.js +20 -0
- package/dist/gs/math/copysign.gs.js.map +1 -0
- package/dist/gs/math/dim.gs.d.ts +5 -0
- package/dist/gs/math/dim.gs.js +69 -0
- package/dist/gs/math/dim.gs.js.map +1 -0
- package/dist/gs/math/erf.gs.d.ts +4 -0
- package/dist/gs/math/erf.gs.js +336 -0
- package/dist/gs/math/erf.gs.js.map +1 -0
- package/dist/gs/math/erfinv.gs.d.ts +2 -0
- package/dist/gs/math/erfinv.gs.js +118 -0
- package/dist/gs/math/erfinv.gs.js.map +1 -0
- package/dist/gs/math/exp.gs.d.ts +5 -0
- package/dist/gs/math/exp.gs.js +30 -0
- package/dist/gs/math/exp.gs.js.map +1 -0
- package/dist/gs/math/expm1.gs.d.ts +2 -0
- package/dist/gs/math/expm1.gs.js +17 -0
- package/dist/gs/math/expm1.gs.js.map +1 -0
- package/dist/gs/math/floor.gs.d.ts +8 -0
- package/dist/gs/math/floor.gs.js +75 -0
- package/dist/gs/math/floor.gs.js.map +1 -0
- package/dist/gs/math/fma.gs.d.ts +1 -0
- package/dist/gs/math/fma.gs.js +8 -0
- package/dist/gs/math/fma.gs.js.map +1 -0
- package/dist/gs/math/frexp.gs.d.ts +2 -0
- package/dist/gs/math/frexp.gs.js +28 -0
- package/dist/gs/math/frexp.gs.js.map +1 -0
- package/dist/gs/math/gamma.gs.d.ts +3 -0
- package/dist/gs/math/gamma.gs.js +149 -0
- package/dist/gs/math/gamma.gs.js.map +1 -0
- package/dist/gs/math/hypot.gs.d.ts +2 -0
- package/dist/gs/math/hypot.gs.js +16 -0
- package/dist/gs/math/hypot.gs.js.map +1 -0
- package/dist/gs/math/index.d.ts +44 -0
- package/dist/gs/math/index.js +45 -0
- package/dist/gs/math/index.js.map +1 -0
- package/dist/gs/math/j0.gs.d.ts +4 -0
- package/dist/gs/math/j0.gs.js +228 -0
- package/dist/gs/math/j0.gs.js.map +1 -0
- package/dist/gs/math/j1.gs.d.ts +4 -0
- package/dist/gs/math/j1.gs.js +211 -0
- package/dist/gs/math/j1.gs.js.map +1 -0
- package/dist/gs/math/jn.gs.d.ts +2 -0
- package/dist/gs/math/jn.gs.js +412 -0
- package/dist/gs/math/jn.gs.js.map +1 -0
- package/dist/gs/math/ldexp.gs.d.ts +2 -0
- package/dist/gs/math/ldexp.gs.js +20 -0
- package/dist/gs/math/ldexp.gs.js.map +1 -0
- package/dist/gs/math/lgamma.gs.d.ts +2 -0
- package/dist/gs/math/lgamma.gs.js +243 -0
- package/dist/gs/math/lgamma.gs.js.map +1 -0
- package/dist/gs/math/log.gs.d.ts +2 -0
- package/dist/gs/math/log.gs.js +16 -0
- package/dist/gs/math/log.gs.js.map +1 -0
- package/dist/gs/math/log10.gs.d.ts +4 -0
- package/dist/gs/math/log10.gs.js +17 -0
- package/dist/gs/math/log10.gs.js.map +1 -0
- package/dist/gs/math/log1p.gs.d.ts +2 -0
- package/dist/gs/math/log1p.gs.js +17 -0
- package/dist/gs/math/log1p.gs.js.map +1 -0
- package/dist/gs/math/logb.gs.d.ts +3 -0
- package/dist/gs/math/logb.gs.js +43 -0
- package/dist/gs/math/logb.gs.js.map +1 -0
- package/dist/gs/math/mod.gs.d.ts +2 -0
- package/dist/gs/math/mod.gs.js +26 -0
- package/dist/gs/math/mod.gs.js.map +1 -0
- package/dist/gs/math/modf.gs.d.ts +2 -0
- package/dist/gs/math/modf.gs.js +24 -0
- package/dist/gs/math/modf.gs.js.map +1 -0
- package/dist/gs/math/nextafter.gs.d.ts +2 -0
- package/dist/gs/math/nextafter.gs.js +66 -0
- package/dist/gs/math/nextafter.gs.js.map +1 -0
- package/dist/gs/math/pow.gs.d.ts +3 -0
- package/dist/gs/math/pow.gs.js +40 -0
- package/dist/gs/math/pow.gs.js.map +1 -0
- package/dist/gs/math/pow10.gs.d.ts +1 -0
- package/dist/gs/math/pow10.gs.js +14 -0
- package/dist/gs/math/pow10.gs.js.map +1 -0
- package/dist/gs/math/remainder.gs.d.ts +2 -0
- package/dist/gs/math/remainder.gs.js +25 -0
- package/dist/gs/math/remainder.gs.js.map +1 -0
- package/dist/gs/math/signbit.gs.d.ts +1 -0
- package/dist/gs/math/signbit.gs.js +5 -0
- package/dist/gs/math/signbit.gs.js.map +1 -0
- package/dist/gs/math/sin.gs.d.ts +4 -0
- package/dist/gs/math/sin.gs.js +29 -0
- package/dist/gs/math/sin.gs.js.map +1 -0
- package/dist/gs/math/sincos.gs.d.ts +1 -0
- package/dist/gs/math/sincos.gs.js +11 -0
- package/dist/gs/math/sincos.gs.js.map +1 -0
- package/dist/gs/math/sinh.gs.d.ts +4 -0
- package/dist/gs/math/sinh.gs.js +27 -0
- package/dist/gs/math/sinh.gs.js.map +1 -0
- package/dist/gs/math/sqrt.gs.d.ts +2 -0
- package/dist/gs/math/sqrt.gs.js +15 -0
- package/dist/gs/math/sqrt.gs.js.map +1 -0
- package/dist/gs/math/tan.gs.d.ts +2 -0
- package/dist/gs/math/tan.gs.js +17 -0
- package/dist/gs/math/tan.gs.js.map +1 -0
- package/dist/gs/math/tanh.gs.d.ts +2 -0
- package/dist/gs/math/tanh.gs.js +17 -0
- package/dist/gs/math/tanh.gs.js.map +1 -0
- package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
- package/dist/gs/math/trig_reduce.gs.js +62 -0
- package/dist/gs/math/trig_reduce.gs.js.map +1 -0
- package/dist/gs/math/unsafe.gs.d.ts +4 -0
- package/dist/gs/math/unsafe.gs.js +47 -0
- package/dist/gs/math/unsafe.gs.js.map +1 -0
- package/dist/gs/slices/slices.d.ts +6 -0
- package/dist/gs/slices/slices.js +8 -0
- package/dist/gs/slices/slices.js.map +1 -1
- package/dist/gs/strconv/atob.gs.d.ts +4 -0
- package/dist/gs/strconv/atob.gs.js +42 -0
- package/dist/gs/strconv/atob.gs.js.map +1 -0
- package/dist/gs/strconv/atof.gs.d.ts +2 -0
- package/dist/gs/strconv/atof.gs.js +51 -0
- package/dist/gs/strconv/atof.gs.js.map +1 -0
- package/dist/gs/strconv/atoi.gs.d.ts +33 -0
- package/dist/gs/strconv/atoi.gs.js +200 -0
- package/dist/gs/strconv/atoi.gs.js.map +1 -0
- package/dist/gs/strconv/doc.gs.d.ts +1 -0
- package/dist/gs/strconv/doc.gs.js +2 -0
- package/dist/gs/strconv/doc.gs.js.map +1 -0
- package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
- package/dist/gs/strconv/ftoa.gs.js +58 -0
- package/dist/gs/strconv/ftoa.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +6 -0
- package/dist/gs/strconv/index.js +7 -0
- package/dist/gs/strconv/index.js.map +1 -0
- package/dist/gs/strconv/itoa.gs.d.ts +6 -0
- package/dist/gs/strconv/itoa.gs.js +37 -0
- package/dist/gs/strconv/itoa.gs.js.map +1 -0
- package/dist/gs/strconv/quote.gs.d.ts +19 -0
- package/dist/gs/strconv/quote.gs.js +217 -0
- package/dist/gs/strconv/quote.gs.js.map +1 -0
- package/dist/gs/strings/index.d.ts +3 -0
- package/dist/gs/strings/index.js +4 -0
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/strings/replace.d.ts +0 -74
- package/dist/gs/strings/replace.js +6 -204
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/search.d.ts +0 -1
- package/dist/gs/strings/search.js +0 -21
- package/dist/gs/strings/search.js.map +1 -1
- package/gs/builtin/builtin.ts +40 -0
- package/gs/builtin/map.ts +12 -7
- package/gs/builtin/slice.ts +174 -34
- package/gs/github.com/pkg/errors/errors.ts +307 -0
- package/gs/github.com/pkg/errors/go113.ts +39 -0
- package/gs/github.com/pkg/errors/index.ts +3 -0
- package/gs/github.com/pkg/errors/stack.ts +127 -0
- package/gs/maps/index.ts +2 -0
- package/gs/maps/iter.ts +67 -0
- package/gs/maps/maps.ts +89 -0
- package/gs/math/TODO.md +156 -0
- package/gs/math/abs.gs.test.ts +29 -0
- package/gs/math/abs.gs.ts +13 -0
- package/gs/math/acosh.gs.test.ts +39 -0
- package/gs/math/acosh.gs.ts +21 -0
- package/gs/math/asin.gs.test.ts +66 -0
- package/gs/math/asin.gs.ts +27 -0
- package/gs/math/asinh.gs.test.ts +37 -0
- package/gs/math/asinh.gs.ts +21 -0
- package/gs/math/atan.gs.test.ts +49 -0
- package/gs/math/atan.gs.ts +27 -0
- package/gs/math/atan2.gs.test.ts +55 -0
- package/gs/math/atan2.gs.ts +37 -0
- package/gs/math/atanh.gs.test.ts +47 -0
- package/gs/math/atanh.gs.ts +21 -0
- package/gs/math/bits.gs.test.ts +88 -0
- package/gs/math/bits.gs.ts +61 -0
- package/gs/math/cbrt.gs.test.ts +57 -0
- package/gs/math/cbrt.gs.ts +20 -0
- package/gs/math/const.gs.test.ts +54 -0
- package/gs/math/const.gs.ts +93 -0
- package/gs/math/copysign.gs.test.ts +44 -0
- package/gs/math/copysign.gs.ts +27 -0
- package/gs/math/dim.gs.test.ts +102 -0
- package/gs/math/dim.gs.ts +84 -0
- package/gs/math/erf.gs.test.ts +92 -0
- package/gs/math/erf.gs.ts +409 -0
- package/gs/math/erfinv.gs.test.ts +104 -0
- package/gs/math/erfinv.gs.ts +169 -0
- package/gs/math/exp.gs.test.ts +82 -0
- package/gs/math/exp.gs.ts +39 -0
- package/gs/math/expm1.gs.test.ts +48 -0
- package/gs/math/expm1.gs.ts +23 -0
- package/gs/math/floor.gs.test.ts +146 -0
- package/gs/math/floor.gs.ts +88 -0
- package/gs/math/fma.gs.test.ts +83 -0
- package/gs/math/fma.gs.ts +7 -0
- package/gs/math/frexp.gs.test.ts +146 -0
- package/gs/math/frexp.gs.ts +37 -0
- package/gs/math/gamma.gs.test.ts +66 -0
- package/gs/math/gamma.gs.ts +158 -0
- package/gs/math/hypot.gs.test.ts +73 -0
- package/gs/math/hypot.gs.ts +23 -0
- package/gs/math/index.ts +44 -0
- package/gs/math/j0.gs.test.ts +74 -0
- package/gs/math/j0.gs.ts +257 -0
- package/gs/math/j1.gs.test.ts +81 -0
- package/gs/math/j1.gs.ts +231 -0
- package/gs/math/jn.gs.test.ts +133 -0
- package/gs/math/jn.gs.ts +447 -0
- package/gs/math/ldexp.gs.test.ts +128 -0
- package/gs/math/ldexp.gs.ts +28 -0
- package/gs/math/lgamma.gs.test.ts +102 -0
- package/gs/math/lgamma.gs.ts +251 -0
- package/gs/math/log.gs.test.ts +40 -0
- package/gs/math/log.gs.ts +21 -0
- package/gs/math/log10.gs.test.ts +80 -0
- package/gs/math/log10.gs.ts +25 -0
- package/gs/math/log1p.gs.test.ts +55 -0
- package/gs/math/log1p.gs.ts +24 -0
- package/gs/math/logb.gs.test.ts +87 -0
- package/gs/math/logb.gs.ts +54 -0
- package/gs/math/mod.gs.test.ts +64 -0
- package/gs/math/mod.gs.ts +36 -0
- package/gs/math/modf.gs.test.ts +80 -0
- package/gs/math/modf.gs.ts +32 -0
- package/gs/math/nextafter.gs.test.ts +107 -0
- package/gs/math/nextafter.gs.ts +71 -0
- package/gs/math/pow.gs.test.ts +103 -0
- package/gs/math/pow.gs.ts +55 -0
- package/gs/math/pow10.gs.test.ts +58 -0
- package/gs/math/pow10.gs.ts +19 -0
- package/gs/math/remainder.gs.test.ts +70 -0
- package/gs/math/remainder.gs.ts +33 -0
- package/gs/math/signbit.gs.test.ts +33 -0
- package/gs/math/signbit.gs.ts +8 -0
- package/gs/math/sin.gs.test.ts +83 -0
- package/gs/math/sin.gs.ts +38 -0
- package/gs/math/sincos.gs.test.ts +91 -0
- package/gs/math/sincos.gs.ts +15 -0
- package/gs/math/sinh.gs.test.ts +66 -0
- package/gs/math/sinh.gs.ts +34 -0
- package/gs/math/sqrt.gs.test.ts +49 -0
- package/gs/math/sqrt.gs.ts +20 -0
- package/gs/math/tan.gs.test.ts +50 -0
- package/gs/math/tan.gs.ts +23 -0
- package/gs/math/tanh.gs.test.ts +52 -0
- package/gs/math/tanh.gs.ts +23 -0
- package/gs/math/trig_reduce.gs.ts +66 -0
- package/gs/math/unsafe.gs.ts +52 -0
- package/gs/slices/slices.ts +9 -0
- package/gs/strconv/atob.gs.ts +45 -0
- package/gs/strconv/atof.gs.ts +60 -0
- package/gs/strconv/atoi.gs.ts +243 -0
- package/gs/strconv/doc.gs.ts +2 -0
- package/gs/strconv/ftoa.gs.ts +66 -0
- package/gs/strconv/index.ts +6 -0
- package/gs/strconv/itoa.gs.ts +41 -0
- package/gs/strconv/quote.gs.ts +245 -0
- package/gs/strings/index.ts +4 -0
- package/gs/strings/replace.ts +9 -237
- package/gs/strings/search.ts +0 -28
- package/package.json +1 -1
- package/gs/stringslite/godoc.txt +0 -17
- package/gs/stringslite/index.ts +0 -1
- package/gs/stringslite/strings.ts +0 -82
package/compiler/expr-call.go
CHANGED
|
@@ -206,17 +206,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
206
206
|
|
|
207
207
|
// Check if it's make([]byte, ...)
|
|
208
208
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
209
|
-
|
|
210
|
-
if len(exp.Args)
|
|
209
|
+
// Check if capacity is different from length
|
|
210
|
+
if len(exp.Args) == 3 {
|
|
211
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
212
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
211
213
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
212
214
|
return err
|
|
213
215
|
}
|
|
214
|
-
|
|
216
|
+
c.tsw.WriteLiterally(", ")
|
|
217
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
218
|
+
return err
|
|
219
|
+
}
|
|
220
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
215
221
|
} else {
|
|
216
|
-
//
|
|
217
|
-
c.tsw.WriteLiterally("
|
|
222
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
223
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
224
|
+
if len(exp.Args) >= 2 {
|
|
225
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
226
|
+
return err
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
// If no length is provided, default to 0
|
|
230
|
+
c.tsw.WriteLiterally("0")
|
|
231
|
+
}
|
|
232
|
+
c.tsw.WriteLiterally(")")
|
|
218
233
|
}
|
|
219
|
-
c.tsw.WriteLiterally(")")
|
|
220
234
|
return nil // Handled make for []byte
|
|
221
235
|
}
|
|
222
236
|
|
|
@@ -251,11 +265,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
251
265
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
252
266
|
c.tsw.WriteLiterally(">(")
|
|
253
267
|
|
|
268
|
+
hasCapacity := len(exp.Args) == 3
|
|
269
|
+
|
|
254
270
|
if len(exp.Args) >= 2 {
|
|
255
271
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
256
272
|
return err
|
|
257
273
|
}
|
|
258
|
-
if
|
|
274
|
+
if hasCapacity {
|
|
259
275
|
c.tsw.WriteLiterally(", ")
|
|
260
276
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
261
277
|
return err
|
|
@@ -267,6 +283,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
267
283
|
// If no length is provided, default to 0
|
|
268
284
|
c.tsw.WriteLiterally("0")
|
|
269
285
|
}
|
|
286
|
+
|
|
287
|
+
// Add type hint for proper zero value initialization
|
|
288
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
289
|
+
if typeHint != "" {
|
|
290
|
+
if !hasCapacity {
|
|
291
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
292
|
+
c.tsw.WriteLiterally(", undefined")
|
|
293
|
+
}
|
|
294
|
+
c.tsw.WriteLiterally(", '")
|
|
295
|
+
c.tsw.WriteLiterally(typeHint)
|
|
296
|
+
c.tsw.WriteLiterally("'")
|
|
297
|
+
}
|
|
298
|
+
|
|
270
299
|
c.tsw.WriteLiterally(")")
|
|
271
300
|
return nil // Handled make for slice
|
|
272
301
|
}
|
|
@@ -291,17 +320,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
291
320
|
if elemType != nil {
|
|
292
321
|
// Check if it's make(S, ...) where S constrains to []byte
|
|
293
322
|
if basicElem, isBasic := elemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
294
|
-
|
|
295
|
-
if len(exp.Args)
|
|
323
|
+
// Check if capacity is different from length
|
|
324
|
+
if len(exp.Args) == 3 {
|
|
325
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
326
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
296
327
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
297
328
|
return err
|
|
298
329
|
}
|
|
299
|
-
|
|
330
|
+
c.tsw.WriteLiterally(", ")
|
|
331
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
332
|
+
return err
|
|
333
|
+
}
|
|
334
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
300
335
|
} else {
|
|
301
|
-
//
|
|
302
|
-
c.tsw.WriteLiterally("
|
|
336
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
337
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
338
|
+
if len(exp.Args) >= 2 {
|
|
339
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
340
|
+
return err
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
// If no length is provided, default to 0
|
|
344
|
+
c.tsw.WriteLiterally("0")
|
|
345
|
+
}
|
|
346
|
+
c.tsw.WriteLiterally(")")
|
|
303
347
|
}
|
|
304
|
-
c.tsw.WriteLiterally(")")
|
|
305
348
|
return nil // Handled make for generic []byte
|
|
306
349
|
}
|
|
307
350
|
|
|
@@ -309,11 +352,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
309
352
|
c.WriteGoType(elemType, GoTypeContextGeneral) // Write the element type
|
|
310
353
|
c.tsw.WriteLiterally(">(")
|
|
311
354
|
|
|
355
|
+
hasCapacity := len(exp.Args) == 3
|
|
356
|
+
|
|
312
357
|
if len(exp.Args) >= 2 {
|
|
313
358
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
314
359
|
return err
|
|
315
360
|
}
|
|
316
|
-
if
|
|
361
|
+
if hasCapacity {
|
|
317
362
|
c.tsw.WriteLiterally(", ")
|
|
318
363
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
319
364
|
return err
|
|
@@ -325,6 +370,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
325
370
|
// If no length is provided, default to 0
|
|
326
371
|
c.tsw.WriteLiterally("0")
|
|
327
372
|
}
|
|
373
|
+
|
|
374
|
+
// Add type hint for proper zero value initialization
|
|
375
|
+
typeHint := c.getTypeHintForSliceElement(elemType)
|
|
376
|
+
if typeHint != "" {
|
|
377
|
+
if !hasCapacity {
|
|
378
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
379
|
+
c.tsw.WriteLiterally(", undefined")
|
|
380
|
+
}
|
|
381
|
+
c.tsw.WriteLiterally(", '")
|
|
382
|
+
c.tsw.WriteLiterally(typeHint)
|
|
383
|
+
c.tsw.WriteLiterally("'")
|
|
384
|
+
}
|
|
385
|
+
|
|
328
386
|
c.tsw.WriteLiterally(")")
|
|
329
387
|
return nil // Handled make for generic slice
|
|
330
388
|
}
|
|
@@ -340,17 +398,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
340
398
|
|
|
341
399
|
// Check if it's a named type with []byte underlying type
|
|
342
400
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
343
|
-
|
|
344
|
-
if len(exp.Args)
|
|
401
|
+
// Check if capacity is different from length
|
|
402
|
+
if len(exp.Args) == 3 {
|
|
403
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
404
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
345
405
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
346
406
|
return err
|
|
347
407
|
}
|
|
348
|
-
|
|
408
|
+
c.tsw.WriteLiterally(", ")
|
|
409
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
410
|
+
return err
|
|
411
|
+
}
|
|
412
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
349
413
|
} else {
|
|
350
|
-
//
|
|
351
|
-
c.tsw.WriteLiterally("
|
|
414
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
415
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
416
|
+
if len(exp.Args) >= 2 {
|
|
417
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
418
|
+
return err
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
// If no length is provided, default to 0
|
|
422
|
+
c.tsw.WriteLiterally("0")
|
|
423
|
+
}
|
|
424
|
+
c.tsw.WriteLiterally(")")
|
|
352
425
|
}
|
|
353
|
-
c.tsw.WriteLiterally(")")
|
|
354
426
|
return nil // Handled make for named []byte type
|
|
355
427
|
}
|
|
356
428
|
|
|
@@ -359,11 +431,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
359
431
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
360
432
|
c.tsw.WriteLiterally(">(")
|
|
361
433
|
|
|
434
|
+
hasCapacity := len(exp.Args) == 3
|
|
435
|
+
|
|
362
436
|
if len(exp.Args) >= 2 {
|
|
363
437
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
364
438
|
return err
|
|
365
439
|
}
|
|
366
|
-
if
|
|
440
|
+
if hasCapacity {
|
|
367
441
|
c.tsw.WriteLiterally(", ")
|
|
368
442
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
369
443
|
return err
|
|
@@ -375,6 +449,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
375
449
|
// If no length is provided, default to 0
|
|
376
450
|
c.tsw.WriteLiterally("0")
|
|
377
451
|
}
|
|
452
|
+
|
|
453
|
+
// Add type hint for proper zero value initialization
|
|
454
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
455
|
+
if typeHint != "" {
|
|
456
|
+
if !hasCapacity {
|
|
457
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
458
|
+
c.tsw.WriteLiterally(", undefined")
|
|
459
|
+
}
|
|
460
|
+
c.tsw.WriteLiterally(", '")
|
|
461
|
+
c.tsw.WriteLiterally(typeHint)
|
|
462
|
+
c.tsw.WriteLiterally("'")
|
|
463
|
+
}
|
|
464
|
+
|
|
378
465
|
c.tsw.WriteLiterally(")")
|
|
379
466
|
return nil // Handled make for named slice type
|
|
380
467
|
}
|
|
@@ -461,17 +548,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
461
548
|
|
|
462
549
|
// Check if it's an instantiated generic type with []byte underlying type
|
|
463
550
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
464
|
-
|
|
465
|
-
if len(exp.Args)
|
|
551
|
+
// Check if capacity is different from length
|
|
552
|
+
if len(exp.Args) == 3 {
|
|
553
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
554
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
466
555
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
467
556
|
return err
|
|
468
557
|
}
|
|
469
|
-
|
|
558
|
+
c.tsw.WriteLiterally(", ")
|
|
559
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
560
|
+
return err
|
|
561
|
+
}
|
|
562
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
470
563
|
} else {
|
|
471
|
-
//
|
|
472
|
-
c.tsw.WriteLiterally("
|
|
564
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
565
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
566
|
+
if len(exp.Args) >= 2 {
|
|
567
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
568
|
+
return err
|
|
569
|
+
}
|
|
570
|
+
} else {
|
|
571
|
+
// If no length is provided, default to 0
|
|
572
|
+
c.tsw.WriteLiterally("0")
|
|
573
|
+
}
|
|
574
|
+
c.tsw.WriteLiterally(")")
|
|
473
575
|
}
|
|
474
|
-
c.tsw.WriteLiterally(")")
|
|
475
576
|
return nil // Handled make for instantiated generic []byte type
|
|
476
577
|
}
|
|
477
578
|
|
|
@@ -480,11 +581,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
480
581
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
481
582
|
c.tsw.WriteLiterally(">(")
|
|
482
583
|
|
|
584
|
+
hasCapacity := len(exp.Args) == 3
|
|
585
|
+
|
|
483
586
|
if len(exp.Args) >= 2 {
|
|
484
587
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
485
588
|
return err
|
|
486
589
|
}
|
|
487
|
-
if
|
|
590
|
+
if hasCapacity {
|
|
488
591
|
c.tsw.WriteLiterally(", ")
|
|
489
592
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
490
593
|
return err
|
|
@@ -496,6 +599,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
496
599
|
// If no length is provided, default to 0
|
|
497
600
|
c.tsw.WriteLiterally("0")
|
|
498
601
|
}
|
|
602
|
+
|
|
603
|
+
// Add type hint for proper zero value initialization
|
|
604
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
605
|
+
if typeHint != "" {
|
|
606
|
+
if !hasCapacity {
|
|
607
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
608
|
+
c.tsw.WriteLiterally(", undefined")
|
|
609
|
+
}
|
|
610
|
+
c.tsw.WriteLiterally(", '")
|
|
611
|
+
c.tsw.WriteLiterally(typeHint)
|
|
612
|
+
c.tsw.WriteLiterally("'")
|
|
613
|
+
}
|
|
614
|
+
|
|
499
615
|
c.tsw.WriteLiterally(")")
|
|
500
616
|
return nil // Handled make for instantiated generic slice type
|
|
501
617
|
}
|
|
@@ -545,6 +661,143 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
545
661
|
}
|
|
546
662
|
}
|
|
547
663
|
}
|
|
664
|
+
// Handle selector expressions: make(pkg.TypeName, ...)
|
|
665
|
+
// This handles cases like: make(fstest.MapFS) where fstest.MapFS is map[string]*MapFile
|
|
666
|
+
if selectorExpr, ok := exp.Args[0].(*ast.SelectorExpr); ok {
|
|
667
|
+
// Get the type information for the selector expression
|
|
668
|
+
if typ := c.pkg.TypesInfo.TypeOf(selectorExpr); typ != nil {
|
|
669
|
+
// Check the underlying type of the selector expression
|
|
670
|
+
underlying := typ.Underlying()
|
|
671
|
+
|
|
672
|
+
// Handle selector expression map types: make(pkg.MapType)
|
|
673
|
+
if mapType, isMap := underlying.(*types.Map); isMap {
|
|
674
|
+
c.tsw.WriteLiterally("$.makeMap<")
|
|
675
|
+
c.WriteGoType(mapType.Key(), GoTypeContextGeneral) // Write the key type
|
|
676
|
+
c.tsw.WriteLiterally(", ")
|
|
677
|
+
c.WriteGoType(mapType.Elem(), GoTypeContextGeneral) // Write the value type
|
|
678
|
+
c.tsw.WriteLiterally(">()")
|
|
679
|
+
return nil // Handled make for selector expression map type
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// Handle selector expression slice types: make(pkg.SliceType, len, cap)
|
|
683
|
+
if sliceType, isSlice := underlying.(*types.Slice); isSlice {
|
|
684
|
+
goElemType := sliceType.Elem()
|
|
685
|
+
|
|
686
|
+
// Check if it's a selector expression with []byte underlying type
|
|
687
|
+
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
688
|
+
// Check if capacity is different from length
|
|
689
|
+
if len(exp.Args) == 3 {
|
|
690
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
691
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
692
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
693
|
+
return err
|
|
694
|
+
}
|
|
695
|
+
c.tsw.WriteLiterally(", ")
|
|
696
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
697
|
+
return err
|
|
698
|
+
}
|
|
699
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
700
|
+
} else {
|
|
701
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
702
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
703
|
+
if len(exp.Args) >= 2 {
|
|
704
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
705
|
+
return err
|
|
706
|
+
}
|
|
707
|
+
} else {
|
|
708
|
+
// If no length is provided, default to 0
|
|
709
|
+
c.tsw.WriteLiterally("0")
|
|
710
|
+
}
|
|
711
|
+
c.tsw.WriteLiterally(")")
|
|
712
|
+
}
|
|
713
|
+
return nil // Handled make for selector expression []byte type
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// Handle other selector expression slice types
|
|
717
|
+
c.tsw.WriteLiterally("$.makeSlice<")
|
|
718
|
+
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
719
|
+
c.tsw.WriteLiterally(">(")
|
|
720
|
+
|
|
721
|
+
hasCapacity := len(exp.Args) == 3
|
|
722
|
+
|
|
723
|
+
if len(exp.Args) >= 2 {
|
|
724
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
725
|
+
return err
|
|
726
|
+
}
|
|
727
|
+
if hasCapacity {
|
|
728
|
+
c.tsw.WriteLiterally(", ")
|
|
729
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
730
|
+
return err
|
|
731
|
+
}
|
|
732
|
+
} else if len(exp.Args) > 3 {
|
|
733
|
+
return errors.New("makeSlice expects 2 or 3 arguments")
|
|
734
|
+
}
|
|
735
|
+
} else {
|
|
736
|
+
// If no length is provided, default to 0
|
|
737
|
+
c.tsw.WriteLiterally("0")
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// Add type hint for proper zero value initialization
|
|
741
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
742
|
+
if typeHint != "" {
|
|
743
|
+
if !hasCapacity {
|
|
744
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
745
|
+
c.tsw.WriteLiterally(", undefined")
|
|
746
|
+
}
|
|
747
|
+
c.tsw.WriteLiterally(", '")
|
|
748
|
+
c.tsw.WriteLiterally(typeHint)
|
|
749
|
+
c.tsw.WriteLiterally("'")
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
c.tsw.WriteLiterally(")")
|
|
753
|
+
return nil // Handled make for selector expression slice type
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Handle selector expression channel types: make(pkg.ChannelType, bufferSize)
|
|
757
|
+
if chanType, isChan := underlying.(*types.Chan); isChan {
|
|
758
|
+
c.tsw.WriteLiterally("$.makeChannel<")
|
|
759
|
+
c.WriteGoType(chanType.Elem(), GoTypeContextGeneral)
|
|
760
|
+
c.tsw.WriteLiterally(">(")
|
|
761
|
+
|
|
762
|
+
// If buffer size is provided, add it
|
|
763
|
+
if len(exp.Args) >= 2 {
|
|
764
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil {
|
|
765
|
+
return fmt.Errorf("failed to write buffer size in makeChannel: %w", err)
|
|
766
|
+
}
|
|
767
|
+
} else {
|
|
768
|
+
// Default to 0 (unbuffered channel)
|
|
769
|
+
c.tsw.WriteLiterally("0")
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
c.tsw.WriteLiterally(", ") // Add comma for zero value argument
|
|
773
|
+
|
|
774
|
+
// Write the zero value for the channel's element type
|
|
775
|
+
if chanType.Elem().String() == "struct{}" {
|
|
776
|
+
c.tsw.WriteLiterally("{}")
|
|
777
|
+
} else {
|
|
778
|
+
c.WriteZeroValueForType(chanType.Elem())
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Add direction parameter
|
|
782
|
+
c.tsw.WriteLiterally(", ")
|
|
783
|
+
|
|
784
|
+
// Determine channel direction
|
|
785
|
+
switch chanType.Dir() {
|
|
786
|
+
case types.SendRecv:
|
|
787
|
+
c.tsw.WriteLiterally("'both'")
|
|
788
|
+
case types.SendOnly:
|
|
789
|
+
c.tsw.WriteLiterally("'send'")
|
|
790
|
+
case types.RecvOnly:
|
|
791
|
+
c.tsw.WriteLiterally("'receive'")
|
|
792
|
+
default:
|
|
793
|
+
c.tsw.WriteLiterally("'both'") // Default to bidirectional
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
c.tsw.WriteLiterally(")")
|
|
797
|
+
return nil // Handled make for selector expression channel type
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
548
801
|
// Fallthrough for unhandled make calls (e.g., channels)
|
|
549
802
|
return errors.New("unhandled make call")
|
|
550
803
|
case "string":
|
|
@@ -666,6 +919,40 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
666
919
|
if i > 0 || len(exp.Args) > 1 { // Add comma before elements if there are any
|
|
667
920
|
c.tsw.WriteLiterally(", ")
|
|
668
921
|
}
|
|
922
|
+
|
|
923
|
+
// Special case: append([]byte, string...) should convert string to bytes
|
|
924
|
+
if exp.Ellipsis != token.NoPos && i == 0 { // This is the first element after slice and has ellipsis
|
|
925
|
+
// Check if the slice is []byte and the argument is a string
|
|
926
|
+
sliceType := c.pkg.TypesInfo.TypeOf(exp.Args[0])
|
|
927
|
+
argType := c.pkg.TypesInfo.TypeOf(arg)
|
|
928
|
+
|
|
929
|
+
if sliceType != nil && argType != nil {
|
|
930
|
+
// Check if slice is []byte (Uint8Array)
|
|
931
|
+
isSliceOfBytes := false
|
|
932
|
+
if sliceUnder, ok := sliceType.Underlying().(*types.Slice); ok {
|
|
933
|
+
if basicElem, ok := sliceUnder.Elem().(*types.Basic); ok && basicElem.Kind() == types.Uint8 {
|
|
934
|
+
isSliceOfBytes = true
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Check if argument is string (including untyped string)
|
|
939
|
+
isArgString := false
|
|
940
|
+
if basicArg, ok := argType.Underlying().(*types.Basic); ok && (basicArg.Kind() == types.String || basicArg.Kind() == types.UntypedString) {
|
|
941
|
+
isArgString = true
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
if isSliceOfBytes && isArgString {
|
|
945
|
+
// Convert string to bytes: append([]byte, string...) -> $.append(slice, ...$.stringToBytes(string))
|
|
946
|
+
c.tsw.WriteLiterally("...$.stringToBytes(")
|
|
947
|
+
if err := c.WriteValueExpr(arg); err != nil {
|
|
948
|
+
return fmt.Errorf("failed to write string argument in append call: %w", err)
|
|
949
|
+
}
|
|
950
|
+
c.tsw.WriteLiterally(")")
|
|
951
|
+
continue
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
669
956
|
if err := c.WriteValueExpr(arg); err != nil {
|
|
670
957
|
return fmt.Errorf("failed to write argument %d in append call: %w", i+1, err)
|
|
671
958
|
}
|
|
@@ -879,12 +1166,16 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
879
1166
|
}
|
|
880
1167
|
c.tsw.WriteLiterally(")")
|
|
881
1168
|
} else {
|
|
882
|
-
// Not an identifier (e.g., method call on a value)
|
|
1169
|
+
// Not an identifier (e.g., method call on a value, function call result)
|
|
883
1170
|
if err := c.WriteValueExpr(expFun); err != nil {
|
|
884
1171
|
return fmt.Errorf("failed to write method expression in call: %w", err)
|
|
885
1172
|
}
|
|
886
1173
|
|
|
887
|
-
if
|
|
1174
|
+
// Check if this is a function call that returns a function (e.g., simpleIterator(m)())
|
|
1175
|
+
// Add non-null assertion since the returned function could be null
|
|
1176
|
+
if _, isCallExpr := expFun.(*ast.CallExpr); isCallExpr {
|
|
1177
|
+
c.tsw.WriteLiterally("!")
|
|
1178
|
+
} else if funType := c.pkg.TypesInfo.TypeOf(expFun); funType != nil {
|
|
888
1179
|
if _, ok := funType.Underlying().(*types.Signature); ok {
|
|
889
1180
|
// Check if this is a function parameter identifier that needs not-null assertion
|
|
890
1181
|
if ident, isIdent := expFun.(*ast.Ident); isIdent {
|
|
@@ -899,6 +1190,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
899
1190
|
} else if _, isNamed := funType.(*types.Named); isNamed {
|
|
900
1191
|
c.tsw.WriteLiterally("!")
|
|
901
1192
|
}
|
|
1193
|
+
} else {
|
|
1194
|
+
// Check if the function type is nullable (e.g., func(...) | null)
|
|
1195
|
+
// This handles cases where a function call returns a nullable function
|
|
1196
|
+
funTypeStr := funType.String()
|
|
1197
|
+
if strings.Contains(funTypeStr, "| null") || strings.Contains(funTypeStr, "null |") {
|
|
1198
|
+
c.tsw.WriteLiterally("!")
|
|
1199
|
+
}
|
|
902
1200
|
}
|
|
903
1201
|
}
|
|
904
1202
|
}
|
|
@@ -1004,3 +1302,22 @@ func hasMixedStringByteConstraint(iface *types.Interface) bool {
|
|
|
1004
1302
|
// Return true only if we have both string and []byte in the constraint
|
|
1005
1303
|
return hasString && hasByteSlice
|
|
1006
1304
|
}
|
|
1305
|
+
|
|
1306
|
+
// getTypeHintForSliceElement returns the appropriate type hint for makeSlice based on the Go element type
|
|
1307
|
+
func (c *GoToTSCompiler) getTypeHintForSliceElement(elemType types.Type) string {
|
|
1308
|
+
if basicType, isBasic := elemType.(*types.Basic); isBasic {
|
|
1309
|
+
switch basicType.Kind() {
|
|
1310
|
+
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
|
|
1311
|
+
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64,
|
|
1312
|
+
types.Float32, types.Float64, types.Complex64, types.Complex128:
|
|
1313
|
+
return "number"
|
|
1314
|
+
case types.Bool:
|
|
1315
|
+
return "boolean"
|
|
1316
|
+
case types.String:
|
|
1317
|
+
return "string"
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
// For other types (structs, interfaces, pointers, etc.), don't provide a hint
|
|
1321
|
+
// This will use the default null initialization which is appropriate for object types
|
|
1322
|
+
return ""
|
|
1323
|
+
}
|
|
@@ -129,7 +129,33 @@ func (c *GoToTSCompiler) WriteSelectorExpr(exp *ast.SelectorExpr) error {
|
|
|
129
129
|
// Add null assertion for selector expressions when accessing fields/methods on nullable types
|
|
130
130
|
// In Go, accessing fields or calling methods on nil pointers/interfaces panics, so we should throw in TypeScript
|
|
131
131
|
baseType := c.pkg.TypesInfo.TypeOf(exp.X)
|
|
132
|
-
|
|
132
|
+
|
|
133
|
+
// Special case: Check if this is a method receiver alias (e.g., `b` in `const b = this`)
|
|
134
|
+
// In this case, we should use regular field access instead of pointer access
|
|
135
|
+
isMethodReceiverAlias := false
|
|
136
|
+
if baseIdent, ok := exp.X.(*ast.Ident); ok {
|
|
137
|
+
// Check if this identifier refers to a method receiver
|
|
138
|
+
// Method receivers are typically named variables that alias `this`
|
|
139
|
+
if obj := c.pkg.TypesInfo.ObjectOf(baseIdent); obj != nil {
|
|
140
|
+
if varObj, ok := obj.(*types.Var); ok {
|
|
141
|
+
// Check if this is a local variable in a method context
|
|
142
|
+
// Method receiver aliases are local variables with pointer types that represent `this`
|
|
143
|
+
if _, isPtr := varObj.Type().(*types.Pointer); isPtr {
|
|
144
|
+
// Additional check: only consider it a method receiver alias if the variable name
|
|
145
|
+
// suggests it's an alias (single letter names are common for aliases like `b`, `r`, etc.)
|
|
146
|
+
// and it's not a common variable name like `f` which is often used for regular variables
|
|
147
|
+
varName := baseIdent.Name
|
|
148
|
+
if len(varName) == 1 && varName != "f" {
|
|
149
|
+
// This is likely a method receiver alias like `const b = this`
|
|
150
|
+
// We should treat it as a regular object access, not a pointer access
|
|
151
|
+
isMethodReceiverAlias = true
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if baseType != nil && !isMethodReceiverAlias {
|
|
133
159
|
// Check if the base is a pointer type
|
|
134
160
|
if _, isPtr := baseType.(*types.Pointer); isPtr {
|
|
135
161
|
c.tsw.WriteLiterally("!.")
|
|
@@ -145,7 +171,7 @@ func (c *GoToTSCompiler) WriteSelectorExpr(exp *ast.SelectorExpr) error {
|
|
|
145
171
|
c.tsw.WriteLiterally(".")
|
|
146
172
|
}
|
|
147
173
|
} else {
|
|
148
|
-
// Add .
|
|
174
|
+
// Add . (either baseType is nil or this is a method receiver alias)
|
|
149
175
|
c.tsw.WriteLiterally(".")
|
|
150
176
|
}
|
|
151
177
|
|