goscript 0.0.18 → 0.0.19
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/compiler.go +94 -25
- package/compiler/compiler_test.go +1 -1
- package/package.json +1 -1
package/compiler/compiler.go
CHANGED
|
@@ -628,6 +628,18 @@ func (c *GoToTSCompiler) WriteZeroValueForType(typ any) {
|
|
|
628
628
|
default:
|
|
629
629
|
c.tsw.WriteLiterally("0")
|
|
630
630
|
}
|
|
631
|
+
case *types.Named:
|
|
632
|
+
// Handle named types, especially struct types
|
|
633
|
+
if _, isStruct := t.Underlying().(*types.Struct); isStruct {
|
|
634
|
+
// Initialize struct types with a new instance
|
|
635
|
+
c.tsw.WriteLiterallyf("new %s()", t.Obj().Name())
|
|
636
|
+
return
|
|
637
|
+
}
|
|
638
|
+
// For other named types, use the zero value of the underlying type
|
|
639
|
+
c.WriteZeroValueForType(t.Underlying())
|
|
640
|
+
case *types.Struct:
|
|
641
|
+
// For anonymous struct types, initialize with {}
|
|
642
|
+
c.tsw.WriteLiterally("{}")
|
|
631
643
|
default:
|
|
632
644
|
c.tsw.WriteLiterally("null")
|
|
633
645
|
}
|
|
@@ -1492,12 +1504,12 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
1492
1504
|
if _, isFuncType := typeName.Type().Underlying().(*types.Signature); isFuncType {
|
|
1493
1505
|
// For function types, we need to add a __goTypeName property
|
|
1494
1506
|
c.tsw.WriteLiterally("Object.assign(")
|
|
1495
|
-
|
|
1507
|
+
|
|
1496
1508
|
// Write the argument first
|
|
1497
1509
|
if err := c.WriteValueExpr(exp.Args[0]); err != nil {
|
|
1498
1510
|
return fmt.Errorf("failed to write argument for function type cast: %w", err)
|
|
1499
1511
|
}
|
|
1500
|
-
|
|
1512
|
+
|
|
1501
1513
|
// Add the __goTypeName property with the function type name
|
|
1502
1514
|
c.tsw.WriteLiterallyf(", { __goTypeName: '%s' })", funIdent.String())
|
|
1503
1515
|
return nil // Handled function type cast
|
|
@@ -1507,7 +1519,7 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
1507
1519
|
if err := c.WriteValueExpr(exp.Args[0]); err != nil {
|
|
1508
1520
|
return fmt.Errorf("failed to write argument for type cast: %w", err)
|
|
1509
1521
|
}
|
|
1510
|
-
|
|
1522
|
+
|
|
1511
1523
|
// Then use the TypeScript "as" operator with the type name
|
|
1512
1524
|
c.tsw.WriteLiterallyf(" as %s)", funIdent.String())
|
|
1513
1525
|
return nil // Handled non-function type cast
|
|
@@ -4600,8 +4612,61 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
|
|
|
4600
4612
|
}
|
|
4601
4613
|
}
|
|
4602
4614
|
|
|
4603
|
-
|
|
4604
|
-
//
|
|
4615
|
+
|
|
4616
|
+
// First, collect all the selector expressions to identify variables that need to be initialized
|
|
4617
|
+
hasSelectors := false
|
|
4618
|
+
for _, lhsExpr := range lhs {
|
|
4619
|
+
if _, ok := lhsExpr.(*ast.SelectorExpr); ok {
|
|
4620
|
+
hasSelectors = true
|
|
4621
|
+
break
|
|
4622
|
+
}
|
|
4623
|
+
}
|
|
4624
|
+
|
|
4625
|
+
// If we have selector expressions, we need to ensure variables are initialized
|
|
4626
|
+
// before the destructuring assignment
|
|
4627
|
+
if hasSelectors {
|
|
4628
|
+
c.tsw.WriteLiterally("{")
|
|
4629
|
+
c.tsw.WriteLine("")
|
|
4630
|
+
|
|
4631
|
+
// Write a temporary variable to hold the function call result
|
|
4632
|
+
c.tsw.WriteLiterally(" const _tmp = ")
|
|
4633
|
+
if err := c.WriteValueExpr(callExpr); err != nil {
|
|
4634
|
+
return fmt.Errorf("failed to write RHS call expression in assignment: %w", err)
|
|
4635
|
+
}
|
|
4636
|
+
c.tsw.WriteLine("")
|
|
4637
|
+
|
|
4638
|
+
for i, lhsExpr := range lhs {
|
|
4639
|
+
// Skip underscore variables
|
|
4640
|
+
if ident, ok := lhsExpr.(*ast.Ident); ok && ident.Name == "_" {
|
|
4641
|
+
continue
|
|
4642
|
+
}
|
|
4643
|
+
|
|
4644
|
+
// Write the LHS with indentation
|
|
4645
|
+
c.tsw.WriteLiterally(" ")
|
|
4646
|
+
if ident, ok := lhsExpr.(*ast.Ident); ok {
|
|
4647
|
+
c.WriteIdent(ident, false)
|
|
4648
|
+
} else if selectorExpr, ok := lhsExpr.(*ast.SelectorExpr); ok {
|
|
4649
|
+
if err := c.WriteValueExpr(selectorExpr); err != nil {
|
|
4650
|
+
return fmt.Errorf("failed to write selector expression in LHS: %w", err)
|
|
4651
|
+
}
|
|
4652
|
+
} else {
|
|
4653
|
+
return errors.Errorf("unhandled LHS expression in assignment: %T", lhsExpr)
|
|
4654
|
+
}
|
|
4655
|
+
|
|
4656
|
+
// Write the assignment
|
|
4657
|
+
c.tsw.WriteLiterallyf(" = _tmp[%d]", i)
|
|
4658
|
+
// Always add a newline after each assignment
|
|
4659
|
+
c.tsw.WriteLine("")
|
|
4660
|
+
}
|
|
4661
|
+
|
|
4662
|
+
// Close the block scope
|
|
4663
|
+
c.tsw.WriteLiterally("}")
|
|
4664
|
+
c.tsw.WriteLine("")
|
|
4665
|
+
|
|
4666
|
+
return nil
|
|
4667
|
+
}
|
|
4668
|
+
|
|
4669
|
+
// For simple cases without selector expressions, use array destructuring
|
|
4605
4670
|
c.tsw.WriteLiterally("[")
|
|
4606
4671
|
|
|
4607
4672
|
for i, lhsExpr := range lhs {
|
|
@@ -4610,11 +4675,15 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
|
|
|
4610
4675
|
}
|
|
4611
4676
|
|
|
4612
4677
|
if ident, ok := lhsExpr.(*ast.Ident); ok {
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
} else {
|
|
4678
|
+
// For underscore variables, use empty slots in destructuring pattern
|
|
4679
|
+
if ident.Name != "_" {
|
|
4616
4680
|
c.WriteIdent(ident, false)
|
|
4617
4681
|
}
|
|
4682
|
+
} else if selectorExpr, ok := lhsExpr.(*ast.SelectorExpr); ok {
|
|
4683
|
+
// Handle selector expressions (e.g., a.b) by using WriteValueExpr
|
|
4684
|
+
if err := c.WriteValueExpr(selectorExpr); err != nil {
|
|
4685
|
+
return fmt.Errorf("failed to write selector expression in LHS: %w", err)
|
|
4686
|
+
}
|
|
4618
4687
|
} else {
|
|
4619
4688
|
// Should not happen for valid Go code in this context, but handle defensively
|
|
4620
4689
|
return errors.Errorf("unhandled LHS expression in destructuring: %T", lhsExpr)
|
|
@@ -5690,14 +5759,14 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5690
5759
|
// For function types, create a type descriptor object with params and results
|
|
5691
5760
|
c.tsw.WriteLiterally("{")
|
|
5692
5761
|
c.tsw.WriteLiterally("kind: $.TypeKind.Function")
|
|
5693
|
-
|
|
5762
|
+
|
|
5694
5763
|
// Add name if this is a named function type
|
|
5695
5764
|
if namedType := c.pkg.TypesInfo.TypeOf(typeExpr); namedType != nil {
|
|
5696
5765
|
if named, ok := namedType.(*types.Named); ok {
|
|
5697
5766
|
c.tsw.WriteLiterallyf(", name: '%s'", named.Obj().Name())
|
|
5698
5767
|
}
|
|
5699
5768
|
}
|
|
5700
|
-
|
|
5769
|
+
|
|
5701
5770
|
// Add params if present
|
|
5702
5771
|
if typeExpr.Params != nil && len(typeExpr.Params.List) > 0 {
|
|
5703
5772
|
c.tsw.WriteLiterally(", params: [")
|
|
@@ -5709,7 +5778,7 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5709
5778
|
}
|
|
5710
5779
|
c.tsw.WriteLiterally("]")
|
|
5711
5780
|
}
|
|
5712
|
-
|
|
5781
|
+
|
|
5713
5782
|
// Add results if present
|
|
5714
5783
|
if typeExpr.Results != nil && len(typeExpr.Results.List) > 0 {
|
|
5715
5784
|
c.tsw.WriteLiterally(", results: [")
|
|
@@ -5721,7 +5790,7 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5721
5790
|
}
|
|
5722
5791
|
c.tsw.WriteLiterally("]")
|
|
5723
5792
|
}
|
|
5724
|
-
|
|
5793
|
+
|
|
5725
5794
|
c.tsw.WriteLiterally("}")
|
|
5726
5795
|
case *ast.Ident:
|
|
5727
5796
|
if isPrimitiveType(typeExpr.Name) {
|
|
@@ -5747,10 +5816,10 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5747
5816
|
c.tsw.WriteLiterally("{")
|
|
5748
5817
|
c.tsw.WriteLiterally("kind: $.TypeKind.Function")
|
|
5749
5818
|
c.tsw.WriteLiterallyf(", name: '%s'", typeExpr.Name)
|
|
5750
|
-
|
|
5819
|
+
|
|
5751
5820
|
// Get the underlying signature
|
|
5752
5821
|
signature := named.Underlying().(*types.Signature)
|
|
5753
|
-
|
|
5822
|
+
|
|
5754
5823
|
// Add params if present
|
|
5755
5824
|
params := signature.Params()
|
|
5756
5825
|
if params != nil && params.Len() > 0 {
|
|
@@ -5763,19 +5832,19 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5763
5832
|
param := params.At(i)
|
|
5764
5833
|
c.tsw.WriteLiterally("{")
|
|
5765
5834
|
c.tsw.WriteLiterally("kind: $.TypeKind.Basic, ")
|
|
5766
|
-
|
|
5835
|
+
|
|
5767
5836
|
typeName := param.Type().String()
|
|
5768
5837
|
if tsType, ok := GoBuiltinToTypescript(typeName); ok {
|
|
5769
5838
|
c.tsw.WriteLiterallyf("name: '%s'", tsType)
|
|
5770
5839
|
} else {
|
|
5771
5840
|
c.tsw.WriteLiterallyf("name: '%s'", typeName)
|
|
5772
5841
|
}
|
|
5773
|
-
|
|
5842
|
+
|
|
5774
5843
|
c.tsw.WriteLiterally("}")
|
|
5775
5844
|
}
|
|
5776
5845
|
c.tsw.WriteLiterally("]")
|
|
5777
5846
|
}
|
|
5778
|
-
|
|
5847
|
+
|
|
5779
5848
|
// Add results if present
|
|
5780
5849
|
results := signature.Results()
|
|
5781
5850
|
if results != nil && results.Len() > 0 {
|
|
@@ -5787,24 +5856,24 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
|
|
|
5787
5856
|
result := results.At(i)
|
|
5788
5857
|
c.tsw.WriteLiterally("{")
|
|
5789
5858
|
c.tsw.WriteLiterally("kind: $.TypeKind.Basic, ")
|
|
5790
|
-
|
|
5859
|
+
|
|
5791
5860
|
typeName := result.Type().String()
|
|
5792
5861
|
if tsType, ok := GoBuiltinToTypescript(typeName); ok {
|
|
5793
5862
|
c.tsw.WriteLiterallyf("name: '%s'", tsType)
|
|
5794
5863
|
} else {
|
|
5795
5864
|
c.tsw.WriteLiterallyf("name: '%s'", typeName)
|
|
5796
5865
|
}
|
|
5797
|
-
|
|
5866
|
+
|
|
5798
5867
|
c.tsw.WriteLiterally("}")
|
|
5799
5868
|
}
|
|
5800
5869
|
c.tsw.WriteLiterally("]")
|
|
5801
5870
|
}
|
|
5802
|
-
|
|
5871
|
+
|
|
5803
5872
|
c.tsw.WriteLiterally("}")
|
|
5804
5873
|
}
|
|
5805
5874
|
}
|
|
5806
5875
|
}
|
|
5807
|
-
|
|
5876
|
+
|
|
5808
5877
|
// Default case for non-function named types
|
|
5809
5878
|
if !isFunctionType {
|
|
5810
5879
|
c.tsw.WriteLiterallyf("'%s'", typeExpr.Name)
|
|
@@ -5924,14 +5993,14 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
|
|
|
5924
5993
|
// For function types, create a type descriptor object with params and results
|
|
5925
5994
|
c.tsw.WriteLiterally("{")
|
|
5926
5995
|
c.tsw.WriteLiterally("kind: $.TypeKind.Function")
|
|
5927
|
-
|
|
5996
|
+
|
|
5928
5997
|
// Add name if this is a named function type
|
|
5929
5998
|
if namedType := c.pkg.TypesInfo.TypeOf(typeExpr); namedType != nil {
|
|
5930
5999
|
if named, ok := namedType.(*types.Named); ok {
|
|
5931
6000
|
c.tsw.WriteLiterallyf(", name: '%s'", named.Obj().Name())
|
|
5932
6001
|
}
|
|
5933
6002
|
}
|
|
5934
|
-
|
|
6003
|
+
|
|
5935
6004
|
// Add params if present
|
|
5936
6005
|
if t.Params != nil && len(t.Params.List) > 0 {
|
|
5937
6006
|
c.tsw.WriteLiterally(", params: [")
|
|
@@ -5943,7 +6012,7 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
|
|
|
5943
6012
|
}
|
|
5944
6013
|
c.tsw.WriteLiterally("]")
|
|
5945
6014
|
}
|
|
5946
|
-
|
|
6015
|
+
|
|
5947
6016
|
// Add results if present
|
|
5948
6017
|
if t.Results != nil && len(t.Results.List) > 0 {
|
|
5949
6018
|
c.tsw.WriteLiterally(", results: [")
|
|
@@ -5955,7 +6024,7 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
|
|
|
5955
6024
|
}
|
|
5956
6025
|
c.tsw.WriteLiterally("]")
|
|
5957
6026
|
}
|
|
5958
|
-
|
|
6027
|
+
|
|
5959
6028
|
c.tsw.WriteLiterally("}")
|
|
5960
6029
|
return
|
|
5961
6030
|
case *ast.ChanType:
|