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.go
CHANGED
|
@@ -52,7 +52,7 @@ func (c *GoToTSCompiler) WriteIndexExpr(exp *ast.IndexExpr) error {
|
|
|
52
52
|
|
|
53
53
|
// Generate the zero value as the default value for mapGet
|
|
54
54
|
c.WriteZeroValueForType(mapType.Elem())
|
|
55
|
-
c.tsw.WriteLiterally(")")
|
|
55
|
+
c.tsw.WriteLiterally(")[0]") // Extract the value from the tuple
|
|
56
56
|
return nil
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -77,6 +77,31 @@ func (c *GoToTSCompiler) WriteIndexExpr(exp *ast.IndexExpr) error {
|
|
|
77
77
|
if constraint != nil {
|
|
78
78
|
underlying := constraint.Underlying()
|
|
79
79
|
if iface, isInterface := underlying.(*types.Interface); isInterface {
|
|
80
|
+
// Check if this is a map constraint (like ~map[K]V)
|
|
81
|
+
if hasMapConstraint(iface) {
|
|
82
|
+
// This is a map type parameter, use map access
|
|
83
|
+
c.tsw.WriteLiterally("$.mapGet(")
|
|
84
|
+
if err := c.WriteValueExpr(exp.X); err != nil {
|
|
85
|
+
return err
|
|
86
|
+
}
|
|
87
|
+
c.tsw.WriteLiterally(", ")
|
|
88
|
+
if err := c.WriteValueExpr(exp.Index); err != nil {
|
|
89
|
+
return err
|
|
90
|
+
}
|
|
91
|
+
c.tsw.WriteLiterally(", ")
|
|
92
|
+
|
|
93
|
+
// Generate the zero value as the default value for mapGet
|
|
94
|
+
// For type parameters, we need to get the value type from the constraint
|
|
95
|
+
mapValueType := getMapValueTypeFromConstraint(iface)
|
|
96
|
+
if mapValueType != nil {
|
|
97
|
+
c.WriteZeroValueForType(mapValueType)
|
|
98
|
+
} else {
|
|
99
|
+
c.tsw.WriteLiterally("null")
|
|
100
|
+
}
|
|
101
|
+
c.tsw.WriteLiterally(")[0]") // Extract the value from the tuple
|
|
102
|
+
return nil
|
|
103
|
+
}
|
|
104
|
+
|
|
80
105
|
// Check if this is a mixed string/byte constraint (like string | []byte)
|
|
81
106
|
if hasMixedStringByteConstraint(iface) {
|
|
82
107
|
// For mixed constraints, use specialized function that returns number (byte value)
|
|
@@ -390,13 +415,18 @@ func (c *GoToTSCompiler) WriteBinaryExpr(exp *ast.BinaryExpr) error {
|
|
|
390
415
|
|
|
391
416
|
// Handle large bit shift expressions that would overflow in JavaScript
|
|
392
417
|
if exp.Op == token.SHL {
|
|
393
|
-
// Check if this is 1 << 63 pattern
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
418
|
+
// Check if this is 1 << 63 pattern using constant evaluation
|
|
419
|
+
leftValue := c.evaluateConstantExpr(exp.X)
|
|
420
|
+
rightValue := c.evaluateConstantExpr(exp.Y)
|
|
421
|
+
|
|
422
|
+
if leftValue != nil && rightValue != nil {
|
|
423
|
+
if leftInt, leftOk := leftValue.(int); leftOk && leftInt == 1 {
|
|
424
|
+
if rightInt, rightOk := rightValue.(int); rightOk && rightInt == 63 {
|
|
425
|
+
// Replace 1 << 63 with Number.MAX_SAFE_INTEGER (9007199254740991)
|
|
426
|
+
// This is the largest integer that can be exactly represented in JavaScript
|
|
427
|
+
c.tsw.WriteLiterally("Number.MAX_SAFE_INTEGER")
|
|
428
|
+
return nil
|
|
429
|
+
}
|
|
400
430
|
}
|
|
401
431
|
}
|
|
402
432
|
}
|
|
@@ -530,23 +560,17 @@ func (c *GoToTSCompiler) WriteUnaryExpr(exp *ast.UnaryExpr) error {
|
|
|
530
560
|
|
|
531
561
|
// WriteSliceExpr translates a Go slice expression (e.g., `s[low:high:max]`) to its TypeScript equivalent.
|
|
532
562
|
// If `s` is a string and it's not a 3-index slice, it uses `s.substring(low, high)`.
|
|
533
|
-
// If `s` is `[]byte` (Uint8Array) and it's not a 3-index slice, it uses
|
|
563
|
+
// If `s` is `[]byte` (Uint8Array) and it's not a 3-index slice, it uses $.goSlice.
|
|
534
564
|
// Otherwise, it falls back to the `$.goSlice(s, low, high, max)` runtime helper.
|
|
535
565
|
func (c *GoToTSCompiler) WriteSliceExpr(exp *ast.SliceExpr) error {
|
|
536
566
|
// Check if the expression being sliced is a string
|
|
537
567
|
tv := c.pkg.TypesInfo.TypeOf(exp.X)
|
|
538
568
|
isString := false
|
|
539
|
-
isByteSlice := false
|
|
540
569
|
isTypeParam := false
|
|
541
570
|
if tv != nil {
|
|
542
571
|
if basicType, isBasic := tv.Underlying().(*types.Basic); isBasic && (basicType.Info()&types.IsString) != 0 {
|
|
543
572
|
isString = true
|
|
544
573
|
}
|
|
545
|
-
if sliceType, isSlice := tv.Underlying().(*types.Slice); isSlice {
|
|
546
|
-
if basicElem, isBasic := sliceType.Elem().(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
547
|
-
isByteSlice = true
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
574
|
if _, isTP := tv.(*types.TypeParam); isTP {
|
|
551
575
|
isTypeParam = true
|
|
552
576
|
}
|
|
@@ -616,29 +640,6 @@ func (c *GoToTSCompiler) WriteSliceExpr(exp *ast.SliceExpr) error {
|
|
|
616
640
|
c.tsw.WriteLiterally("undefined")
|
|
617
641
|
}
|
|
618
642
|
c.tsw.WriteLiterally(")")
|
|
619
|
-
} else if isByteSlice && !exp.Slice3 {
|
|
620
|
-
// Use s.subarray(low, high) for []byte slices
|
|
621
|
-
if err := c.WriteValueExpr(exp.X); err != nil {
|
|
622
|
-
return err
|
|
623
|
-
}
|
|
624
|
-
c.tsw.WriteLiterally(".subarray(")
|
|
625
|
-
if exp.Low != nil {
|
|
626
|
-
if err := c.WriteValueExpr(exp.Low); err != nil {
|
|
627
|
-
return err
|
|
628
|
-
}
|
|
629
|
-
} else {
|
|
630
|
-
c.tsw.WriteLiterally("0") // Default low for subarray is 0
|
|
631
|
-
}
|
|
632
|
-
if exp.High != nil {
|
|
633
|
-
c.tsw.WriteLiterally(", ")
|
|
634
|
-
if err := c.WriteValueExpr(exp.High); err != nil {
|
|
635
|
-
return err
|
|
636
|
-
}
|
|
637
|
-
} else {
|
|
638
|
-
// If high is omitted, subarray goes to the end of the array.
|
|
639
|
-
// No need to write undefined or length, just close the parenthesis if low was the last arg.
|
|
640
|
-
}
|
|
641
|
-
c.tsw.WriteLiterally(")")
|
|
642
643
|
} else {
|
|
643
644
|
// Fallback to $.goSlice for actual slices (arrays) or 3-index string slices (which are rare and might need $.goSlice's complexity)
|
|
644
645
|
// Or if it's a string but has Slice3, it's not handled by simple substring.
|
|
@@ -694,3 +695,43 @@ func (c *GoToTSCompiler) WriteKeyValueExpr(exp *ast.KeyValueExpr) error {
|
|
|
694
695
|
}
|
|
695
696
|
return nil
|
|
696
697
|
}
|
|
698
|
+
|
|
699
|
+
// hasMapConstraint checks if an interface constraint includes map types
|
|
700
|
+
// For constraints like ~map[K]V, this returns true
|
|
701
|
+
func hasMapConstraint(iface *types.Interface) bool {
|
|
702
|
+
// Check if the interface has type terms that include map types
|
|
703
|
+
for i := 0; i < iface.NumEmbeddeds(); i++ {
|
|
704
|
+
embedded := iface.EmbeddedType(i)
|
|
705
|
+
if union, ok := embedded.(*types.Union); ok {
|
|
706
|
+
for j := 0; j < union.Len(); j++ {
|
|
707
|
+
term := union.Term(j)
|
|
708
|
+
if _, isMap := term.Type().Underlying().(*types.Map); isMap {
|
|
709
|
+
return true
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
} else if _, isMap := embedded.Underlying().(*types.Map); isMap {
|
|
713
|
+
return true
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return false
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// getMapValueTypeFromConstraint extracts the value type from a map constraint
|
|
720
|
+
// For constraints like ~map[K]V, this returns V
|
|
721
|
+
func getMapValueTypeFromConstraint(iface *types.Interface) types.Type {
|
|
722
|
+
// Check if the interface has type terms that include map types
|
|
723
|
+
for i := 0; i < iface.NumEmbeddeds(); i++ {
|
|
724
|
+
embedded := iface.EmbeddedType(i)
|
|
725
|
+
if union, ok := embedded.(*types.Union); ok {
|
|
726
|
+
for j := 0; j < union.Len(); j++ {
|
|
727
|
+
term := union.Term(j)
|
|
728
|
+
if mapType, isMap := term.Type().Underlying().(*types.Map); isMap {
|
|
729
|
+
return mapType.Elem()
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
} else if mapType, isMap := embedded.Underlying().(*types.Map); isMap {
|
|
733
|
+
return mapType.Elem()
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return nil
|
|
737
|
+
}
|
package/compiler/lit.go
CHANGED
|
@@ -4,7 +4,9 @@ import (
|
|
|
4
4
|
"fmt"
|
|
5
5
|
"go/ast"
|
|
6
6
|
"go/token"
|
|
7
|
+
"regexp"
|
|
7
8
|
"strconv"
|
|
9
|
+
"strings"
|
|
8
10
|
)
|
|
9
11
|
|
|
10
12
|
// WriteBasicLit translates a Go basic literal (`ast.BasicLit`) into its
|
|
@@ -15,6 +17,8 @@ import (
|
|
|
15
17
|
// `exp.Value` string, which typically corresponds to valid TypeScript syntax
|
|
16
18
|
// (e.g., `123`, `3.14`, `"hello"`). Imaginary literals might need special
|
|
17
19
|
// handling if they are to be fully supported beyond direct string output.
|
|
20
|
+
// - Legacy octal literals (e.g., `0777`) are converted to modern TypeScript
|
|
21
|
+
// octal syntax (e.g., `0o777`) to avoid ES module compatibility issues.
|
|
18
22
|
func (c *GoToTSCompiler) WriteBasicLit(exp *ast.BasicLit) {
|
|
19
23
|
if exp.Kind == token.CHAR {
|
|
20
24
|
// Go char literal 'x' is a rune (int32). Translate to its numeric code point.
|
|
@@ -26,8 +30,60 @@ func (c *GoToTSCompiler) WriteBasicLit(exp *ast.BasicLit) {
|
|
|
26
30
|
} else {
|
|
27
31
|
c.tsw.WriteLiterallyf("%d", val)
|
|
28
32
|
}
|
|
33
|
+
} else if exp.Kind == token.INT {
|
|
34
|
+
// Handle integer literals, including legacy octal conversion
|
|
35
|
+
value := exp.Value
|
|
36
|
+
|
|
37
|
+
// Check for legacy octal literals (starts with 0, followed by octal digits, but not 0x, 0b, or 0o)
|
|
38
|
+
if len(value) > 1 && value[0] == '0' && value != "0" {
|
|
39
|
+
// Check if it's already modern syntax (0x, 0b, 0o) or just legacy octal
|
|
40
|
+
if len(value) > 2 && (value[1] == 'x' || value[1] == 'X' ||
|
|
41
|
+
value[1] == 'b' || value[1] == 'B' ||
|
|
42
|
+
value[1] == 'o' || value[1] == 'O') {
|
|
43
|
+
// Already modern syntax (hex, binary, or modern octal), write as-is
|
|
44
|
+
c.tsw.WriteLiterally(value)
|
|
45
|
+
} else {
|
|
46
|
+
// Check if all remaining characters are valid octal digits (0-7)
|
|
47
|
+
isLegacyOctal := true
|
|
48
|
+
for i := 1; i < len(value); i++ {
|
|
49
|
+
if value[i] < '0' || value[i] > '7' {
|
|
50
|
+
isLegacyOctal = false
|
|
51
|
+
break
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if isLegacyOctal {
|
|
56
|
+
// Convert legacy octal 0777 to modern octal 0o777
|
|
57
|
+
c.tsw.WriteLiterallyf("0o%s", value[1:])
|
|
58
|
+
} else {
|
|
59
|
+
// Not a valid octal, write as-is (might be decimal with leading zero)
|
|
60
|
+
c.tsw.WriteLiterally(value)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
// Regular decimal integer or single zero, write as-is
|
|
65
|
+
c.tsw.WriteLiterally(value)
|
|
66
|
+
}
|
|
67
|
+
} else if exp.Kind == token.STRING {
|
|
68
|
+
// Handle string literals, with special processing for raw strings
|
|
69
|
+
value := exp.Value
|
|
70
|
+
|
|
71
|
+
// Check if this is a raw string literal (starts and ends with backticks)
|
|
72
|
+
if len(value) >= 2 && value[0] == '`' && value[len(value)-1] == '`' {
|
|
73
|
+
// This is a Go raw string - need to escape invalid \x sequences for JavaScript
|
|
74
|
+
content := value[1 : len(value)-1] // Remove surrounding backticks
|
|
75
|
+
|
|
76
|
+
// Escape invalid \x, \u, and \U sequences that would cause TS1125 errors
|
|
77
|
+
content = c.escapeInvalidEscapeSequences(content)
|
|
78
|
+
|
|
79
|
+
// Write as template literal with corrected content
|
|
80
|
+
c.tsw.WriteLiterallyf("`%s`", content)
|
|
81
|
+
} else {
|
|
82
|
+
// Regular string literal (double quotes) - write as-is
|
|
83
|
+
c.tsw.WriteLiterally(value)
|
|
84
|
+
}
|
|
29
85
|
} else {
|
|
30
|
-
// Other literals (
|
|
86
|
+
// Other literals (FLOAT, IMAG)
|
|
31
87
|
c.tsw.WriteLiterally(exp.Value)
|
|
32
88
|
}
|
|
33
89
|
}
|
|
@@ -108,7 +164,7 @@ func (c *GoToTSCompiler) WriteFuncLitValue(exp *ast.FuncLit) error {
|
|
|
108
164
|
// Declare named return variables and initialize them to their zero values
|
|
109
165
|
for _, field := range exp.Type.Results.List {
|
|
110
166
|
for _, name := range field.Names {
|
|
111
|
-
c.tsw.WriteLiterallyf("let %s: ", name.Name)
|
|
167
|
+
c.tsw.WriteLiterallyf("let %s: ", c.sanitizeIdentifier(name.Name))
|
|
112
168
|
c.WriteTypeExpr(field.Type)
|
|
113
169
|
c.tsw.WriteLiterally(" = ")
|
|
114
170
|
c.WriteZeroValueForType(c.pkg.TypesInfo.TypeOf(field.Type))
|
|
@@ -118,7 +174,7 @@ func (c *GoToTSCompiler) WriteFuncLitValue(exp *ast.FuncLit) error {
|
|
|
118
174
|
}
|
|
119
175
|
|
|
120
176
|
// Write function body
|
|
121
|
-
if err := c.WriteStmtBlock(exp.Body,
|
|
177
|
+
if err := c.WriteStmtBlock(exp.Body, true); err != nil {
|
|
122
178
|
return fmt.Errorf("failed to write block statement: %w", err)
|
|
123
179
|
}
|
|
124
180
|
|
|
@@ -129,3 +185,56 @@ func (c *GoToTSCompiler) WriteFuncLitValue(exp *ast.FuncLit) error {
|
|
|
129
185
|
|
|
130
186
|
return nil
|
|
131
187
|
}
|
|
188
|
+
|
|
189
|
+
// escapeInvalidEscapeSequences escapes \x, \u, and \U sequences in raw strings that would be invalid in JavaScript template literals.
|
|
190
|
+
// JavaScript template literals expect:
|
|
191
|
+
// - \x to be followed by exactly 2 hexadecimal digits
|
|
192
|
+
// - \u to be followed by exactly 4 hexadecimal digits
|
|
193
|
+
// - \U to be followed by exactly 8 hexadecimal digits
|
|
194
|
+
// This function escapes sequences that don't meet these requirements.
|
|
195
|
+
func (c *GoToTSCompiler) escapeInvalidEscapeSequences(content string) string {
|
|
196
|
+
// Use regex to find all \x, \u, and \U sequences
|
|
197
|
+
re := regexp.MustCompile(`\\([xuU])([0-9a-fA-F]*)`)
|
|
198
|
+
|
|
199
|
+
return re.ReplaceAllStringFunc(content, func(match string) string {
|
|
200
|
+
if len(match) < 2 {
|
|
201
|
+
return match
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
escapeType := match[1] // x, u, or U
|
|
205
|
+
suffix := match[2:] // The hex digits that follow
|
|
206
|
+
|
|
207
|
+
var expectedLength int
|
|
208
|
+
switch escapeType {
|
|
209
|
+
case 'x':
|
|
210
|
+
expectedLength = 2
|
|
211
|
+
case 'u':
|
|
212
|
+
expectedLength = 4
|
|
213
|
+
case 'U':
|
|
214
|
+
expectedLength = 8
|
|
215
|
+
default:
|
|
216
|
+
return match
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Check if it has exactly the expected number of hex digits
|
|
220
|
+
if len(suffix) == expectedLength {
|
|
221
|
+
// Check if all characters are hex digits
|
|
222
|
+
isValidHex := true
|
|
223
|
+
for _, char := range suffix {
|
|
224
|
+
if !((char >= '0' && char <= '9') ||
|
|
225
|
+
(char >= 'a' && char <= 'f') ||
|
|
226
|
+
(char >= 'A' && char <= 'F')) {
|
|
227
|
+
isValidHex = false
|
|
228
|
+
break
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if isValidHex {
|
|
232
|
+
// Valid escape sequence, keep as-is
|
|
233
|
+
return match
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Invalid escape sequence, escape the backslash
|
|
238
|
+
return strings.Replace(match, `\`, `\\`, 1)
|
|
239
|
+
})
|
|
240
|
+
}
|
package/compiler/primitive.go
CHANGED
|
@@ -100,12 +100,12 @@ var tokenMap = map[token.Token]string{
|
|
|
100
100
|
token.QUO_ASSIGN: "/=",
|
|
101
101
|
token.REM_ASSIGN: "%=",
|
|
102
102
|
|
|
103
|
-
token.AND_ASSIGN:
|
|
104
|
-
token.OR_ASSIGN:
|
|
105
|
-
token.XOR_ASSIGN:
|
|
106
|
-
token.SHL_ASSIGN:
|
|
107
|
-
token.SHR_ASSIGN:
|
|
108
|
-
token.AND_NOT_ASSIGN: "&^=",
|
|
103
|
+
token.AND_ASSIGN: "&=",
|
|
104
|
+
token.OR_ASSIGN: "|=",
|
|
105
|
+
token.XOR_ASSIGN: "^=", // TODO: check if this works
|
|
106
|
+
token.SHL_ASSIGN: "<<=",
|
|
107
|
+
token.SHR_ASSIGN: ">>=",
|
|
108
|
+
// token.AND_NOT_ASSIGN: "&^=", // Handled specially in assignment logic
|
|
109
109
|
|
|
110
110
|
token.LAND: "&&",
|
|
111
111
|
token.LOR: "||",
|
package/compiler/protobuf.go
CHANGED
|
@@ -61,11 +61,6 @@ func isProtobufGoLitePackage(pkgPath string) bool {
|
|
|
61
61
|
if strings.HasPrefix(pkgPath, "github.com/aperturerobotics/json-iterator-lite") {
|
|
62
62
|
return true
|
|
63
63
|
}
|
|
64
|
-
// Skip other packages commonly used only by .pb.go files
|
|
65
|
-
switch pkgPath {
|
|
66
|
-
case "encoding/json", "encoding/base64", "strconv", "fmt":
|
|
67
|
-
return true
|
|
68
|
-
}
|
|
69
64
|
return false
|
|
70
65
|
}
|
|
71
66
|
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package compiler
|
|
2
|
+
|
|
3
|
+
// sanitizeIdentifier checks if an identifier is a JavaScript/TypeScript reserved word
|
|
4
|
+
// or conflicts with built-in types, and transforms it if needed. This prevents
|
|
5
|
+
// compilation errors when Go identifiers conflict with JS/TS keywords or built-ins.
|
|
6
|
+
func sanitizeIdentifier(name string) string {
|
|
7
|
+
// Don't sanitize boolean literals - they are valid in both Go and JS/TS
|
|
8
|
+
if name == "true" || name == "false" {
|
|
9
|
+
return name
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Handle TypeScript built-in types that conflict with Go type parameter names
|
|
13
|
+
builtinTypes := map[string]string{
|
|
14
|
+
"Map": "MapType",
|
|
15
|
+
// Add other built-in types as needed
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if replacement, exists := builtinTypes[name]; exists {
|
|
19
|
+
return replacement
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// List of JavaScript/TypeScript reserved words that could conflict
|
|
23
|
+
reservedWords := map[string]bool{
|
|
24
|
+
"abstract": true,
|
|
25
|
+
"any": true,
|
|
26
|
+
"as": true,
|
|
27
|
+
"asserts": true,
|
|
28
|
+
"async": true,
|
|
29
|
+
"await": true,
|
|
30
|
+
"boolean": true,
|
|
31
|
+
"break": true,
|
|
32
|
+
"case": true,
|
|
33
|
+
"catch": true,
|
|
34
|
+
"class": true,
|
|
35
|
+
"const": true,
|
|
36
|
+
"constructor": true,
|
|
37
|
+
"continue": true,
|
|
38
|
+
"debugger": true,
|
|
39
|
+
"declare": true,
|
|
40
|
+
"default": true,
|
|
41
|
+
"delete": true,
|
|
42
|
+
"do": true,
|
|
43
|
+
"else": true,
|
|
44
|
+
"enum": true,
|
|
45
|
+
"export": true,
|
|
46
|
+
"extends": true,
|
|
47
|
+
"finally": true,
|
|
48
|
+
"for": true,
|
|
49
|
+
"from": true,
|
|
50
|
+
"function": true,
|
|
51
|
+
"get": true,
|
|
52
|
+
"if": true,
|
|
53
|
+
"implements": true,
|
|
54
|
+
"import": true,
|
|
55
|
+
"in": true,
|
|
56
|
+
"instanceof": true,
|
|
57
|
+
"interface": true,
|
|
58
|
+
"is": true,
|
|
59
|
+
"keyof": true,
|
|
60
|
+
"let": true,
|
|
61
|
+
"module": true,
|
|
62
|
+
"namespace": true,
|
|
63
|
+
"never": true,
|
|
64
|
+
"new": true,
|
|
65
|
+
"null": true,
|
|
66
|
+
"number": true,
|
|
67
|
+
"object": true,
|
|
68
|
+
"of": true,
|
|
69
|
+
"package": true,
|
|
70
|
+
"private": true,
|
|
71
|
+
"protected": true,
|
|
72
|
+
"public": true,
|
|
73
|
+
"readonly": true,
|
|
74
|
+
"require": true,
|
|
75
|
+
"return": true,
|
|
76
|
+
"set": true,
|
|
77
|
+
"static": true,
|
|
78
|
+
"string": true,
|
|
79
|
+
"super": true,
|
|
80
|
+
"switch": true,
|
|
81
|
+
"symbol": true,
|
|
82
|
+
"this": true,
|
|
83
|
+
"throw": true,
|
|
84
|
+
"try": true,
|
|
85
|
+
"type": true,
|
|
86
|
+
"typeof": true,
|
|
87
|
+
"undefined": true,
|
|
88
|
+
"unique": true,
|
|
89
|
+
"unknown": true,
|
|
90
|
+
"var": true,
|
|
91
|
+
"void": true,
|
|
92
|
+
"while": true,
|
|
93
|
+
"with": true,
|
|
94
|
+
"yield": true,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if reservedWords[name] {
|
|
98
|
+
return "_" + name
|
|
99
|
+
}
|
|
100
|
+
return name
|
|
101
|
+
}
|
package/compiler/spec-value.go
CHANGED
|
@@ -91,7 +91,7 @@ func (c *GoToTSCompiler) WriteValueSpec(a *ast.ValueSpec) error {
|
|
|
91
91
|
c.tsw.WriteLiterally("export ")
|
|
92
92
|
}
|
|
93
93
|
c.tsw.WriteLiterally("let ")
|
|
94
|
-
c.tsw.WriteLiterally(name.Name)
|
|
94
|
+
c.tsw.WriteLiterally(c.sanitizeIdentifier(name.Name))
|
|
95
95
|
|
|
96
96
|
// Write type annotation if:
|
|
97
97
|
// 1. Not a slice conversion (normal case), OR
|
|
@@ -193,27 +193,34 @@ func (c *GoToTSCompiler) WriteValueSpec(a *ast.ValueSpec) error {
|
|
|
193
193
|
if hasInitializer {
|
|
194
194
|
// Handle &v initializer specifically for unvarrefed variables
|
|
195
195
|
if unaryExpr, isUnary := initializerExpr.(*ast.UnaryExpr); isUnary && unaryExpr.Op == token.AND {
|
|
196
|
-
// Initializer is &
|
|
197
|
-
// Check if
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
196
|
+
// Initializer is &expr
|
|
197
|
+
// Check if expr is an identifier (variable) or something else (e.g., composite literal)
|
|
198
|
+
if unaryExprXIdent, ok := unaryExpr.X.(*ast.Ident); ok {
|
|
199
|
+
// Case: &variable
|
|
200
|
+
// Check if the variable is varrefed
|
|
201
201
|
innerObj := c.pkg.TypesInfo.Uses[unaryExprXIdent]
|
|
202
|
-
needsVarRefOperand
|
|
203
|
-
}
|
|
202
|
+
needsVarRefOperand := innerObj != nil && c.analysis.NeedsVarRef(innerObj)
|
|
204
203
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
204
|
+
// If variable is varrefed, assign the varRef itself (variable)
|
|
205
|
+
// If variable is not varrefed, assign $.varRef(variable)
|
|
206
|
+
if needsVarRefOperand {
|
|
207
|
+
// do not write .value here.
|
|
208
|
+
c.WriteIdent(unaryExprXIdent, false)
|
|
209
|
+
} else {
|
|
210
|
+
// &unvarrefedVar -> $.varRef(unvarrefedVar)
|
|
211
|
+
c.tsw.WriteLiterally("$.varRef(")
|
|
212
|
+
if err := c.WriteValueExpr(unaryExpr.X); err != nil { // Write 'variable'
|
|
213
|
+
return err
|
|
214
|
+
}
|
|
215
|
+
c.tsw.WriteLiterally(")")
|
|
216
|
+
}
|
|
210
217
|
} else {
|
|
211
|
-
// &
|
|
212
|
-
|
|
213
|
-
|
|
218
|
+
// Case: &compositeLiteral or &otherExpression
|
|
219
|
+
// For composite literals and other expressions, just write the expression directly
|
|
220
|
+
// Example: &MyStruct{} -> new MyStruct({})
|
|
221
|
+
if err := c.WriteValueExpr(unaryExpr.X); err != nil {
|
|
214
222
|
return err
|
|
215
223
|
}
|
|
216
|
-
c.tsw.WriteLiterally(")")
|
|
217
224
|
}
|
|
218
225
|
} else {
|
|
219
226
|
// Check if this is a named type with methods and the initializer is a basic value
|
|
@@ -315,7 +322,7 @@ func (c *GoToTSCompiler) WriteValueSpec(a *ast.ValueSpec) error {
|
|
|
315
322
|
if i != 0 {
|
|
316
323
|
c.tsw.WriteLiterally(", ")
|
|
317
324
|
}
|
|
318
|
-
c.tsw.WriteLiterally(name.Name)
|
|
325
|
+
c.tsw.WriteLiterally(c.sanitizeIdentifier(name.Name))
|
|
319
326
|
// TODO: Add type annotations for multi-var declarations if possible/needed
|
|
320
327
|
}
|
|
321
328
|
c.tsw.WriteLiterally("]")
|