goscript 0.0.16 → 0.0.17

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.
@@ -320,7 +320,7 @@ func (c *GoToTSCompiler) WriteGoType(typ types.Type) {
320
320
  default:
321
321
  // For other types, just write "any" and add a comment
322
322
  c.tsw.WriteLiterally("any")
323
- c.tsw.WriteCommentInline(fmt.Sprintf("unhandled type: %T", typ))
323
+ c.tsw.WriteCommentInlinef("unhandled type: %T", typ)
324
324
  }
325
325
  }
326
326
 
@@ -452,7 +452,7 @@ func (c *GoToTSCompiler) WriteSignatureType(t *types.Signature) {
452
452
  if param.Name() != "" {
453
453
  c.tsw.WriteLiterally(param.Name())
454
454
  } else {
455
- c.tsw.WriteLiterally(fmt.Sprintf("p%d", i))
455
+ c.tsw.WriteLiterallyf("p%d", i)
456
456
  }
457
457
  c.tsw.WriteLiterally(": ")
458
458
 
@@ -763,7 +763,7 @@ func (c *GoToTSCompiler) WriteValueExpr(a ast.Expr) error {
763
763
  case *ast.FuncLit:
764
764
  return c.WriteFuncLitValue(exp)
765
765
  default:
766
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled value expr: %T", exp))
766
+ c.tsw.WriteCommentLinef("unhandled value expr: %T", exp)
767
767
  return nil
768
768
  }
769
769
  }
@@ -774,19 +774,19 @@ func (c *GoToTSCompiler) WriteIndexExpr(exp *ast.IndexExpr) error {
774
774
  if tv, ok := c.pkg.TypesInfo.Types[exp.X]; ok {
775
775
  // Check if it's a map type
776
776
  if mapType, isMap := tv.Type.Underlying().(*types.Map); isMap {
777
+ c.tsw.WriteLiterally("$.mapGet(")
777
778
  if err := c.WriteValueExpr(exp.X); err != nil {
778
779
  return err
779
780
  }
780
- c.tsw.WriteLiterally(".get(")
781
+ c.tsw.WriteLiterally(", ")
781
782
  if err := c.WriteValueExpr(exp.Index); err != nil {
782
783
  return err
783
784
  }
784
- // Map.get() returns undefined when key not found, but Go returns zero value
785
- // Add nullish coalescing with the appropriate zero value for the map's value type
786
- c.tsw.WriteLiterally(") ?? ")
785
+ c.tsw.WriteLiterally(", ")
787
786
 
788
- // Generate the zero value based on the map's value type
787
+ // Generate the zero value as the default value for mapGet
789
788
  c.WriteZeroValueForType(mapType.Elem())
789
+ c.tsw.WriteLiterally(")")
790
790
  return nil
791
791
  }
792
792
  }
@@ -821,7 +821,7 @@ func (c *GoToTSCompiler) WriteTypeAssertExpr(exp *ast.TypeAssertExpr) error {
821
821
  return fmt.Errorf("failed to write interface expression in type assertion expression: %w", err)
822
822
  }
823
823
  c.tsw.WriteLiterally(", ")
824
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", typeName))
824
+ c.tsw.WriteLiterallyf("'%s'", typeName)
825
825
  c.tsw.WriteLiterally(").value") // Access the value field directly in expression context
826
826
  return nil
827
827
  }
