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