goscript 0.0.34 → 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 +28 -20
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +19 -7
- package/compiler/compiler.go +15 -99
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +233 -35
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +13 -37
- package/compiler/lit.go +111 -2
- 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 +22 -1
- package/compiler/stmt.go +26 -9
- package/compiler/type.go +3 -3
- package/dist/gs/builtin/builtin.d.ts +3 -2
- package/dist/gs/builtin/builtin.js +2 -1
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +7 -1
- package/dist/gs/builtin/slice.js +112 -22
- 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 -2
- package/dist/gs/maps/index.js +2 -2
- package/dist/gs/maps/index.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +7 -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.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/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 +9 -7
- package/gs/builtin/map.ts +5 -1
- package/gs/builtin/slice.ts +152 -24
- 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 -2
- 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/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/maps/iter.gs.ts +0 -71
- package/gs/maps/maps.gs.ts +0 -87
- 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 {
|
|
@@ -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
|
|
|
@@ -282,14 +282,21 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
282
282
|
// Continue, we've already written part of the mapSet() function call
|
|
283
283
|
} else {
|
|
284
284
|
c.tsw.WriteLiterally(" ")
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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("&= ~(")
|
|
289
290
|
} else {
|
|
290
|
-
|
|
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(" ")
|
|
291
299
|
}
|
|
292
|
-
c.tsw.WriteLiterally(" ")
|
|
293
300
|
}
|
|
294
301
|
|
|
295
302
|
// Write RHS
|
|
@@ -353,6 +360,11 @@ func (c *GoToTSCompiler) writeAssignmentCore(lhs, rhs []ast.Expr, tok token.Toke
|
|
|
353
360
|
}
|
|
354
361
|
}
|
|
355
362
|
|
|
363
|
+
// Close the parenthesis for &^= transformation
|
|
364
|
+
if tok == token.AND_NOT_ASSIGN && !(isMapIndexLHS && len(lhs) == 1) {
|
|
365
|
+
c.tsw.WriteLiterally(")")
|
|
366
|
+
}
|
|
367
|
+
|
|
356
368
|
// If the LHS was a single map index, close the mapSet call
|
|
357
369
|
if isMapIndexLHS && len(lhs) == 1 {
|
|
358
370
|
c.tsw.WriteLiterally(")")
|
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,104 +899,9 @@ func (c *GoToTSCompiler) WriteDoc(doc *ast.CommentGroup) {
|
|
|
891
899
|
}
|
|
892
900
|
}
|
|
893
901
|
|
|
894
|
-
// sanitizeIdentifier
|
|
895
|
-
// or conflicts with built-in types, and transforms it if needed. This prevents
|
|
896
|
-
// compilation errors when Go identifiers conflict with JS/TS keywords or built-ins.
|
|
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
|
-
// Handle TypeScript built-in types that conflict with Go type parameter names
|
|
904
|
-
builtinTypes := map[string]string{
|
|
905
|
-
"Map": "MapType",
|
|
906
|
-
// Add other built-in types as needed
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
if replacement, exists := builtinTypes[name]; exists {
|
|
910
|
-
return replacement
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// List of JavaScript/TypeScript reserved words that could conflict
|
|
914
|
-
reservedWords := map[string]bool{
|
|
915
|
-
"abstract": true,
|
|
916
|
-
"any": true,
|
|
917
|
-
"as": true,
|
|
918
|
-
"asserts": true,
|
|
919
|
-
"async": true,
|
|
920
|
-
"await": true,
|
|
921
|
-
"boolean": true,
|
|
922
|
-
"break": true,
|
|
923
|
-
"case": true,
|
|
924
|
-
"catch": true,
|
|
925
|
-
"class": true,
|
|
926
|
-
"const": true,
|
|
927
|
-
"constructor": true,
|
|
928
|
-
"continue": true,
|
|
929
|
-
"debugger": true,
|
|
930
|
-
"declare": true,
|
|
931
|
-
"default": true,
|
|
932
|
-
"delete": true,
|
|
933
|
-
"do": true,
|
|
934
|
-
"else": true,
|
|
935
|
-
"enum": true,
|
|
936
|
-
"export": true,
|
|
937
|
-
"extends": true,
|
|
938
|
-
"finally": true,
|
|
939
|
-
"for": true,
|
|
940
|
-
"from": true,
|
|
941
|
-
"function": true,
|
|
942
|
-
"get": true,
|
|
943
|
-
"if": true,
|
|
944
|
-
"implements": true,
|
|
945
|
-
"import": true,
|
|
946
|
-
"in": true,
|
|
947
|
-
"instanceof": true,
|
|
948
|
-
"interface": true,
|
|
949
|
-
"is": true,
|
|
950
|
-
"keyof": true,
|
|
951
|
-
"let": true,
|
|
952
|
-
"module": true,
|
|
953
|
-
"namespace": true,
|
|
954
|
-
"never": true,
|
|
955
|
-
"new": true,
|
|
956
|
-
"null": true,
|
|
957
|
-
"number": true,
|
|
958
|
-
"object": true,
|
|
959
|
-
"of": true,
|
|
960
|
-
"package": true,
|
|
961
|
-
"private": true,
|
|
962
|
-
"protected": true,
|
|
963
|
-
"public": true,
|
|
964
|
-
"readonly": true,
|
|
965
|
-
"require": true,
|
|
966
|
-
"return": true,
|
|
967
|
-
"set": true,
|
|
968
|
-
"static": true,
|
|
969
|
-
"string": true,
|
|
970
|
-
"super": true,
|
|
971
|
-
"switch": true,
|
|
972
|
-
"symbol": true,
|
|
973
|
-
"this": true,
|
|
974
|
-
"throw": true,
|
|
975
|
-
"try": true,
|
|
976
|
-
"type": true,
|
|
977
|
-
"typeof": true,
|
|
978
|
-
"undefined": true,
|
|
979
|
-
"unique": true,
|
|
980
|
-
"unknown": true,
|
|
981
|
-
"var": true,
|
|
982
|
-
"void": true,
|
|
983
|
-
"while": true,
|
|
984
|
-
"with": true,
|
|
985
|
-
"yield": true,
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
if reservedWords[name] {
|
|
989
|
-
return "_" + name
|
|
990
|
-
}
|
|
991
|
-
return name
|
|
904
|
+
return sanitizeIdentifier(name)
|
|
992
905
|
}
|
|
993
906
|
|
|
994
907
|
// writeConstantValue writes the evaluated value of a Go constant to TypeScript.
|
|
@@ -1006,7 +919,10 @@ func (c *GoToTSCompiler) writeConstantValue(constObj *types.Const) {
|
|
|
1006
919
|
c.tsw.WriteLiterally(val.String())
|
|
1007
920
|
case constant.String:
|
|
1008
921
|
// For string constants, write as a quoted string literal
|
|
1009
|
-
|
|
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
|
|
1010
926
|
case constant.Bool:
|
|
1011
927
|
// For boolean constants, write true/false
|
|
1012
928
|
if constant.BoolVal(val) {
|
|
@@ -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))
|