@@ -1709,10 +1709,10 @@ func (c *GoToTSCompiler) WriteBasicLit(exp *ast.BasicLit) {
1709
1709
  // Use strconv.UnquoteChar to handle escape sequences correctly.
1710
1710
  val, _, _, err := strconv.UnquoteChar(exp.Value[1:len(exp.Value)-1], '\'')
1711
1711
  if err != nil {
1712
- c.tsw.WriteCommentInline(fmt.Sprintf("error parsing char literal %s: %v", exp.Value, err))
1712
+ c.tsw.WriteCommentInlinef("error parsing char literal %s: %v", exp.Value, err)
1713
1713
  c.tsw.WriteLiterally("0") // Default to 0 on error
1714
1714
  } else {
1715
- c.tsw.WriteLiterally(fmt.Sprintf("%d", val))
1715
+ c.tsw.WriteLiterallyf("%d", val)
1716
1716
  }
1717
1717
  } else {
1718
1718
  // Other literals (INT, FLOAT, STRING, IMAG)
@@ -2362,7 +2362,7 @@ func (c *GoToTSCompiler) collectMethodNames(structName string) string {
2362
2362
  // Check if the receiver identifier name matches the struct name
2363
2363
  if ident, ok := recvType.(*ast.Ident); ok && ident.Name == structName {
2364
2364
  // Found a method for this struct
2365
- methodNames = append(methodNames, fmt.Sprintf("'%s'", funcDecl.Name.Name))
2365
+ methodNames = append(methodNames, strconv.Quote(funcDecl.Name.Name))
2366
2366
  }
2367
2367
  }
2368
2368
  }
@@ -3120,7 +3120,7 @@ func (c *GoToTSCompiler) WriteField(field *ast.Field, isArguments bool) {
3120
3120
  if !isArguments {
3121
3121
  // write tag comment if any for struct fields
3122
3122
  if field.Tag != nil {
3123
- c.tsw.WriteCommentLine(fmt.Sprintf(" // tag: %s", field.Tag.Value))
3123
+ c.tsw.WriteCommentLinef("tag: %s", field.Tag.Value)
3124
3124
  } else {
3125
3125
  c.tsw.WriteLine("") // No semicolon
3126
3126
  }
@@ -3199,7 +3199,7 @@ func (c *GoToTSCompiler) WriteStmt(a ast.Stmt) error {
3199
3199
  return fmt.Errorf("failed to write value spec in declaration statement: %w", err)
3200
3200
  }
3201
3201
  } else {
3202
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled spec in DeclStmt: %T", spec))
3202
+ c.tsw.WriteCommentLinef("unhandled spec in DeclStmt: %T", spec)
3203
3203
  }
3204
3204
  }
3205
3205
  } else {
@@ -3278,7 +3278,7 @@ func (c *GoToTSCompiler) WriteStmt(a ast.Stmt) error {
3278
3278
  case token.CONTINUE:
3279
3279
  c.tsw.WriteLine("continue") // No semicolon needed
3280
3280
  default:
3281
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled branch statement token: %s", exp.Tok.String()))
3281
+ c.tsw.WriteCommentLinef("unhandled branch statement token: %s", exp.Tok.String())
3282
3282
  }
3283
3283
  default:
3284
3284
  return errors.Errorf("unknown statement: %s\n", a)
@@ -3316,7 +3316,7 @@ func (c *GoToTSCompiler) WriteStmtDefer(exp *ast.DeferStmt) error {
3316
3316
 
3317
3317
  // Set stack variable based on whether we are in an async function
3318
3318
  stackVar := "__defer"
3319
- c.tsw.WriteLiterally(fmt.Sprintf("%s.defer(%s() => {", stackVar, asyncPrefix))
3319
+ c.tsw.WriteLiterallyf("%s.defer(%s() => {", stackVar, asyncPrefix)
3320
3320
  c.tsw.Indent(1)
3321
3321
  c.tsw.WriteLine("")
3322
3322
 
@@ -3418,7 +3418,7 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
3418
3418
  c.tsw.WriteLiterally("{") // Start object literal
3419
3419
  c.tsw.Indent(1)
3420
3420
  c.tsw.WriteLine("")
3421
- c.tsw.WriteLiterally(fmt.Sprintf("id: %d,", caseID))
3421
+ c.tsw.WriteLiterallyf("id: %d,", caseID)
3422
3422
  c.tsw.WriteLine("")
3423
3423
 
3424
3424
  // Handle different types of comm statements
@@ -3437,10 +3437,10 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
3437
3437
  c.tsw.WriteLiterally(",")
3438
3438
  c.tsw.WriteLine("")
3439
3439
  } else {
3440
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled RHS in select assignment case: %T", comm.Rhs[0]))
3440
+ c.tsw.WriteCommentLinef("unhandled RHS in select assignment case: %T", comm.Rhs[0])
3441
3441
  }
3442
3442
  } else {
3443
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled RHS count in select assignment case: %d", len(comm.Rhs)))
3443
+ c.tsw.WriteCommentLinef("unhandled RHS count in select assignment case: %d", len(comm.Rhs))
3444
3444
  }
