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/analysis.go
CHANGED
|
@@ -317,9 +317,6 @@ func (v *analysisVisitor) Visit(node ast.Node) ast.Visitor {
|
|
|
317
317
|
if lhsObj == nil {
|
|
318
318
|
continue
|
|
319
319
|
}
|
|
320
|
-
// Ensure usage info exists for LHS
|
|
321
|
-
lhsUsageInfo := v.getOrCreateUsageInfo(lhsObj)
|
|
322
|
-
|
|
323
320
|
// Check if there's a corresponding initial value (RHS)
|
|
324
321
|
if valueSpec.Values != nil && i < len(valueSpec.Values) {
|
|
325
322
|
rhsExpr := valueSpec.Values[i]
|
|
@@ -327,12 +324,18 @@ func (v *analysisVisitor) Visit(node ast.Node) ast.Visitor {
|
|
|
327
324
|
// --- Analyze RHS and Update Usage Info (similar to AssignStmt) ---
|
|
328
325
|
assignmentType := DirectAssignment
|
|
329
326
|
var sourceObj types.Object
|
|
327
|
+
shouldTrackUsage := true
|
|
330
328
|
|
|
331
329
|
if unaryExpr, ok := rhsExpr.(*ast.UnaryExpr); ok && unaryExpr.Op == token.AND {
|
|
332
|
-
// Case: var lhs = &
|
|
330
|
+
// Case: var lhs = &rhs_expr
|
|
333
331
|
assignmentType = AddressOfAssignment
|
|
334
332
|
if rhsIdent, ok := unaryExpr.X.(*ast.Ident); ok {
|
|
333
|
+
// Case: var lhs = &rhs_ident (taking address of a variable)
|
|
335
334
|
sourceObj = v.pkg.TypesInfo.ObjectOf(rhsIdent)
|
|
335
|
+
} else {
|
|
336
|
+
// Case: var lhs = &CompositeLit{} (taking address of composite literal)
|
|
337
|
+
// No variable tracking needed - this doesn't create VarRef requirements
|
|
338
|
+
shouldTrackUsage = false
|
|
336
339
|
}
|
|
337
340
|
} else if rhsIdent, ok := rhsExpr.(*ast.Ident); ok {
|
|
338
341
|
// Case: var lhs = rhs_ident
|
|
@@ -341,21 +344,26 @@ func (v *analysisVisitor) Visit(node ast.Node) ast.Visitor {
|
|
|
341
344
|
}
|
|
342
345
|
|
|
343
346
|
// --- Record Usage ---
|
|
344
|
-
if
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
347
|
+
// Only create usage tracking if we're dealing with variable references
|
|
348
|
+
if shouldTrackUsage {
|
|
349
|
+
// Ensure usage info exists for LHS only when needed
|
|
350
|
+
lhsUsageInfo := v.getOrCreateUsageInfo(lhsObj)
|
|
351
|
+
|
|
352
|
+
if sourceObj != nil {
|
|
353
|
+
// Record source for LHS
|
|
354
|
+
lhsUsageInfo.Sources = append(lhsUsageInfo.Sources, AssignmentInfo{
|
|
355
|
+
Object: sourceObj,
|
|
356
|
+
Type: assignmentType,
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
// Record destination for RHS source
|
|
360
|
+
sourceUsageInfo := v.getOrCreateUsageInfo(sourceObj)
|
|
361
|
+
sourceUsageInfo.Destinations = append(sourceUsageInfo.Destinations, AssignmentInfo{
|
|
362
|
+
Object: lhsObj,
|
|
363
|
+
Type: assignmentType,
|
|
364
|
+
})
|
|
365
|
+
}
|
|
357
366
|
}
|
|
358
|
-
// Note: We might need to handle var lhs = &T{} cases later if necessary.
|
|
359
367
|
}
|
|
360
368
|
}
|
|
361
369
|
}
|
|
@@ -418,12 +426,12 @@ func (v *analysisVisitor) Visit(node ast.Node) ast.Visitor {
|
|
|
418
426
|
}
|
|
419
427
|
}
|
|
420
428
|
|
|
421
|
-
// Store named return variables
|
|
429
|
+
// Store named return variables (sanitized for TypeScript)
|
|
422
430
|
if n.Type != nil && n.Type.Results != nil {
|
|
423
431
|
var namedReturns []string
|
|
424
432
|
for _, field := range n.Type.Results.List {
|
|
425
433
|
for _, name := range field.Names {
|
|
426
|
-
namedReturns = append(namedReturns, name.Name)
|
|
434
|
+
namedReturns = append(namedReturns, sanitizeIdentifier(name.Name))
|
|
427
435
|
}
|
|
428
436
|
}
|
|
429
437
|
if len(namedReturns) > 0 {
|
|
@@ -864,8 +872,8 @@ func AnalyzeFile(file *ast.File, pkg *packages.Package, analysis *Analysis, cmap
|
|
|
864
872
|
importVars: make(map[string]struct{}),
|
|
865
873
|
}
|
|
866
874
|
|
|
867
|
-
// Use the import name or
|
|
868
|
-
key := path
|
|
875
|
+
// Use the import name or package name as the key
|
|
876
|
+
key := packageNameFromGoPath(path)
|
|
869
877
|
if name != "" {
|
|
870
878
|
key = name
|
|
871
879
|
}
|
|
@@ -88,6 +88,20 @@ func main() {
|
|
|
88
88
|
"data": {NeedsVarRef: false, NeedsVarRefAccess: false}, // Should NOT be varrefed
|
|
89
89
|
},
|
|
90
90
|
},
|
|
91
|
+
{
|
|
92
|
+
name: "var_declaration_composite_literal",
|
|
93
|
+
code: `package main
|
|
94
|
+
type MockInode struct {
|
|
95
|
+
Value int
|
|
96
|
+
}
|
|
97
|
+
func main() {
|
|
98
|
+
var childInode *MockInode = &MockInode{Value: 42}
|
|
99
|
+
println("childInode.Value:", childInode.Value)
|
|
100
|
+
}`,
|
|
101
|
+
expected: map[string]AnalysisExpectation{
|
|
102
|
+
"childInode": {NeedsVarRef: false, NeedsVarRefAccess: false}, // Should NOT be varrefed
|
|
103
|
+
},
|
|
104
|
+
},
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
for _, tt := range tests {
|
package/compiler/assignment.go
CHANGED
|
@@ -94,7 +94,7 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
94
94
|
if isLHSVarRefed && lhsIdent != nil {
|
|
95
95
|
c.tsw.WriteLiterally("let ")
|
|
96
96
|
// Just write the identifier name without .value
|
|
97
|
-
c.tsw.WriteLiterally(lhsIdent.Name)
|
|
97
|
+
c.tsw.WriteLiterally(c.sanitizeIdentifier(lhsIdent.Name))
|
|
98
98
|
// No type annotation, allow TypeScript to infer it from varRef.
|
|
99
99
|
c.tsw.WriteLiterally(" = ")
|
|
100
100
|
|
|
@@ -197,6 +197,7 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
197
197
|
currentIsMapIndex := false
|
|
198
198
|
if indexExpr, ok := l.(*ast.IndexExpr); ok {
|
|
199
199
|
if tv, ok := c.pkg.TypesInfo.Types[indexExpr.X]; ok {
|
|
200
|
+
// Check if it's a concrete map type
|
|
200
201
|
if _, isMap := tv.Type.Underlying().(*types.Map); isMap {
|
|
201
202
|
currentIsMapIndex = true
|
|
202
203
|
if i == 0 {
|
|
@@ -213,6 +214,31 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
213
214
|
}
|
|
214
215
|
c.tsw.WriteLiterally(", ")
|
|
215
216
|
// Value will be added after operator and RHS
|
|
217
|
+
} else if typeParam, isTypeParam := tv.Type.(*types.TypeParam); isTypeParam {
|
|
218
|
+
// Check if the type parameter is constrained to be a map type
|
|
219
|
+
constraint := typeParam.Constraint()
|
|
220
|
+
if constraint != nil {
|
|
221
|
+
underlying := constraint.Underlying()
|
|
222
|
+
if iface, isInterface := underlying.(*types.Interface); isInterface {
|
|
223
|
+
if hasMapConstraint(iface) {
|
|
224
|
+
currentIsMapIndex = true
|
|
225
|
+
if i == 0 {
|
|
226
|
+
isMapIndexLHS = true
|
|
227
|
+
}
|
|
228
|
+
// Use mapSet helper for type parameter constrained to map
|
|
229
|
+
c.tsw.WriteLiterally("$.mapSet(")
|
|
230
|
+
if err := c.WriteValueExpr(indexExpr.X); err != nil { // Map
|
|
231
|
+
return err
|
|
232
|
+
}
|
|
233
|
+
c.tsw.WriteLiterally(", ")
|
|
234
|
+
if err := c.WriteValueExpr(indexExpr.Index); err != nil { // Key
|
|
235
|
+
return err
|
|
236
|
+
}
|
|
237
|
+
c.tsw.WriteLiterally(", ")
|
|
238
|
+
// Value will be added after operator and RHS
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
216
242
|
}
|
|
217
243
|
}
|
|
218
244
|
}
|
|
@@ -256,14 +282,21 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
256
282
|
// Continue, we've already written part of the mapSet() function call
|
|
257
283
|
} else {
|
|
258
284
|
c.tsw.WriteLiterally(" ")
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
285
|
+
|
|
286
|
+
// Special handling for &^= (bitwise AND NOT assignment)
|
|
287
|
+
if tok == token.AND_NOT_ASSIGN {
|
|
288
|
+
// Transform x &^= y to x &= ~(y)
|
|
289
|
+
c.tsw.WriteLiterally("&= ~(")
|
|
263
290
|
} else {
|
|
264
|
-
|
|
291
|
+
tokStr, ok := TokenToTs(tok) // Use explicit gstypes alias
|
|
292
|
+
if !ok {
|
|
293
|
+
c.tsw.WriteLiterally("?= ")
|
|
294
|
+
c.tsw.WriteCommentLine("Unknown token " + tok.String())
|
|
295
|
+
} else {
|
|
296
|
+
c.tsw.WriteLiterally(tokStr)
|
|
297
|
+
}
|
|
298
|
+
c.tsw.WriteLiterally(" ")
|
|
265
299
|
}
|
|
266
|
-
c.tsw.WriteLiterally(" ")
|
|
267
300
|
}
|
|
268
301
|
|
|
269
302
|
// Write RHS
|
|
@@ -327,6 +360,11 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
327
360
|
}
|
|
328
361
|
}
|
|
329
362
|
|
|
363
|
+
// Close the parenthesis for &^= transformation
|
|
364
|
+
if tok == token.AND_NOT_ASSIGN && !(isMapIndexLHS && len(lhs) == 1) {
|
|
365
|
+
c.tsw.WriteLiterally(")")
|
|
366
|
+
}
|
|
367
|
+
|
|
330
368
|
// If the LHS was a single map index, close the mapSet call
|
|
331
369
|
if isMapIndexLHS && len(lhs) == 1 {
|
|
332
370
|
c.tsw.WriteLiterally(")")
|
package/compiler/builtin_test.go
CHANGED
package/compiler/compiler.go
CHANGED
|
@@ -611,7 +611,15 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
|
|
|
611
611
|
// Generate auto-imports for functions from other files in the same package
|
|
612
612
|
currentFileName := strings.TrimSuffix(filepath.Base(c.fullPath), ".go")
|
|
613
613
|
if imports := c.PackageAnalysis.FunctionCalls[currentFileName]; imports != nil {
|
|
614
|
-
|
|
614
|
+
// Sort source files for consistent import order
|
|
615
|
+
var sourceFiles []string
|
|
616
|
+
for sourceFile := range imports {
|
|
617
|
+
sourceFiles = append(sourceFiles, sourceFile)
|
|
618
|
+
}
|
|
619
|
+
sort.Strings(sourceFiles)
|
|
620
|
+
|
|
621
|
+
for _, sourceFile := range sourceFiles {
|
|
622
|
+
functions := imports[sourceFile]
|
|
615
623
|
if len(functions) > 0 {
|
|
616
624
|
// Sort functions for consistent output
|
|
617
625
|
sort.Strings(functions)
|
|
@@ -891,94 +899,9 @@ func (c *GoToTSCompiler) WriteDoc(doc *ast.CommentGroup) {
|
|
|
891
899
|
}
|
|
892
900
|
}
|
|
893
901
|
|
|
894
|
-
// sanitizeIdentifier
|
|
895
|
-
// and prefixes it with an underscore if it is. This prevents compilation errors
|
|
896
|
-
// when Go identifiers conflict with JS/TS keywords.
|
|
902
|
+
// sanitizeIdentifier is a method wrapper around the package-level sanitizeIdentifier function
|
|
897
903
|
func (c *GoToTSCompiler) sanitizeIdentifier(name string) string {
|
|
898
|
-
|
|
899
|
-
if name == "true" || name == "false" {
|
|
900
|
-
return name
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
// List of JavaScript/TypeScript reserved words that could conflict
|
|
904
|
-
reservedWords := map[string]bool{
|
|
905
|
-
"abstract": true,
|
|
906
|
-
"any": true,
|
|
907
|
-
"as": true,
|
|
908
|
-
"asserts": true,
|
|
909
|
-
"async": true,
|
|
910
|
-
"await": true,
|
|
911
|
-
"boolean": true,
|
|
912
|
-
"break": true,
|
|
913
|
-
"case": true,
|
|
914
|
-
"catch": true,
|
|
915
|
-
"class": true,
|
|
916
|
-
"const": true,
|
|
917
|
-
"constructor": true,
|
|
918
|
-
"continue": true,
|
|
919
|
-
"debugger": true,
|
|
920
|
-
"declare": true,
|
|
921
|
-
"default": true,
|
|
922
|
-
"delete": true,
|
|
923
|
-
"do": true,
|
|
924
|
-
"else": true,
|
|
925
|
-
"enum": true,
|
|
926
|
-
"export": true,
|
|
927
|
-
"extends": true,
|
|
928
|
-
"finally": true,
|
|
929
|
-
"for": true,
|
|
930
|
-
"from": true,
|
|
931
|
-
"function": true,
|
|
932
|
-
"get": true,
|
|
933
|
-
"if": true,
|
|
934
|
-
"implements": true,
|
|
935
|
-
"import": true,
|
|
936
|
-
"in": true,
|
|
937
|
-
"instanceof": true,
|
|
938
|
-
"interface": true,
|
|
939
|
-
"is": true,
|
|
940
|
-
"keyof": true,
|
|
941
|
-
"let": true,
|
|
942
|
-
"module": true,
|
|
943
|
-
"namespace": true,
|
|
944
|
-
"never": true,
|
|
945
|
-
"new": true,
|
|
946
|
-
"null": true,
|
|
947
|
-
"number": true,
|
|
948
|
-
"object": true,
|
|
949
|
-
"of": true,
|
|
950
|
-
"package": true,
|
|
951
|
-
"private": true,
|
|
952
|
-
"protected": true,
|
|
953
|
-
"public": true,
|
|
954
|
-
"readonly": true,
|
|
955
|
-
"require": true,
|
|
956
|
-
"return": true,
|
|
957
|
-
"set": true,
|
|
958
|
-
"static": true,
|
|
959
|
-
"string": true,
|
|
960
|
-
"super": true,
|
|
961
|
-
"switch": true,
|
|
962
|
-
"symbol": true,
|
|
963
|
-
"this": true,
|
|
964
|
-
"throw": true,
|
|
965
|
-
"try": true,
|
|
966
|
-
"type": true,
|
|
967
|
-
"typeof": true,
|
|
968
|
-
"undefined": true,
|
|
969
|
-
"unique": true,
|
|
970
|
-
"unknown": true,
|
|
971
|
-
"var": true,
|
|
972
|
-
"void": true,
|
|
973
|
-
"while": true,
|
|
974
|
-
"with": true,
|
|
975
|
-
"yield": true,
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
if reservedWords[name] {
|
|
979
|
-
return "_" + name
|
|
980
|
-
}
|
|
981
|
-
return name
|
|
904
|
+
return sanitizeIdentifier(name)
|
|
982
905
|
}
|
|
983
906
|
|
|
984
907
|
// writeConstantValue writes the evaluated value of a Go constant to TypeScript.
|
|
@@ -996,7 +919,10 @@ func (c *GoToTSCompiler) writeConstantValue(constObj *types.Const) {
|
|
|
996
919
|
c.tsw.WriteLiterally(val.String())
|
|
997
920
|
case constant.String:
|
|
998
921
|
// For string constants, write as a quoted string literal
|
|
999
|
-
|
|
922
|
+
// Use constant.StringVal to get the full string value without truncation,
|
|
923
|
+
// then manually add quotes since StringVal returns the unquoted string
|
|
924
|
+
stringValue := constant.StringVal(val)
|
|
925
|
+
c.tsw.WriteLiterallyf("%q", stringValue) // %q adds proper quotes and escaping
|
|
1000
926
|
case constant.Bool:
|
|
1001
927
|
// For boolean constants, write true/false
|
|
1002
928
|
if constant.BoolVal(val) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
package compiler_test
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
-
"context"
|
|
5
4
|
"os"
|
|
6
5
|
"os/exec"
|
|
7
6
|
"path/filepath"
|
|
@@ -12,9 +11,7 @@ import (
|
|
|
12
11
|
"sync/atomic"
|
|
13
12
|
"testing"
|
|
14
13
|
|
|
15
|
-
"github.com/aperturerobotics/goscript/compiler"
|
|
16
14
|
"github.com/aperturerobotics/goscript/compliance"
|
|
17
|
-
"github.com/sirupsen/logrus"
|
|
18
15
|
)
|
|
19
16
|
|
|
20
17
|
// NOTE: this is here instead of compliance/compliance_test.go so coverage ends up in this package.
|
|
@@ -140,53 +137,3 @@ func getParentGoModulePath() (string, error) {
|
|
|
140
137
|
}
|
|
141
138
|
return strings.TrimSpace(string(output)), nil
|
|
142
139
|
}
|
|
143
|
-
|
|
144
|
-
func TestUnsafePackageCompilation(t *testing.T) {
|
|
145
|
-
// Create a temporary directory for the test output
|
|
146
|
-
tempDir, err := os.MkdirTemp("", "goscript-test-unsafe")
|
|
147
|
-
if err != nil {
|
|
148
|
-
t.Fatalf("Failed to create temp dir: %v", err)
|
|
149
|
-
}
|
|
150
|
-
defer os.RemoveAll(tempDir)
|
|
151
|
-
|
|
152
|
-
// Setup logger
|
|
153
|
-
log := logrus.New()
|
|
154
|
-
log.SetLevel(logrus.DebugLevel)
|
|
155
|
-
le := logrus.NewEntry(log)
|
|
156
|
-
|
|
157
|
-
// Test with AllDependencies=true and DisableEmitBuiltin=false to ensure handwritten packages are copied
|
|
158
|
-
config := &compiler.Config{
|
|
159
|
-
OutputPath: tempDir,
|
|
160
|
-
AllDependencies: true,
|
|
161
|
-
DisableEmitBuiltin: false, // This ensures handwritten packages are copied to output
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
comp, err := compiler.NewCompiler(config, le, nil)
|
|
165
|
-
if err != nil {
|
|
166
|
-
t.Fatalf("Failed to create compiler: %v", err)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Try to compile a package that has dependencies that import unsafe
|
|
170
|
-
// We'll use "sync/atomic" which imports unsafe but doesn't have a handwritten equivalent
|
|
171
|
-
result, err := comp.CompilePackages(context.Background(), "sync/atomic")
|
|
172
|
-
// This should now succeed since we have a handwritten unsafe package
|
|
173
|
-
if err != nil {
|
|
174
|
-
t.Fatalf("Expected compilation to succeed with handwritten unsafe package, but it failed: %v", err)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Verify that the unsafe package was copied (not compiled) since it has a handwritten equivalent
|
|
178
|
-
if !slices.Contains(result.CopiedPackages, "unsafe") {
|
|
179
|
-
t.Errorf("Expected unsafe package to be in CopiedPackages, but it wasn't. CopiedPackages: %v", result.CopiedPackages)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Verify that sync/atomic was compiled
|
|
183
|
-
if !slices.Contains(result.CompiledPackages, "sync/atomic") {
|
|
184
|
-
t.Errorf("Expected sync/atomic package to be in CompiledPackages, but it wasn't. CompiledPackages: %v", result.CompiledPackages)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Check that the unsafe package directory exists in the output
|
|
188
|
-
unsafePath := filepath.Join(tempDir, "@goscript/unsafe")
|
|
189
|
-
if _, err := os.Stat(unsafePath); os.IsNotExist(err) {
|
|
190
|
-
t.Errorf("unsafe package directory was not created at %s", unsafePath)
|
|
191
|
-
}
|
|
192
|
-
}
|
|
@@ -3,6 +3,7 @@ package compiler
|
|
|
3
3
|
import (
|
|
4
4
|
"fmt"
|
|
5
5
|
"go/ast"
|
|
6
|
+
"go/constant"
|
|
6
7
|
"go/token"
|
|
7
8
|
"go/types"
|
|
8
9
|
"slices"
|
|
@@ -108,6 +109,9 @@ func (c *GoToTSCompiler) WriteCompositeLit(exp *ast.CompositeLit) error {
|
|
|
108
109
|
if at, ok := typ.Underlying().(*types.Array); ok {
|
|
109
110
|
arrayLen = int(at.Len())
|
|
110
111
|
goElemType = at.Elem()
|
|
112
|
+
} else if st, ok := typ.Underlying().(*types.Slice); ok {
|
|
113
|
+
// For slices, get the element type
|
|
114
|
+
goElemType = st.Elem()
|
|
111
115
|
}
|
|
112
116
|
}
|
|
113
117
|
if arrType.Len != nil {
|
|
@@ -116,6 +120,13 @@ func (c *GoToTSCompiler) WriteCompositeLit(exp *ast.CompositeLit) error {
|
|
|
116
120
|
if _, err := fmt.Sscan(bl.Value, &arrayLen); err != nil {
|
|
117
121
|
return fmt.Errorf("failed to parse array length from basic literal: %w", err)
|
|
118
122
|
}
|
|
123
|
+
} else {
|
|
124
|
+
// Try to evaluate as a constant expression (e.g., const N = 5; [N]int{})
|
|
125
|
+
if lenValue := c.evaluateConstantExpr(arrType.Len); lenValue != nil {
|
|
126
|
+
if length, ok := lenValue.(int); ok {
|
|
127
|
+
arrayLen = length
|
|
128
|
+
}
|
|
129
|
+
}
|
|
119
130
|
}
|
|
120
131
|
}
|
|
121
132
|
elemType = arrType.Elt
|
|
@@ -128,28 +139,35 @@ func (c *GoToTSCompiler) WriteCompositeLit(exp *ast.CompositeLit) error {
|
|
|
128
139
|
|
|
129
140
|
for _, elm := range exp.Elts {
|
|
130
141
|
if kv, ok := elm.(*ast.KeyValueExpr); ok {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
// Try to evaluate the key expression as a constant (handles both literals and expressions)
|
|
143
|
+
if keyValue := c.evaluateConstantExpr(kv.Key); keyValue != nil {
|
|
144
|
+
if index, ok := keyValue.(int); ok {
|
|
145
|
+
elements[index] = kv.Value
|
|
146
|
+
if index > maxIndex {
|
|
147
|
+
maxIndex = index
|
|
148
|
+
}
|
|
149
|
+
hasKeyedElements = true
|
|
150
|
+
} else {
|
|
151
|
+
return fmt.Errorf("keyed array literal key must evaluate to an integer, got %T", keyValue)
|
|
139
152
|
}
|
|
140
|
-
hasKeyedElements = true
|
|
141
153
|
} else {
|
|
142
|
-
|
|
143
|
-
if err := c.WriteVarRefedValue(elm); err != nil {
|
|
144
|
-
return fmt.Errorf("failed to write keyed array literal element with unhandled key type: %w", err)
|
|
145
|
-
}
|
|
154
|
+
return fmt.Errorf("keyed array literal key must be a constant expression")
|
|
146
155
|
}
|
|
147
156
|
} else {
|
|
148
|
-
elements
|
|
149
|
-
|
|
150
|
-
|
|
157
|
+
// For unkeyed elements, place them at the next available index
|
|
158
|
+
// If we have keyed elements, start after the highest keyed index
|
|
159
|
+
currentIndex := orderedCount
|
|
160
|
+
if hasKeyedElements && orderedCount <= maxIndex {
|
|
161
|
+
currentIndex = maxIndex + 1
|
|
162
|
+
for elements[currentIndex] != nil {
|
|
163
|
+
currentIndex++
|
|
164
|
+
}
|
|
151
165
|
}
|
|
152
|
-
|
|
166
|
+
elements[currentIndex] = elm
|
|
167
|
+
if currentIndex > maxIndex {
|
|
168
|
+
maxIndex = currentIndex
|
|
169
|
+
}
|
|
170
|
+
orderedCount = currentIndex + 1
|
|
153
171
|
}
|
|
154
172
|
}
|
|
155
173
|
|
|
@@ -639,3 +657,28 @@ func (c *GoToTSCompiler) WriteVarRefedValue(expr ast.Expr) error {
|
|
|
639
657
|
return c.WriteValueExpr(expr)
|
|
640
658
|
}
|
|
641
659
|
}
|
|
660
|
+
|
|
661
|
+
// evaluateConstantExpr attempts to evaluate a Go expression as a compile-time constant.
|
|
662
|
+
// It returns the constant value if successful, or nil if the expression is not a constant.
|
|
663
|
+
// This is used for evaluating array literal keys that are constant expressions.
|
|
664
|
+
func (c *GoToTSCompiler) evaluateConstantExpr(expr ast.Expr) interface{} {
|
|
665
|
+
// Use the type checker's constant evaluation
|
|
666
|
+
if tv, ok := c.pkg.TypesInfo.Types[expr]; ok && tv.Value != nil {
|
|
667
|
+
// The expression has a constant value
|
|
668
|
+
switch tv.Value.Kind() {
|
|
669
|
+
case constant.Int:
|
|
670
|
+
if val, exact := constant.Int64Val(tv.Value); exact {
|
|
671
|
+
return int(val)
|
|
672
|
+
}
|
|
673
|
+
case constant.Float:
|
|
674
|
+
if val, exact := constant.Float64Val(tv.Value); exact {
|
|
675
|
+
return val
|
|
676
|
+
}
|
|
677
|
+
case constant.String:
|
|
678
|
+
return constant.StringVal(tv.Value)
|
|
679
|
+
case constant.Bool:
|
|
680
|
+
return constant.BoolVal(tv.Value)
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
return nil
|
|
684
|
+
}
|
package/compiler/decl.go
CHANGED
|
@@ -118,7 +118,7 @@ func (c *GoToTSCompiler) WriteFuncDeclAsFunction(decl *ast.FuncDecl) error {
|
|
|
118
118
|
// Declare named return variables and initialize them to their zero values
|
|
119
119
|
for _, field := range decl.Type.Results.List {
|
|
120
120
|
for _, name := range field.Names {
|
|
121
|
-
c.tsw.WriteLiterallyf("let %s: ", name.Name)
|
|
121
|
+
c.tsw.WriteLiterallyf("let %s: ", c.sanitizeIdentifier(name.Name))
|
|
122
122
|
c.WriteTypeExpr(field.Type)
|
|
123
123
|
c.tsw.WriteLiterally(" = ")
|
|
124
124
|
c.WriteZeroValueForType(c.pkg.TypesInfo.TypeOf(field.Type))
|