3445
3445
  case *ast.ExprStmt:
3446
3446
  // This is a simple receive: case <-ch:
@@ -3454,7 +3454,7 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
3454
3454
  c.tsw.WriteLiterally(",")
3455
3455
  c.tsw.WriteLine("")
3456
3456
  } else {
3457
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled expression in select case: %T", comm.X))
3457
+ c.tsw.WriteCommentLinef("unhandled expression in select case: %T", comm.X)
3458
3458
  }
3459
3459
  case *ast.SendStmt:
3460
3460
  // This is a send operation: case ch <- v:
@@ -3473,7 +3473,7 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
3473
3473
  c.tsw.WriteLiterally(",")
3474
3474
  c.tsw.WriteLine("")
3475
3475
  default:
3476
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled comm statement in select case: %T", comm))
3476
+ c.tsw.WriteCommentLinef("unhandled comm statement in select case: %T", comm)
3477
3477
  }
3478
3478
 
3479
3479
  // Add the onSelected handler to execute the case body after the select resolves
@@ -3530,14 +3530,14 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
3530
3530
  c.tsw.WriteLine("")
3531
3531
 
3532
3532
  } else {
3533
- c.tsw.WriteCommentLine(fmt.Sprintf("unknown statement in select body: %T", stmt))
3533
+ c.tsw.WriteCommentLinef("unknown statement in select body: %T", stmt)
3534
3534
  }
3535
3535
  }
3536
3536
 
3537
3537
  // Close the array literal and the selectStatement call
3538
3538
  c.tsw.Indent(-1)
3539
3539
  c.tsw.WriteLiterally("], ")
3540
- c.tsw.WriteLiterally(fmt.Sprintf("%t", hasDefault))
3540
+ c.tsw.WriteLiterallyf("%t", hasDefault)
3541
3541
  c.tsw.WriteLiterally(")")
3542
3542
  c.tsw.WriteLine("")
3543
3543
 
@@ -3591,7 +3591,7 @@ func (c *GoToTSCompiler) WriteStmtSwitch(exp *ast.SwitchStmt) error {
3591
3591
  return fmt.Errorf("failed to write case clause in switch statement: %w", err)
3592
3592
  }
3593
3593
  } else {
3594
- c.tsw.WriteCommentLine(fmt.Sprintf("unhandled statement in switch body: %T", stmt))
3594
+ c.tsw.WriteCommentLinef("unhandled statement in switch body: %T", stmt)
3595
3595
  }
3596
3596
  }
3597
3597
 
@@ -4547,10 +4547,11 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
4547
4547
  if !existsIsBlank {
4548
4548
  c.tsw.WriteLiterally(existsName)
4549
4549
  c.tsw.WriteLiterally(" = ")
4550
+ c.tsw.WriteLiterally("$.mapHas(")
4550
4551
  if err := c.WriteValueExpr(indexExpr.X); err != nil { // Map
4551
4552
  return err
4552
4553
  }
4553
- c.tsw.WriteLiterally(".has(")
4554
+ c.tsw.WriteLiterally(", ")
4554
4555
  if err := c.WriteValueExpr(indexExpr.Index); err != nil { // Key
4555
4556
  return err
4556
4557
  }
@@ -4562,14 +4563,15 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
4562
4563
  if !valueIsBlank {
4563
4564
  c.tsw.WriteLiterally(valueName)
4564
4565
  c.tsw.WriteLiterally(" = ")
4566
+ c.tsw.WriteLiterally("$.mapGet(")
4565
4567
  if err := c.WriteValueExpr(indexExpr.X); err != nil { // Map
4566
4568
  return err
4567
4569
  }
4568
- c.tsw.WriteLiterally(".get(")
4570
+ c.tsw.WriteLiterally(", ")
4569
4571
  if err := c.WriteValueExpr(indexExpr.Index); err != nil { // Key
4570
4572
  return err
4571
4573
  }
4572
- c.tsw.WriteLiterally(") ?? ")
4574
+ c.tsw.WriteLiterally(", ")
4573
4575
  // Write the zero value for the map's value type
4574
4576
  if tv, ok := c.pkg.TypesInfo.Types[indexExpr.X]; ok {
4575
4577
  if mapType, isMap := tv.Type.Underlying().(*types.Map); isMap {
@@ -4581,27 +4583,30 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
4581
4583
  } else {
4582
4584
  c.tsw.WriteLiterally("null")
4583
4585
  }
4586
+ c.tsw.WriteLiterally(")")
4584
4587
  c.tsw.WriteLine("")
4585
4588
  } else if existsIsBlank {
4586
4589
  // If both are blank, still evaluate for side effects (though .has/.get are usually pure)
4587
4590
  // We add a ; otherwise TypeScript thinks we are invoking a function.
4588
- c.tsw.WriteLiterally(";(") // Wrap in parens to make it an expression statement
4591
+ c.tsw.WriteLiterally(";(") // Wrap in parens to make it an expression statement
4592
+ c.tsw.WriteLiterally("$.mapHas(")
4589
4593
  if err := c.WriteValueExpr(indexExpr.X); err != nil { // Map
4590
4594
  return err
4591
4595
  }
4592
- c.tsw.WriteLiterally(".has(")
4596
+ c.tsw.WriteLiterally(", ")
4593
4597
  if err := c.WriteValueExpr(indexExpr.Index); err != nil { // Key
4594
4598
  return err
4595
4599
  }
4596
- c.tsw.WriteLiterally("), ") // Evaluate .has
4600
+ c.tsw.WriteLiterally("), ") // Evaluate .has
4601
+ c.tsw.WriteLiterally("$.mapGet(")
4597
4602
  if err := c.WriteValueExpr(indexExpr.X); err != nil { // Map
4598
4603
  return err
4599
4604
  }
4600
- c.tsw.WriteLiterally(".get(")
4605
+ c.tsw.WriteLiterally(", ")
4601
4606
  if err := c.WriteValueExpr(indexExpr.Index); err != nil { // Key
4602
4607
  return err
4603
4608
  }
4604
- c.tsw.WriteLiterally("))") // Evaluate .get
4609
+ c.tsw.WriteLiterally(", null))") // Evaluate .get with null as default
4605
4610
  c.tsw.WriteLine("")
4606
4611
  }
4607
4612
 
@@ -5072,7 +5077,7 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5072
5077
  indexVarName = keyIdent.Name
5073
5078
  }
5074
5079
  }
5075
- c.tsw.WriteLiterally(fmt.Sprintf("for (let %s = 0; %s < _runes.length; %s++) {", indexVarName, indexVarName, indexVarName))
5080
+ c.tsw.WriteLiterallyf("for (let %s = 0; %s < _runes.length; %s++) {", indexVarName, indexVarName, indexVarName)
5076
5081
  c.tsw.Indent(1)
5077
5082
  c.tsw.WriteLine("")
5078
5083
  // Declare value if provided and not blank
@@ -5104,11 +5109,11 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5104
5109
  }
5105
5110
  }
5106
5111
 
5107
- c.tsw.WriteLiterally(fmt.Sprintf("for (let %s = 0; %s < ", indexVarName, indexVarName))
5112
+ c.tsw.WriteLiterallyf("for (let %s = 0; %s < ", indexVarName, indexVarName)
5108
5113
  if err := c.WriteValueExpr(exp.X); err != nil { // This is N
5109
5114
  return fmt.Errorf("failed to write range loop integer expression: %w", err)
5110
5115
  }
5111
- c.tsw.WriteLiterally(fmt.Sprintf("; %s++) {", indexVarName))
5116
+ c.tsw.WriteLiterallyf("; %s++) {", indexVarName)
5112
5117
  c.tsw.Indent(1)
5113
5118
  c.tsw.WriteLine("")
5114
5119
 
@@ -5139,11 +5144,11 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5139
5144
  }
5140
5145
  // If both key and value are provided, use an index loop and assign both
5141
5146
  if exp.Key != nil && exp.Value != nil {
5142
- c.tsw.WriteLiterally(fmt.Sprintf("for (let %s = 0; %s < ", indexVarName, indexVarName))
5147
+ c.tsw.WriteLiterallyf("for (let %s = 0; %s < $.len(", indexVarName, indexVarName)
5143
5148
  if err := c.WriteValueExpr(exp.X); err != nil { // Write the expression for the iterable
5144
5149
  return fmt.Errorf("failed to write range loop array/slice expression (key and value): %w", err)
5145
5150
  }
5146
- c.tsw.WriteLiterally(fmt.Sprintf(".length; %s++) {", indexVarName))
5151
+ c.tsw.WriteLiterallyf("); %s++) {", indexVarName)
5147
5152
  c.tsw.Indent(1)
5148
5153
  c.tsw.WriteLine("")
5149
5154
  // Declare value if not blank
@@ -5154,7 +5159,7 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5154
5159
  if err := c.WriteValueExpr(exp.X); err != nil {
5155
5160
  return fmt.Errorf("failed to write range loop array/slice value expression: %w", err)
5156
5161
  }
5157
- c.tsw.WriteLiterally(fmt.Sprintf("[%s]", indexVarName)) // Use indexVarName
5162
+ c.tsw.WriteLiterallyf("![%s]", indexVarName) // Use indexVarName with not-null assert
5158
5163
  c.tsw.WriteLine("")
5159
5164
  }
5160
5165
  if err := c.WriteStmt(exp.Body); err != nil {
@@ -5164,12 +5169,12 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5164
5169
  c.tsw.WriteLine("}")
5165
5170
  return nil
5166
5171
  } else if exp.Key != nil && exp.Value == nil { // Only key provided
5167
- c.tsw.WriteLiterally(fmt.Sprintf("for (let %s = 0; %s < ", indexVarName, indexVarName))
5172
+ c.tsw.WriteLiterallyf("for (let %s = 0; %s < $.len(", indexVarName, indexVarName)
5168
5173
  // Write the expression for the iterable
5169
5174
  if err := c.WriteValueExpr(exp.X); err != nil {
5170
5175
  return fmt.Errorf("failed to write expression for the iterable: %w", err)
5171
5176
  }
5172
- c.tsw.WriteLiterally(fmt.Sprintf(".length; %s++) {", indexVarName))
5177
+ c.tsw.WriteLiterallyf("); %s++) {", indexVarName)
5173
5178
  c.tsw.Indent(1)
5174
5179
  c.tsw.WriteLine("")
5175
5180
  if err := c.WriteStmtBlock(exp.Body, false); err != nil {
@@ -5184,11 +5189,11 @@ func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error {
5184
5189
  } else {
5185
5190
  // Fallback: simple index loop without declaring range variables, use _i
5186
5191
  indexVarName := "_i"
5187
- c.tsw.WriteLiterally(fmt.Sprintf("for (let %s = 0; %s < ", indexVarName, indexVarName))
5192
+ c.tsw.WriteLiterallyf("for (let %s = 0; %s < $.len(", indexVarName, indexVarName)
5188
5193
  if err := c.WriteValueExpr(exp.X); err != nil {
5189
5194
  return fmt.Errorf("failed to write range loop array/slice length expression (fallback): %w", err)
5190
5195
  }
5191
- c.tsw.WriteLiterally(fmt.Sprintf(".length; %s++) {", indexVarName))
5196
+ c.tsw.WriteLiterallyf("); %s++) {", indexVarName)
5192
5197
  c.tsw.Indent(1)
5193
5198
  c.tsw.WriteLine("")
5194
5199
  if err := c.WriteStmtBlock(exp.Body, false); err != nil {
@@ -5319,9 +5324,9 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5319
5324
  c.tsw.WriteLiterally("keyType: ")
5320
5325
  if ident, ok := typeExpr.Key.(*ast.Ident); ok && isPrimitiveType(ident.Name) {
5321
5326
  if tsType, ok := GoBuiltinToTypescript(ident.Name); ok {
5322
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", tsType))
5327
+ c.tsw.WriteLiterallyf("'%s'", tsType)
5323
5328
  } else {
5324
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", ident.Name)) // Fallback
5329
+ c.tsw.WriteLiterallyf("'%s'", ident.Name) // Fallback
5325
5330
  }
5326
5331
  } else {
5327
5332
  c.writeTypeDescription(typeExpr.Key)
@@ -5333,9 +5338,9 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5333
5338
  c.tsw.WriteLiterally("elemType: ")
5334
5339
  if ident, ok := typeExpr.Value.(*ast.Ident); ok && isPrimitiveType(ident.Name) {
5335
5340
  if tsType, ok := GoBuiltinToTypescript(ident.Name); ok {
5336
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", tsType))
5341
+ c.tsw.WriteLiterallyf("'%s'", tsType)
5337
5342
  } else {
5338
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", ident.Name)) // Fallback
5343
+ c.tsw.WriteLiterallyf("'%s'", ident.Name) // Fallback
5339
5344
  }
5340
5345
  } else {
5341
5346
  c.writeTypeDescription(typeExpr.Value)
@@ -5351,15 +5356,15 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5351
5356
 
5352
5357
  // Create a type descriptor object
5353
5358
  c.tsw.WriteLiterally("{")
5354
- c.tsw.WriteLiterally(fmt.Sprintf("kind: %s, ", typeKind))
5359
+ c.tsw.WriteLiterallyf("kind: %s, ", typeKind)
5355
5360
 
5356
5361
  // Add element type
5357
5362
  c.tsw.WriteLiterally("elemType: ")
5358
5363
  if ident, ok := typeExpr.Elt.(*ast.Ident); ok && isPrimitiveType(ident.Name) {
5359
5364
  if tsType, ok := GoBuiltinToTypescript(ident.Name); ok {
5360
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", tsType))
5365
+ c.tsw.WriteLiterallyf("'%s'", tsType)
5361
5366
  } else {
5362
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", ident.Name)) // Fallback
5367
+ c.tsw.WriteLiterallyf("'%s'", ident.Name) // Fallback
5363
5368
  }
5364
5369
  } else {
5365
5370
  c.writeTypeDescription(typeExpr.Elt)
@@ -5374,7 +5379,7 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5374
5379
  // Get the type name if available
5375
5380
  typeName := c.getTypeNameString(assertedType)
5376
5381
  if typeName != "unknown" {
5377
- c.tsw.WriteLiterally(fmt.Sprintf(", name: '%s'", typeName))
5382
+ c.tsw.WriteLiterallyf(", name: '%s'", typeName)
5378
5383
  }
5379
5384
 
5380
5385
  if typeExpr.Fields != nil && typeExpr.Fields.List != nil {
@@ -5402,7 +5407,7 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5402
5407
  // Get the type name if available
5403
5408
  typeName := c.getTypeNameString(assertedType)
5404
5409
  if typeName != "unknown" {
5405
- c.tsw.WriteLiterally(fmt.Sprintf(", name: '%s'", typeName))
5410
+ c.tsw.WriteLiterallyf(", name: '%s'", typeName)
5406
5411
  }
5407
5412
 
5408
5413
  // Add methods if available
@@ -5427,7 +5432,7 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5427
5432
 
5428
5433
  // Add element type if it's a named type
5429
5434
  if ident, ok := typeExpr.X.(*ast.Ident); ok {
5430
- c.tsw.WriteLiterally(fmt.Sprintf(", elemType: '%s'", ident.Name))
5435
+ c.tsw.WriteLiterallyf(", elemType: '%s'", ident.Name)
5431
5436
  }
5432
5437
 
5433
5438
  c.tsw.WriteLiterally("}")
@@ -5439,9 +5444,9 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5439
5444
  c.tsw.WriteLiterally(", elemType: ")
5440
5445
  if ident, ok := typeExpr.Value.(*ast.Ident); ok && isPrimitiveType(ident.Name) {
5441
5446
  if tsType, ok := GoBuiltinToTypescript(ident.Name); ok {
5442
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", tsType))
5447
+ c.tsw.WriteLiterallyf("'%s'", tsType)
5443
5448
  } else {
5444
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", ident.Name)) // Fallback
5449
+ c.tsw.WriteLiterallyf("'%s'", ident.Name) // Fallback
5445
5450
  }
5446
5451
  } else {
5447
5452
  c.writeTypeDescription(typeExpr.Value)
@@ -5460,26 +5465,26 @@ func (c *GoToTSCompiler) writeTypeAssertion(lhs []ast.Expr, typeAssertExpr *ast.
5460
5465
 
5461
5466
  // Use TypeScript equivalent if available
5462
5467
  if tsType, ok := GoBuiltinToTypescript(typeExpr.Name); ok {
5463
- c.tsw.WriteLiterally(fmt.Sprintf("name: '%s'", tsType))
5468
+ c.tsw.WriteLiterallyf("name: '%s'", tsType) // TODO: use %q?
5464
5469
  } else {
5465
- c.tsw.WriteLiterally(fmt.Sprintf("name: '%s'", typeExpr.Name))
5470
+ c.tsw.WriteLiterallyf("name: '%s'", typeExpr.Name)
5466
5471
  }
5467
5472
 
5468
5473
  c.tsw.WriteLiterally("}")
5469
5474
  } else {
5470
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", typeExpr.Name))
5475
+ c.tsw.WriteLiterallyf("'%s'", typeExpr.Name)
5471
5476
  }
5472
5477
  case *ast.SelectorExpr:
5473
5478
  // For imported types like pkg.Type
5474
5479
  if ident, ok := typeExpr.X.(*ast.Ident); ok {
5475
- c.tsw.WriteLiterally(fmt.Sprintf("'%s.%s'", ident.Name, typeExpr.Sel.Name))
5480
+ c.tsw.WriteLiterallyf("'%s.%s'", ident.Name, typeExpr.Sel.Name)
5476
5481
  } else {
5477
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", c.getTypeNameString(assertedType)))
5482
+ c.tsw.WriteLiterallyf("'%s'", c.getTypeNameString(assertedType))
5478
5483
  }
5479
5484
  default:
5480
5485
  // For other types, use the string name as before
5481
5486
  typeName := c.getTypeNameString(assertedType)
5482
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", typeName))
5487
+ c.tsw.WriteLiterallyf("'%s'", typeName)
5483
5488
  }
5484
5489
 
5485
5490
  c.tsw.WriteLiterally(")")
@@ -5506,22 +5511,22 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
5506
5511
  if tsType, ok := GoBuiltinToTypescript(t.Name); ok {
5507
5512
  c.tsw.WriteLiterally("{")
5508
5513
  c.tsw.WriteLiterally("kind: $.TypeKind.Basic, ")
5509
- c.tsw.WriteLiterally(fmt.Sprintf("name: '%s'", tsType))
5514
+ c.tsw.WriteLiterallyf("name: '%s'", tsType)
5510
5515
  c.tsw.WriteLiterally("}")
5511
5516
  } else {
5512
5517
  // Fallback for other primitive types
5513
5518
  c.tsw.WriteLiterally("{")
5514
5519
  c.tsw.WriteLiterally("kind: $.TypeKind.Basic, ")
5515
- c.tsw.WriteLiterally(fmt.Sprintf("name: '%s'", t.Name))
5520
+ c.tsw.WriteLiterallyf("name: '%s'", t.Name)
5516
5521
  c.tsw.WriteLiterally("}")
5517
5522
  }
5518
5523
  } else {
5519
5524
  // For named types, just use the name string
5520
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", t.Name))
5525
+ c.tsw.WriteLiterallyf("'%s'", t.Name)
5521
5526
  }
5522
5527
  case *ast.SelectorExpr:
5523
5528
  if ident, ok := t.X.(*ast.Ident); ok {
5524
- c.tsw.WriteLiterally(fmt.Sprintf("'%s.%s'", ident.Name, t.Sel.Name))
5529
+ c.tsw.WriteLiterallyf("'%s.%s'", ident.Name, t.Sel.Name)
5525
5530
  }
5526
5531
  case *ast.ArrayType:
5527
5532
  typeKind := "$.TypeKind.Slice"
@@ -5530,7 +5535,7 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
5530
5535
  }
5531
5536
 
5532
5537
  c.tsw.WriteLiterally("{")
5533
- c.tsw.WriteLiterally(fmt.Sprintf("kind: %s, ", typeKind))
5538
+ c.tsw.WriteLiterallyf("kind: %s, ", typeKind)
5534
5539
  c.tsw.WriteLiterally("elemType: ")
5535
5540
  c.writeTypeDescription(t.Elt)
5536
5541
  c.tsw.WriteLiterally("}")
@@ -5562,7 +5567,7 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
5562
5567
  if hasFields {
5563
5568
  c.tsw.WriteLiterally(", ")
5564
5569
  }
5565
- c.tsw.WriteLiterally(fmt.Sprintf("'%s': ", name.Name))
5570
+ c.tsw.WriteLiterallyf("'%s': ", name.Name)
5566
5571
  c.writeTypeDescription(field.Type)
5567
5572
  hasFields = true
5568
5573
  }
@@ -5594,7 +5599,7 @@ func (c *GoToTSCompiler) writeTypeDescription(typeExpr ast.Expr) {
5594
5599
  c.tsw.WriteLiterally("}")
5595
5600
  default:
5596
5601
  // For other types, use the string representation
5597
- c.tsw.WriteLiterally(fmt.Sprintf("'%s'", c.getTypeNameString(typeExpr)))
5602
+ c.tsw.WriteLiterallyf("'%s'", c.getTypeNameString(typeExpr))
5598
5603
  }
5599
5604
  }
5600
5605
 
@@ -65,6 +65,15 @@ func (w *TSCodeWriter) WriteCommentLine(commentText string) {
65
65
  }
66
66
  }
67
67
 
68
+ // WriteCommentLinef writes a formatted comment as a // line.
69
+ func (w *TSCodeWriter) WriteCommentLinef(format string, args ...any) {
70
+ commentText := fmt.Sprintf(format, args...)
71
+ lines := strings.SplitSeq(commentText, "\n")
72
+ for line := range lines {
73
+ w.WriteLinef("// %s", line)
74
+ }
75
+ }
76
+
68
77
  // WriteCommentInline write a comment within /* */.
69
78
  func (w *TSCodeWriter) WriteCommentInline(commentText string) {
70
79
  w.w.Write([]byte("/* ")) //nolint:errcheck
@@ -72,6 +81,11 @@ func (w *TSCodeWriter) WriteCommentInline(commentText string) {
72
81
  w.w.Write([]byte(" */")) //nolint:errcheck
73
82
  }
74
83
 
84
+ // WriteCommentInlinef writes a formatted comment within /* */.
85
+ func (w *TSCodeWriter) WriteCommentInlinef(format string, args ...any) {
86
+ w.WriteCommentInline(fmt.Sprintf(format, args...))
87
+ }
88
+
75
89
  // WriteLiterally writes something to the output without processing
76
90
  func (w *TSCodeWriter) WriteLiterally(literal string) {
77
91
  w.sectionWrittenFlag = true
package/go.mod CHANGED
@@ -1,8 +1,6 @@
1
1
  module github.com/aperturerobotics/goscript
2
2
 
3
- go 1.24
4
-
5
- toolchain go1.24.3
3
+ go 1.24.3
6
4
 
7
5
  require (
8
6
  github.com/aperturerobotics/cli v1.0.0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "goscript",
3
3
  "description": "Go to TypeScript transpiler",
4
- "version": "0.0.16",
4
+ "version": "0.0.17",
5
5
  "author": {
6
6
  "name": "Aperture Robotics LLC.",
7
7
  "email": "support@aperture.us",