goscript 0.0.39 → 0.0.41

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 (421) hide show
  1. package/compiler/analysis.go +15 -6
  2. package/compiler/compiler.go +185 -35
  3. package/compiler/expr-call.go +7 -9
  4. package/compiler/expr-type.go +25 -0
  5. package/compiler/field.go +21 -7
  6. package/compiler/gs_dependencies_test.go +80 -0
  7. package/compiler/lit.go +1 -6
  8. package/compiler/output.go +10 -4
  9. package/compiler/spec.go +16 -3
  10. package/compiler/type-assert.go +111 -21
  11. package/compiler/type.go +56 -12
  12. package/dist/gs/builtin/builtin.d.ts +20 -8
  13. package/dist/gs/builtin/builtin.js +30 -53
  14. package/dist/gs/builtin/builtin.js.map +1 -1
  15. package/dist/gs/builtin/errors.d.ts +8 -0
  16. package/dist/gs/builtin/errors.js +18 -0
  17. package/dist/gs/builtin/errors.js.map +1 -0
  18. package/dist/gs/builtin/index.d.ts +7 -0
  19. package/dist/gs/builtin/index.js +7 -0
  20. package/dist/gs/builtin/index.js.map +1 -1
  21. package/dist/gs/builtin/slice.d.ts +5 -2
  22. package/dist/gs/builtin/slice.js +123 -77
  23. package/dist/gs/builtin/slice.js.map +1 -1
  24. package/dist/gs/bytes/buffer.gs.d.ts +1 -1
  25. package/dist/gs/bytes/buffer.gs.js +1 -1
  26. package/dist/gs/bytes/buffer.gs.js.map +1 -1
  27. package/dist/gs/bytes/bytes.gs.d.ts +1 -1
  28. package/dist/gs/bytes/bytes.gs.js +111 -15
  29. package/dist/gs/bytes/bytes.gs.js.map +1 -1
  30. package/dist/gs/bytes/iter.gs.d.ts +1 -1
  31. package/dist/gs/bytes/iter.gs.js +1 -1
  32. package/dist/gs/bytes/iter.gs.js.map +1 -1
  33. package/dist/gs/bytes/reader.gs.d.ts +1 -1
  34. package/dist/gs/bytes/reader.gs.js +1 -1
  35. package/dist/gs/bytes/reader.gs.js.map +1 -1
  36. package/dist/gs/context/context.d.ts +1 -1
  37. package/dist/gs/context/context.js +1 -1
  38. package/dist/gs/context/context.js.map +1 -1
  39. package/dist/gs/errors/errors.d.ts +1 -1
  40. package/dist/gs/errors/errors.js +3 -15
  41. package/dist/gs/errors/errors.js.map +1 -1
  42. package/dist/gs/fmt/fmt.d.ts +49 -0
  43. package/dist/gs/fmt/fmt.js +322 -0
  44. package/dist/gs/fmt/fmt.js.map +1 -0
  45. package/dist/gs/fmt/index.d.ts +1 -0
  46. package/dist/gs/fmt/index.js +2 -0
  47. package/dist/gs/fmt/index.js.map +1 -0
  48. package/dist/gs/github.com/pkg/errors/errors.d.ts +1 -1
  49. package/dist/gs/github.com/pkg/errors/errors.js +1 -1
  50. package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
  51. package/dist/gs/github.com/pkg/errors/go113.d.ts +1 -1
  52. package/dist/gs/github.com/pkg/errors/go113.js +1 -1
  53. package/dist/gs/github.com/pkg/errors/go113.js.map +1 -1
  54. package/dist/gs/github.com/pkg/errors/stack.d.ts +1 -1
  55. package/dist/gs/github.com/pkg/errors/stack.js +1 -1
  56. package/dist/gs/github.com/pkg/errors/stack.js.map +1 -1
  57. package/dist/gs/internal/byteorder/index.d.ts +1 -1
  58. package/dist/gs/internal/byteorder/index.js +1 -1
  59. package/dist/gs/internal/byteorder/index.js.map +1 -1
  60. package/dist/gs/internal/oserror/errors.d.ts +1 -1
  61. package/dist/gs/io/fs/format.js +1 -1
  62. package/dist/gs/io/fs/format.js.map +1 -1
  63. package/dist/gs/io/fs/fs.d.ts +1 -1
  64. package/dist/gs/io/fs/fs.js +1 -1
  65. package/dist/gs/io/fs/fs.js.map +1 -1
  66. package/dist/gs/io/fs/glob.d.ts +1 -1
  67. package/dist/gs/io/fs/glob.js +1 -1
  68. package/dist/gs/io/fs/glob.js.map +1 -1
  69. package/dist/gs/io/fs/readdir.d.ts +1 -1
  70. package/dist/gs/io/fs/readdir.js +1 -1
  71. package/dist/gs/io/fs/readdir.js.map +1 -1
  72. package/dist/gs/io/fs/readfile.d.ts +1 -1
  73. package/dist/gs/io/fs/readfile.js +1 -1
  74. package/dist/gs/io/fs/readfile.js.map +1 -1
  75. package/dist/gs/io/fs/stat.d.ts +1 -1
  76. package/dist/gs/io/fs/stat.js +1 -1
  77. package/dist/gs/io/fs/stat.js.map +1 -1
  78. package/dist/gs/io/fs/sub.d.ts +1 -1
  79. package/dist/gs/io/fs/sub.js +1 -1
  80. package/dist/gs/io/fs/sub.js.map +1 -1
  81. package/dist/gs/io/fs/walk.d.ts +1 -1
  82. package/dist/gs/io/fs/walk.js +1 -1
  83. package/dist/gs/io/fs/walk.js.map +1 -1
  84. package/dist/gs/io/io.d.ts +1 -1
  85. package/dist/gs/maps/iter.d.ts +1 -1
  86. package/dist/gs/maps/iter.js +1 -1
  87. package/dist/gs/maps/iter.js.map +1 -1
  88. package/dist/gs/maps/maps.d.ts +1 -1
  89. package/dist/gs/maps/maps.js +1 -1
  90. package/dist/gs/maps/maps.js.map +1 -1
  91. package/dist/gs/math/erfinv.gs.js +1 -1
  92. package/dist/gs/math/erfinv.gs.js.map +1 -1
  93. package/dist/gs/math/gamma.gs.js +1 -1
  94. package/dist/gs/math/gamma.gs.js.map +1 -1
  95. package/dist/gs/math/j0.gs.js +1 -1
  96. package/dist/gs/math/j0.gs.js.map +1 -1
  97. package/dist/gs/math/j1.gs.js +1 -1
  98. package/dist/gs/math/j1.gs.js.map +1 -1
  99. package/dist/gs/math/lgamma.gs.js +1 -1
  100. package/dist/gs/math/lgamma.gs.js.map +1 -1
  101. package/dist/gs/math/pow10.gs.js +1 -1
  102. package/dist/gs/math/pow10.gs.js.map +1 -1
  103. package/dist/gs/math/sin.gs.js +1 -1
  104. package/dist/gs/math/sin.gs.js.map +1 -1
  105. package/dist/gs/math/tan.gs.js +1 -1
  106. package/dist/gs/math/tan.gs.js.map +1 -1
  107. package/dist/gs/math/tanh.gs.js +1 -1
  108. package/dist/gs/math/tanh.gs.js.map +1 -1
  109. package/dist/gs/math/trig_reduce.gs.js +1 -1
  110. package/dist/gs/math/trig_reduce.gs.js.map +1 -1
  111. package/dist/gs/os/dir.gs.d.ts +1 -1
  112. package/dist/gs/os/dir_unix.gs.d.ts +1 -1
  113. package/dist/gs/os/dir_unix.gs.js +1 -1
  114. package/dist/gs/os/dir_unix.gs.js.map +1 -1
  115. package/dist/gs/os/dirent_js.gs.d.ts +1 -1
  116. package/dist/gs/os/env.gs.d.ts +1 -1
  117. package/dist/gs/os/env.gs.js +1 -1
  118. package/dist/gs/os/env.gs.js.map +1 -1
  119. package/dist/gs/os/error.gs.d.ts +1 -1
  120. package/dist/gs/os/error.gs.js +1 -1
  121. package/dist/gs/os/error.gs.js.map +1 -1
  122. package/dist/gs/os/exec.gs.d.ts +1 -1
  123. package/dist/gs/os/exec.gs.js +1 -1
  124. package/dist/gs/os/exec.gs.js.map +1 -1
  125. package/dist/gs/os/exec_posix.gs.d.ts +1 -1
  126. package/dist/gs/os/exec_posix.gs.js +1 -1
  127. package/dist/gs/os/exec_posix.gs.js.map +1 -1
  128. package/dist/gs/os/exec_unix.gs.d.ts +1 -1
  129. package/dist/gs/os/executable.gs.d.ts +1 -1
  130. package/dist/gs/os/executable_js.gs.d.ts +1 -1
  131. package/dist/gs/os/executable_wasm.gs.d.ts +1 -1
  132. package/dist/gs/os/file_constants_js.gs.d.ts +1 -1
  133. package/dist/gs/os/file_constants_js.gs.js +1 -1
  134. package/dist/gs/os/file_constants_js.gs.js.map +1 -1
  135. package/dist/gs/os/file_js.gs.d.ts +1 -1
  136. package/dist/gs/os/file_open_unix.gs.d.ts +1 -1
  137. package/dist/gs/os/file_posix_js.gs.d.ts +1 -1
  138. package/dist/gs/os/file_unix_js.gs.d.ts +1 -1
  139. package/dist/gs/os/getwd_js.gs.d.ts +1 -1
  140. package/dist/gs/os/path.gs.d.ts +1 -1
  141. package/dist/gs/os/path.gs.js +1 -1
  142. package/dist/gs/os/path.gs.js.map +1 -1
  143. package/dist/gs/os/path_unix.gs.js +1 -1
  144. package/dist/gs/os/path_unix.gs.js.map +1 -1
  145. package/dist/gs/os/pidfd_js.gs.d.ts +1 -1
  146. package/dist/gs/os/pipe_wasm.gs.d.ts +1 -1
  147. package/dist/gs/os/proc.gs.d.ts +1 -1
  148. package/dist/gs/os/proc.gs.js +1 -1
  149. package/dist/gs/os/proc.gs.js.map +1 -1
  150. package/dist/gs/os/proc_js.gs.d.ts +1 -1
  151. package/dist/gs/os/proc_js.gs.js +1 -1
  152. package/dist/gs/os/proc_js.gs.js.map +1 -1
  153. package/dist/gs/os/rawconn_js.gs.d.ts +1 -1
  154. package/dist/gs/os/removeall_js.gs.d.ts +1 -1
  155. package/dist/gs/os/root_js.gs.d.ts +1 -1
  156. package/dist/gs/os/root_js.gs.js +1 -1
  157. package/dist/gs/os/root_js.gs.js.map +1 -1
  158. package/dist/gs/os/root_nonwindows.gs.d.ts +1 -1
  159. package/dist/gs/os/root_noopenat.gs.d.ts +1 -1
  160. package/dist/gs/os/stat.gs.d.ts +1 -1
  161. package/dist/gs/os/stat_js.gs.d.ts +1 -1
  162. package/dist/gs/os/stat_unix_js.gs.d.ts +1 -1
  163. package/dist/gs/os/sys.gs.d.ts +1 -1
  164. package/dist/gs/os/sys_bsd.gs.d.ts +1 -1
  165. package/dist/gs/os/sys_js.gs.d.ts +1 -1
  166. package/dist/gs/os/tempfile.gs.d.ts +1 -1
  167. package/dist/gs/os/types.gs.d.ts +1 -1
  168. package/dist/gs/os/types.gs.js +1 -1
  169. package/dist/gs/os/types.gs.js.map +1 -1
  170. package/dist/gs/os/types_js.gs.d.ts +1 -1
  171. package/dist/gs/os/types_js.gs.js +1 -1
  172. package/dist/gs/os/types_js.gs.js.map +1 -1
  173. package/dist/gs/os/types_unix.gs.d.ts +1 -1
  174. package/dist/gs/os/types_unix.gs.js +1 -1
  175. package/dist/gs/os/types_unix.gs.js.map +1 -1
  176. package/dist/gs/os/zero_copy_posix.gs.d.ts +1 -1
  177. package/dist/gs/os/zero_copy_posix.gs.js +1 -1
  178. package/dist/gs/os/zero_copy_posix.gs.js.map +1 -1
  179. package/dist/gs/path/filepath/index.d.ts +3 -0
  180. package/dist/gs/path/filepath/index.js +3 -0
  181. package/dist/gs/path/filepath/index.js.map +1 -0
  182. package/dist/gs/path/filepath/match.d.ts +3 -0
  183. package/dist/gs/path/filepath/match.js +212 -0
  184. package/dist/gs/path/filepath/match.js.map +1 -0
  185. package/dist/gs/path/filepath/path.d.ts +26 -0
  186. package/dist/gs/path/filepath/path.js +266 -0
  187. package/dist/gs/path/filepath/path.js.map +1 -0
  188. package/dist/gs/path/match.d.ts +1 -1
  189. package/dist/gs/path/match.js +1 -1
  190. package/dist/gs/path/match.js.map +1 -1
  191. package/dist/gs/path/path.js +1 -1
  192. package/dist/gs/path/path.js.map +1 -1
  193. package/dist/gs/reflect/value.d.ts +1 -1
  194. package/dist/gs/reflect/value.js +14 -6
  195. package/dist/gs/reflect/value.js.map +1 -1
  196. package/dist/gs/reflect/visiblefields.d.ts +1 -1
  197. package/dist/gs/reflect/visiblefields.js +1 -1
  198. package/dist/gs/reflect/visiblefields.js.map +1 -1
  199. package/dist/gs/slices/slices.d.ts +1 -1
  200. package/dist/gs/slices/slices.js +1 -1
  201. package/dist/gs/slices/slices.js.map +1 -1
  202. package/dist/gs/sort/index.d.ts +4 -0
  203. package/dist/gs/sort/index.js +4 -0
  204. package/dist/gs/sort/index.js.map +1 -0
  205. package/dist/gs/sort/search.gs.d.ts +6 -0
  206. package/dist/gs/sort/search.gs.js +125 -0
  207. package/dist/gs/sort/search.gs.js.map +1 -0
  208. package/dist/gs/sort/slice.gs.d.ts +4 -0
  209. package/dist/gs/sort/slice.gs.js +49 -0
  210. package/dist/gs/sort/slice.gs.js.map +1 -0
  211. package/dist/gs/sort/sort.gs.d.ts +37 -0
  212. package/dist/gs/sort/sort.gs.js +203 -0
  213. package/dist/gs/sort/sort.gs.js.map +1 -0
  214. package/dist/gs/strconv/atob.gs.d.ts +1 -1
  215. package/dist/gs/strconv/atob.gs.js +1 -1
  216. package/dist/gs/strconv/atob.gs.js.map +1 -1
  217. package/dist/gs/strconv/atof.gs.d.ts +1 -1
  218. package/dist/gs/strconv/atoi.gs.d.ts +1 -1
  219. package/dist/gs/strconv/atoi.gs.js +5 -6
  220. package/dist/gs/strconv/atoi.gs.js.map +1 -1
  221. package/dist/gs/strconv/ftoa.gs.d.ts +1 -1
  222. package/dist/gs/strconv/ftoa.gs.js +1 -1
  223. package/dist/gs/strconv/ftoa.gs.js.map +1 -1
  224. package/dist/gs/strconv/itoa.gs.d.ts +1 -1
  225. package/dist/gs/strconv/itoa.gs.js +1 -1
  226. package/dist/gs/strconv/itoa.gs.js.map +1 -1
  227. package/dist/gs/strconv/quote.gs.d.ts +1 -1
  228. package/dist/gs/strconv/quote.gs.js +1 -1
  229. package/dist/gs/strconv/quote.gs.js.map +1 -1
  230. package/dist/gs/strings/builder.d.ts +1 -1
  231. package/dist/gs/strings/builder.js +1 -1
  232. package/dist/gs/strings/builder.js.map +1 -1
  233. package/dist/gs/strings/iter.js +1 -1
  234. package/dist/gs/strings/iter.js.map +1 -1
  235. package/dist/gs/strings/reader.d.ts +1 -1
  236. package/dist/gs/strings/reader.js +7 -8
  237. package/dist/gs/strings/reader.js.map +1 -1
  238. package/dist/gs/strings/replace.d.ts +1 -1
  239. package/dist/gs/strings/replace.js +1 -1
  240. package/dist/gs/strings/replace.js.map +1 -1
  241. package/dist/gs/strings/strings.d.ts +1 -1
  242. package/dist/gs/strings/strings.js +1 -1
  243. package/dist/gs/strings/strings.js.map +1 -1
  244. package/dist/gs/sync/atomic/doc.gs.d.ts +1 -1
  245. package/dist/gs/sync/atomic/doc_64.gs.d.ts +1 -1
  246. package/dist/gs/sync/atomic/type.gs.d.ts +1 -1
  247. package/dist/gs/sync/atomic/type.gs.js +1 -1
  248. package/dist/gs/sync/atomic/type.gs.js.map +1 -1
  249. package/dist/gs/sync/atomic/value.gs.d.ts +1 -1
  250. package/dist/gs/sync/atomic/value.gs.js +1 -1
  251. package/dist/gs/sync/atomic/value.gs.js.map +1 -1
  252. package/dist/gs/syscall/index.d.ts +1 -1
  253. package/dist/gs/syscall/index.js +1 -1
  254. package/dist/gs/syscall/index.js.map +1 -1
  255. package/dist/gs/unicode/utf8/utf8.d.ts +1 -1
  256. package/dist/gs/unicode/utf8/utf8.js +1 -1
  257. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  258. package/gs/builtin/builtin.ts +33 -64
  259. package/gs/builtin/errors.ts +25 -0
  260. package/gs/builtin/index.ts +7 -0
  261. package/gs/builtin/slice.ts +142 -71
  262. package/gs/bytes/buffer.gs.ts +1 -1
  263. package/gs/bytes/bytes.gs.ts +123 -15
  264. package/gs/bytes/iter.gs.ts +1 -1
  265. package/gs/bytes/metadata.go +12 -0
  266. package/gs/bytes/reader.gs.ts +1 -1
  267. package/gs/context/context.ts +1 -1
  268. package/gs/errors/errors.ts +3 -16
  269. package/gs/fmt/fmt.ts +407 -0
  270. package/gs/fmt/godoc.txt +382 -0
  271. package/gs/fmt/index.ts +31 -0
  272. package/gs/fmt/metadata.go +7 -0
  273. package/gs/github.com/pkg/errors/errors.ts +1 -1
  274. package/gs/github.com/pkg/errors/go113.ts +2 -2
  275. package/gs/github.com/pkg/errors/stack.ts +1 -1
  276. package/gs/internal/byteorder/index.ts +1 -1
  277. package/gs/internal/metadata.go +7 -0
  278. package/gs/internal/oserror/errors.ts +1 -1
  279. package/gs/io/fs/format.ts +1 -1
  280. package/gs/io/fs/fs.ts +1 -1
  281. package/gs/io/fs/glob.ts +1 -1
  282. package/gs/io/fs/readdir.ts +1 -1
  283. package/gs/io/fs/readfile.ts +1 -1
  284. package/gs/io/fs/stat.ts +1 -1
  285. package/gs/io/fs/sub.ts +1 -1
  286. package/gs/io/fs/walk.ts +1 -1
  287. package/gs/io/io.ts +1 -1
  288. package/gs/io/metadata.go +11 -0
  289. package/gs/maps/iter.ts +1 -1
  290. package/gs/maps/maps.ts +1 -1
  291. package/gs/maps/metadata.go +8 -0
  292. package/gs/math/abs.gs.ts +1 -1
  293. package/gs/math/acosh.gs.ts +1 -1
  294. package/gs/math/asinh.gs.ts +1 -1
  295. package/gs/math/atan.gs.ts +1 -1
  296. package/gs/math/atan2.gs.ts +1 -1
  297. package/gs/math/atanh.gs.ts +1 -1
  298. package/gs/math/bits.gs.ts +1 -1
  299. package/gs/math/cbrt.gs.ts +1 -1
  300. package/gs/math/const.gs.ts +1 -1
  301. package/gs/math/copysign.gs.ts +1 -1
  302. package/gs/math/dim.gs.ts +1 -1
  303. package/gs/math/erf.gs.ts +1 -1
  304. package/gs/math/erfinv.gs.ts +1 -1
  305. package/gs/math/exp.gs.ts +1 -1
  306. package/gs/math/expm1.gs.ts +1 -1
  307. package/gs/math/floor.gs.ts +1 -1
  308. package/gs/math/frexp.gs.ts +1 -1
  309. package/gs/math/gamma.gs.ts +1 -1
  310. package/gs/math/hypot.gs.ts +1 -1
  311. package/gs/math/j0.gs.ts +1 -1
  312. package/gs/math/j1.gs.ts +1 -1
  313. package/gs/math/jn.gs.ts +1 -1
  314. package/gs/math/ldexp.gs.ts +1 -1
  315. package/gs/math/lgamma.gs.ts +1 -1
  316. package/gs/math/log.gs.ts +1 -1
  317. package/gs/math/log10.gs.ts +1 -1
  318. package/gs/math/log1p.gs.ts +1 -1
  319. package/gs/math/logb.gs.ts +1 -1
  320. package/gs/math/metadata.go +7 -0
  321. package/gs/math/mod.gs.ts +1 -1
  322. package/gs/math/modf.gs.ts +1 -1
  323. package/gs/math/nextafter.gs.ts +1 -1
  324. package/gs/math/pow.gs.ts +1 -1
  325. package/gs/math/pow10.gs.ts +1 -1
  326. package/gs/math/remainder.gs.ts +1 -1
  327. package/gs/math/signbit.gs.ts +1 -1
  328. package/gs/math/sin.gs.ts +1 -1
  329. package/gs/math/sincos.gs.ts +1 -1
  330. package/gs/math/sinh.gs.ts +1 -1
  331. package/gs/math/sqrt.gs.ts +1 -1
  332. package/gs/math/tan.gs.ts +1 -1
  333. package/gs/math/tanh.gs.ts +1 -1
  334. package/gs/math/trig_reduce.gs.ts +1 -1
  335. package/gs/math/unsafe.gs.ts +1 -1
  336. package/gs/os/dir.gs.ts +1 -1
  337. package/gs/os/dir_unix.gs.ts +1 -1
  338. package/gs/os/dirent_js.gs.ts +1 -1
  339. package/gs/os/env.gs.ts +1 -1
  340. package/gs/os/error.gs.ts +1 -1
  341. package/gs/os/error_errno.gs.ts +1 -1
  342. package/gs/os/exec.gs.ts +1 -1
  343. package/gs/os/exec_nohandle.gs.ts +1 -1
  344. package/gs/os/exec_posix.gs.ts +1 -1
  345. package/gs/os/exec_unix.gs.ts +1 -1
  346. package/gs/os/executable.gs.ts +1 -1
  347. package/gs/os/executable_js.gs.ts +1 -1
  348. package/gs/os/executable_wasm.gs.ts +1 -1
  349. package/gs/os/file_constants_js.gs.ts +1 -1
  350. package/gs/os/file_js.gs.ts +1 -1
  351. package/gs/os/file_open_unix.gs.ts +1 -1
  352. package/gs/os/file_posix_js.gs.ts +1 -1
  353. package/gs/os/file_unix_js.gs.ts +1 -1
  354. package/gs/os/getwd_js.gs.ts +1 -1
  355. package/gs/os/metadata.go +17 -0
  356. package/gs/os/path.gs.ts +1 -1
  357. package/gs/os/path_unix.gs.ts +1 -1
  358. package/gs/os/pidfd_js.gs.ts +1 -1
  359. package/gs/os/pipe_wasm.gs.ts +1 -1
  360. package/gs/os/proc.gs.ts +1 -1
  361. package/gs/os/proc_js.gs.ts +1 -1
  362. package/gs/os/rawconn_js.gs.ts +1 -1
  363. package/gs/os/removeall_js.gs.ts +1 -1
  364. package/gs/os/root_js.gs.ts +1 -1
  365. package/gs/os/root_nonwindows.gs.ts +1 -1
  366. package/gs/os/root_noopenat.gs.ts +1 -1
  367. package/gs/os/stat.gs.ts +1 -1
  368. package/gs/os/stat_js.gs.ts +1 -1
  369. package/gs/os/stat_unix_js.gs.ts +1 -1
  370. package/gs/os/sticky_bsd.gs.ts +1 -1
  371. package/gs/os/sys.gs.ts +1 -1
  372. package/gs/os/sys_bsd.gs.ts +1 -1
  373. package/gs/os/sys_js.gs.ts +1 -1
  374. package/gs/os/tempfile.gs.ts +1 -1
  375. package/gs/os/types.gs.ts +1 -1
  376. package/gs/os/types_js.gs.ts +1 -1
  377. package/gs/os/types_unix.gs.ts +1 -1
  378. package/gs/os/wait_unimp.gs.ts +1 -1
  379. package/gs/os/zero_copy_posix.gs.ts +1 -1
  380. package/gs/os/zero_copy_stub.gs.ts +1 -1
  381. package/gs/path/filepath/godoc.txt +35 -0
  382. package/gs/path/filepath/index.ts +27 -0
  383. package/gs/path/filepath/match.test.ts +274 -0
  384. package/gs/path/filepath/match.ts +249 -0
  385. package/gs/path/filepath/path.test.ts +246 -0
  386. package/gs/path/filepath/path.ts +329 -0
  387. package/gs/path/match.ts +1 -1
  388. package/gs/path/metadata.go +8 -0
  389. package/gs/path/path.ts +1 -1
  390. package/gs/reflect/metadata.go +7 -0
  391. package/gs/reflect/value.ts +14 -6
  392. package/gs/reflect/visiblefields.ts +1 -1
  393. package/gs/slices/slices.ts +1 -1
  394. package/gs/sort/godoc.txt +27 -0
  395. package/gs/sort/index.ts +24 -0
  396. package/gs/sort/search.gs.ts +128 -0
  397. package/gs/sort/slice.gs.ts +59 -0
  398. package/gs/sort/sort.gs.ts +227 -0
  399. package/gs/strconv/atob.gs.ts +1 -1
  400. package/gs/strconv/atof.gs.ts +1 -1
  401. package/gs/strconv/atoi.gs.ts +5 -6
  402. package/gs/strconv/doc.gs.ts +1 -1
  403. package/gs/strconv/ftoa.gs.ts +1 -1
  404. package/gs/strconv/itoa.gs.ts +1 -1
  405. package/gs/strconv/metadata.go +7 -0
  406. package/gs/strconv/quote.gs.ts +1 -1
  407. package/gs/strings/builder.ts +1 -1
  408. package/gs/strings/iter.ts +1 -1
  409. package/gs/strings/metadata.go +11 -0
  410. package/gs/strings/reader.ts +7 -9
  411. package/gs/strings/replace.ts +1 -1
  412. package/gs/strings/strings.ts +1 -1
  413. package/gs/sync/atomic/doc.gs.ts +1 -1
  414. package/gs/sync/atomic/doc_64.gs.ts +1 -1
  415. package/gs/sync/atomic/type.gs.ts +1 -1
  416. package/gs/sync/atomic/value.gs.ts +1 -1
  417. package/gs/sync/metadata.go +7 -0
  418. package/gs/syscall/index.ts +1 -1
  419. package/gs/unicode/utf8/utf8.ts +1 -1
  420. package/package.json +3 -3
  421. package/gs/builtin/io.ts +0 -22
@@ -831,10 +831,8 @@ func (v *analysisVisitor) containsAsyncOperations(node ast.Node) bool {
831
831
 
832
832
  // Check if the type is from an imported package
833
833
  if typePkg := namedType.Obj().Pkg(); typePkg != nil && typePkg != v.pkg.Types {
834
- pkgPath := typePkg.Path()
835
- // Extract package name from path (e.g., "sync" from "github.com/.../gs/sync")
836
- parts := strings.Split(pkgPath, "/")
837
- pkgName := parts[len(parts)-1]
834
+ // Use the actual package name from the type information
835
+ pkgName := typePkg.Name()
838
836
 
839
837
  // Check if this method is async based on metadata
840
838
  if v.analysis.IsMethodAsync(pkgName, typeName, methodName) {
@@ -905,10 +903,21 @@ func AnalyzeFile(file *ast.File, pkg *packages.Package, analysis *Analysis, cmap
905
903
  importVars: make(map[string]struct{}),
906
904
  }
907
905
 
908
- // Use the import name or package name as the key
909
- key := packageNameFromGoPath(path)
906
+ // Use the import name or the actual package name as the key
907
+ var key string
910
908
  if name != "" {
909
+ // Explicit alias provided
911
910
  key = name
911
+ } else {
912
+ // No explicit alias, use the actual package name from type information
913
+ // This handles cases where package name differs from the last path segment
914
+ if actualName, err := getActualPackageName(path, pkg.Imports); err == nil {
915
+ key = actualName
916
+ } else {
917
+ // Fallback to last segment of path if package not found in type information
918
+ pts := strings.Split(path, "/")
919
+ key = pts[len(pts)-1]
920
+ }
912
921
  }
913
922
 
914
923
  analysis.Imports[key] = fileImp
@@ -5,6 +5,7 @@ import (
5
5
  "fmt"
6
6
  "go/ast"
7
7
  "go/constant"
8
+ "go/parser"
8
9
  "go/token"
9
10
  "go/types"
10
11
  "os"
@@ -140,7 +141,7 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
140
141
  // Check if this package has a handwritten equivalent
141
142
  if hasHandwrittenEquivalent(pkg.PkgPath) {
142
143
  // Add this package but don't visit its dependencies
143
- c.le.Debugf("Skipping dependencies of handwritten package: %s", pkg.PkgPath)
144
+ // c.le.Debugf("Skipping dependencies of handwritten package: %s", pkg.PkgPath)
144
145
  return
145
146
  }
146
147
 
@@ -148,13 +149,13 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
148
149
  for _, imp := range pkg.Imports {
149
150
  // Skip protobuf-go-lite packages and their dependencies
150
151
  if isProtobufGoLitePackage(imp.PkgPath) {
151
- c.le.Debugf("Skipping protobuf-go-lite package: %s", imp.PkgPath)
152
+ // c.le.Debugf("Skipping protobuf-go-lite package: %s", imp.PkgPath)
152
153
  continue
153
154
  }
154
155
 
155
156
  // Skip packages that are only used by .pb.go files
156
157
  if isPackageOnlyUsedByProtobufFiles(pkg, imp.PkgPath) {
157
- c.le.Debugf("Skipping package only used by .pb.go files: %s", imp.PkgPath)
158
+ // c.le.Debugf("Skipping package only used by .pb.go files: %s", imp.PkgPath)
158
159
  continue
159
160
  }
160
161
 
@@ -193,7 +194,7 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
193
194
 
194
195
  // If DisableEmitBuiltin is false, we need to copy the builtin package to the output directory
195
196
  if !c.config.DisableEmitBuiltin {
196
- c.le.Infof("Copying builtin package to output directory")
197
+ c.le.Debugf("Copying builtin package to output directory")
197
198
  builtinPath := "gs/builtin"
198
199
  outputPath := ComputeModulePath(c.config.OutputPath, "builtin")
199
200
  if err := c.copyEmbeddedPackage(builtinPath, outputPath); err != nil {
@@ -202,6 +203,9 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
202
203
  result.CopiedPackages = append(result.CopiedPackages, "builtin")
203
204
  }
204
205
 
206
+ // Track which gs packages have been processed to avoid duplicates
207
+ processedGsPackages := make(map[string]bool)
208
+
205
209
  // Compile all packages
206
210
  for _, pkg := range pkgs {
207
211
  // Check if the package has a handwritten equivalent
@@ -214,32 +218,14 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
214
218
  }
215
219
  if gsErr == nil {
216
220
  if c.config.DisableEmitBuiltin {
217
- c.le.Infof("Skipping compilation for overridden package %s", pkg.PkgPath)
221
+ // c.le.Infof("Skipping compilation for overridden package %s", pkg.PkgPath)
218
222
  result.CopiedPackages = append(result.CopiedPackages, pkg.PkgPath)
219
223
  continue
220
224
  } else {
221
- // If DisableEmitBuiltin is false, we need to copy the handwritten package to the output directory
222
- c.le.Infof("Copying handwritten package %s to output directory", pkg.PkgPath)
223
-
224
- // Compute output path for this package
225
- outputPath := ComputeModulePath(c.config.OutputPath, pkg.PkgPath)
226
-
227
- // Remove existing directory if it exists
228
- if err := os.RemoveAll(outputPath); err != nil {
229
- return nil, fmt.Errorf("failed to remove existing output directory for %s: %w", pkg.PkgPath, err)
230
- }
231
-
232
- // Create the output directory
233
- if err := os.MkdirAll(outputPath, 0o755); err != nil {
234
- return nil, fmt.Errorf("failed to create output directory for %s: %w", pkg.PkgPath, err)
225
+ // If DisableEmitBuiltin is false, we need to copy the handwritten package and its dependencies
226
+ if err := c.copyGsPackageWithDependencies(pkg.PkgPath, processedGsPackages, result); err != nil {
227
+ return nil, fmt.Errorf("failed to copy handwritten package %s with dependencies: %w", pkg.PkgPath, err)
235
228
  }
236
-
237
- // Copy files from embedded FS to output directory
238
- if err := c.copyEmbeddedPackage(gsSourcePath, outputPath); err != nil {
239
- return nil, fmt.Errorf("failed to copy embedded package %s: %w", pkg.PkgPath, err)
240
- }
241
-
242
- result.CopiedPackages = append(result.CopiedPackages, pkg.PkgPath)
243
229
  continue
244
230
  }
245
231
  }
@@ -260,6 +246,8 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
260
246
  return nil, fmt.Errorf("failed to compile package %s: %w", pkg.PkgPath, err)
261
247
  }
262
248
 
249
+ c.le.Info(pkg.PkgPath)
250
+
263
251
  result.CompiledPackages = append(result.CompiledPackages, pkg.PkgPath)
264
252
  }
265
253
 
@@ -352,7 +340,8 @@ func (c *PackageCompiler) Compile(ctx context.Context) error {
352
340
  }
353
341
  }
354
342
 
355
- c.le.WithField("file", relWdFileName).Debug("compiling file")
343
+ // log just the filename
344
+ c.le.Debugf("GS: %s", filepath.Base(fileName))
356
345
  if err := c.CompileFile(ctx, fileName, f, packageAnalysis); err != nil {
357
346
  return err
358
347
  }
@@ -604,7 +593,7 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
604
593
  goWriter := NewGoToTSCompiler(c.codeWriter, c.pkg, c.Analysis)
605
594
 
606
595
  // Add import for the goscript runtime using namespace import and alias
607
- c.codeWriter.WriteLinef("import * as $ from %q;", "@goscript/builtin/builtin.js")
596
+ c.codeWriter.WriteLinef("import * as $ from %q;", "@goscript/builtin/index.js")
608
597
 
609
598
  // Check if there are any .pb.go files in this package and add imports for them
610
599
  if err := c.addProtobufImports(); err != nil {
@@ -945,13 +934,9 @@ func (c *GoToTSCompiler) writeConstantValue(constObj *types.Const) {
945
934
 
946
935
  // copyEmbeddedPackage recursively copies files from an embedded FS path to a filesystem directory.
947
936
  // It handles both regular files and directories, but only copies .gs.ts and .ts files.
937
+ // It preserves existing subdirectories that aren't being overwritten.
948
938
  func (c *Compiler) copyEmbeddedPackage(embeddedPath string, outputPath string) error {
949
- // Remove the output path if it exists
950
- if err := os.RemoveAll(outputPath); err != nil {
951
- return fmt.Errorf("failed to remove output directory %s: %w", outputPath, err)
952
- }
953
-
954
- // Create the output path
939
+ // Create the output path if it doesn't exist
955
940
  if err := os.MkdirAll(outputPath, 0o755); err != nil {
956
941
  return fmt.Errorf("failed to create output directory %s: %w", outputPath, err)
957
942
  }
@@ -981,10 +966,17 @@ func (c *Compiler) copyEmbeddedPackage(embeddedPath string, outputPath string) e
981
966
  // Only copy .gs.ts and .ts files, skip .go files and others
982
967
  fileName := entry.Name()
983
968
  if !strings.HasSuffix(fileName, ".gs.ts") && !strings.HasSuffix(fileName, ".ts") {
984
- c.le.Debugf("Skipping non-TypeScript file: %s", fileName)
969
+ // c.le.Debugf("Skipping non-TypeScript file: %s", fileName)
985
970
  continue
986
971
  }
987
972
 
973
+ // Remove existing file if it exists (but preserve directories)
974
+ if stat, err := os.Stat(outputEntryPath); err == nil && !stat.IsDir() {
975
+ if err := os.Remove(outputEntryPath); err != nil {
976
+ return fmt.Errorf("failed to remove existing file %s: %w", outputEntryPath, err)
977
+ }
978
+ }
979
+
988
980
  // Read the file content from the embedded FS
989
981
  content, err := gs.GsOverrides.ReadFile(entryPath)
990
982
  if err != nil {
@@ -1000,3 +992,161 @@ func (c *Compiler) copyEmbeddedPackage(embeddedPath string, outputPath string) e
1000
992
 
1001
993
  return nil
1002
994
  }
995
+
996
+ // GsPackageMetadata holds metadata about a gs/ package
997
+ type GsPackageMetadata struct {
998
+ // Dependencies lists the import paths that this gs/ package requires
999
+ Dependencies []string
1000
+ }
1001
+
1002
+ // ReadGsPackageMetadata reads dependency metadata from .go files in a gs/ package
1003
+ // It looks for a var variable named "GsDependencies" which should be a slice of strings
1004
+ // containing the import paths that this package depends on.
1005
+ func (c *Compiler) ReadGsPackageMetadata(gsSourcePath string) (*GsPackageMetadata, error) {
1006
+ metadata := &GsPackageMetadata{
1007
+ Dependencies: []string{},
1008
+ }
1009
+
1010
+ // Check if there are any .go files in the gs package directory
1011
+ entries, err := gs.GsOverrides.ReadDir(gsSourcePath)
1012
+ if err != nil {
1013
+ return metadata, nil // No metadata files, return empty metadata
1014
+ }
1015
+
1016
+ // Look for .go files containing metadata
1017
+ for _, entry := range entries {
1018
+ if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go") {
1019
+ metadataFilePath := filepath.Join(gsSourcePath, entry.Name())
1020
+
1021
+ // Read the .go file content
1022
+ content, err := gs.GsOverrides.ReadFile(metadataFilePath)
1023
+ if err != nil {
1024
+ continue // Skip files we can't read
1025
+ }
1026
+
1027
+ // Parse the file to extract metadata
1028
+ if deps, err := c.extractDependenciesFromGoFile(content); err == nil {
1029
+ metadata.Dependencies = append(metadata.Dependencies, deps...)
1030
+ }
1031
+ }
1032
+ }
1033
+
1034
+ return metadata, nil
1035
+ }
1036
+
1037
+ // extractDependenciesFromGoFile parses a .go file and extracts the GsDependencies var
1038
+ func (c *Compiler) extractDependenciesFromGoFile(content []byte) ([]string, error) {
1039
+ // Parse the Go file
1040
+ fset := token.NewFileSet()
1041
+ file, err := parser.ParseFile(fset, "metadata.go", content, 0)
1042
+ if err != nil {
1043
+ return nil, err
1044
+ }
1045
+
1046
+ var dependencies []string
1047
+
1048
+ // Look for var declarations
1049
+ for _, decl := range file.Decls {
1050
+ if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.VAR {
1051
+ for _, spec := range genDecl.Specs {
1052
+ if valueSpec, ok := spec.(*ast.ValueSpec); ok {
1053
+ for i, name := range valueSpec.Names {
1054
+ if name.Name == "GsDependencies" {
1055
+ // Found the GsDependencies var, extract its value
1056
+ if i < len(valueSpec.Values) {
1057
+ if deps := c.extractStringSliceFromExpr(valueSpec.Values[i]); deps != nil {
1058
+ dependencies = append(dependencies, deps...)
1059
+ }
1060
+ }
1061
+ }
1062
+ }
1063
+ }
1064
+ }
1065
+ }
1066
+ }
1067
+
1068
+ return dependencies, nil
1069
+ }
1070
+
1071
+ // extractStringSliceFromExpr extracts string values from a composite literal expression
1072
+ func (c *Compiler) extractStringSliceFromExpr(expr ast.Expr) []string {
1073
+ var result []string
1074
+
1075
+ if compLit, ok := expr.(*ast.CompositeLit); ok {
1076
+ for _, elt := range compLit.Elts {
1077
+ if basicLit, ok := elt.(*ast.BasicLit); ok && basicLit.Kind == token.STRING {
1078
+ // Remove quotes from string literal
1079
+ value := basicLit.Value
1080
+ if len(value) >= 2 && value[0] == '"' && value[len(value)-1] == '"' {
1081
+ result = append(result, value[1:len(value)-1])
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ return result
1088
+ }
1089
+
1090
+ // copyGsPackageWithDependencies copies a gs/ package and all its dependencies recursively
1091
+ // It tracks already processed packages to avoid infinite loops and duplicate work
1092
+ func (c *Compiler) copyGsPackageWithDependencies(packagePath string, processedPackages map[string]bool, result *CompilationResult) error {
1093
+ // Check if we've already processed this package
1094
+ if processedPackages[packagePath] {
1095
+ return nil
1096
+ }
1097
+
1098
+ // Mark this package as being processed
1099
+ processedPackages[packagePath] = true
1100
+
1101
+ gsSourcePath := "gs/" + packagePath
1102
+
1103
+ // Check if the gs package actually exists
1104
+ _, gsErr := gs.GsOverrides.ReadDir(gsSourcePath)
1105
+ if gsErr != nil {
1106
+ if os.IsNotExist(gsErr) {
1107
+ c.le.Debugf("gs package %s does not exist, skipping", packagePath)
1108
+ return nil
1109
+ }
1110
+ return gsErr
1111
+ }
1112
+
1113
+ // Read metadata to get dependencies
1114
+ metadata, err := c.ReadGsPackageMetadata(gsSourcePath)
1115
+ if err != nil {
1116
+ c.le.WithError(err).Warnf("Failed to read metadata for gs package %s, continuing without dependencies", packagePath)
1117
+ metadata = &GsPackageMetadata{Dependencies: []string{}}
1118
+ }
1119
+
1120
+ // Log dependencies if any are found
1121
+ /*
1122
+ if len(metadata.Dependencies) > 0 {
1123
+ c.le.Debugf("Package %s has dependencies: %v", packagePath, metadata.Dependencies)
1124
+ }
1125
+ */
1126
+
1127
+ // First, recursively process all dependencies
1128
+ for _, depPath := range metadata.Dependencies {
1129
+ if err := c.copyGsPackageWithDependencies(depPath, processedPackages, result); err != nil {
1130
+ return fmt.Errorf("failed to copy dependency %s of package %s: %w", depPath, packagePath, err)
1131
+ }
1132
+ }
1133
+
1134
+ // Now copy the package itself
1135
+ // c.le.Debugf("Copying handwritten package %s to output directory", packagePath)
1136
+
1137
+ // Compute output path for this package
1138
+ outputPath := ComputeModulePath(c.config.OutputPath, packagePath)
1139
+
1140
+ // Create the output directory
1141
+ if err := os.MkdirAll(outputPath, 0o755); err != nil {
1142
+ return fmt.Errorf("failed to create output directory for %s: %w", packagePath, err)
1143
+ }
1144
+
1145
+ // Copy files from embedded FS to output directory
1146
+ if err := c.copyEmbeddedPackage(gsSourcePath, outputPath); err != nil {
1147
+ return fmt.Errorf("failed to copy embedded package %s: %w", packagePath, err)
1148
+ }
1149
+
1150
+ result.CopiedPackages = append(result.CopiedPackages, packagePath)
1151
+ return nil
1152
+ }
@@ -22,7 +22,7 @@ import (
22
22
  // - `make(map[K]V)` becomes `$.makeMap<K_ts, V_ts>()`.
23
23
  // - `make([]T, len, cap)` becomes `$.makeSlice<T_ts>(len, cap)`.
24
24
  // - `make([]byte, len, cap)` becomes `new Uint8Array(len)`.
25
- // - `string(runeVal)` becomes `String.fromCharCode(runeVal)`.
25
+ // - `string(runeVal)` becomes `$.runeOrStringToString(runeVal)`.
26
26
  // - `string([]runeVal)` becomes `$.runesToString(sliceVal)`.
27
27
  // - `string([]byteVal)` becomes `$.bytesToString(sliceVal)`.
28
28
  // - `[]rune(stringVal)` becomes `$.stringToRunes(stringVal)“.
@@ -830,9 +830,9 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
830
830
  if isCallExpr {
831
831
  // Check if it's a call to rune()
832
832
  if innerFunIdent, innerFunIsIdent := innerCall.Fun.(*ast.Ident); innerFunIsIdent && innerFunIdent.String() == "rune" {
833
- // Translate string(rune(val)) to String.fromCharCode(val)
833
+ // Translate string(rune(val)) to $.runeOrStringToString(val)
834
834
  if len(innerCall.Args) == 1 {
835
- c.tsw.WriteLiterally("String.fromCharCode(")
835
+ c.tsw.WriteLiterally("$.runeOrStringToString(")
836
836
  if err := c.WriteValueExpr(innerCall.Args[0]); err != nil {
837
837
  return fmt.Errorf("failed to write argument for string(rune) conversion: %w", err)
838
838
  }
@@ -855,8 +855,8 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
855
855
  }
856
856
 
857
857
  if basic, isBasic := tv.Type.Underlying().(*types.Basic); isBasic && (basic.Kind() == types.Int32 || basic.Kind() == types.UntypedRune) {
858
- // Translate string(rune_val) to String.fromCharCode(rune_val)
859
- c.tsw.WriteLiterally("String.fromCharCode(")
858
+ // Translate string(rune_val) to $.runeOrStringToString(rune_val)
859
+ c.tsw.WriteLiterally("$.runeOrStringToString(")
860
860
  if err := c.WriteValueExpr(arg); err != nil {
861
861
  return fmt.Errorf("failed to write argument for string(int32) conversion: %w", err)
862
862
  }
@@ -1167,10 +1167,8 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
1167
1167
 
1168
1168
  // Check if the type is from an imported package
1169
1169
  if typePkg := namedType.Obj().Pkg(); typePkg != nil && typePkg != c.pkg.Types {
1170
- pkgPath := typePkg.Path()
1171
- // Extract package name from path (e.g., "sync" from "github.com/.../gs/sync")
1172
- parts := strings.Split(pkgPath, "/")
1173
- pkgName := parts[len(parts)-1]
1170
+ // Use the actual package name from the type information
1171
+ pkgName := typePkg.Name()
1174
1172
 
1175
1173
  // Check if this method is async based on metadata
1176
1174
  if c.analysis.IsMethodAsync(pkgName, typeName, methodName) {
@@ -20,6 +20,31 @@ import (
20
20
  // - Interface types -> TypeScript interface types or "any"
21
21
  // - Function types -> TypeScript function signatures
22
22
  func (c *GoToTSCompiler) WriteTypeExpr(a ast.Expr) {
23
+ // Handle selector expressions (e.g., os.FileInfo) specially to preserve qualified names
24
+ if selectorExpr, ok := a.(*ast.SelectorExpr); ok {
25
+ if pkgIdent, ok := selectorExpr.X.(*ast.Ident); ok {
26
+ // Check if this is a package selector (e.g., os.FileInfo)
27
+ if obj := c.pkg.TypesInfo.Uses[pkgIdent]; obj != nil {
28
+ if _, isPkg := obj.(*types.PkgName); isPkg {
29
+ // This is a package.Type reference - write the qualified name
30
+ c.tsw.WriteLiterally(pkgIdent.Name)
31
+ c.tsw.WriteLiterally(".")
32
+ c.tsw.WriteLiterally(selectorExpr.Sel.Name)
33
+
34
+ // Check if this is a function type and add | null
35
+ if typ := c.pkg.TypesInfo.TypeOf(a); typ != nil {
36
+ if namedType, isNamed := typ.(*types.Named); isNamed {
37
+ if _, isSignature := namedType.Underlying().(*types.Signature); isSignature {
38
+ c.tsw.WriteLiterally(" | null")
39
+ }
40
+ }
41
+ }
42
+ return
43
+ }
44
+ }
45
+ }
46
+ }
47
+
23
48
  // Get type information for the expression and use WriteGoType
24
49
  typ := c.pkg.TypesInfo.TypeOf(a)
25
50
  c.WriteGoType(typ, GoTypeContextGeneral)
package/compiler/field.go CHANGED
@@ -1,6 +1,9 @@
1
1
  package compiler
2
2
 
3
- import "go/ast"
3
+ import (
4
+ "go/ast"
5
+ "go/types"
6
+ )
4
7
 
5
8
  // WriteFieldList translates a Go field list (`ast.FieldList`), which can represent
6
9
  // function parameters, function results, or struct fields, into its TypeScript equivalent.
@@ -49,8 +52,8 @@ func (c *GoToTSCompiler) WriteFieldList(a *ast.FieldList, isArguments bool) {
49
52
  }
50
53
  c.tsw.WriteLiterally(c.sanitizeIdentifier(name.Name))
51
54
  c.tsw.WriteLiterally(": ")
52
- typ := c.pkg.TypesInfo.TypeOf(field.Type)
53
- c.WriteGoType(typ, GoTypeContextGeneral)
55
+ // Use WriteTypeExpr to preserve qualified names like os.FileInfo
56
+ c.WriteTypeExpr(field.Type)
54
57
  }
55
58
  }
56
59
 
@@ -70,8 +73,19 @@ func (c *GoToTSCompiler) WriteFieldList(a *ast.FieldList, isArguments bool) {
70
73
 
71
74
  c.tsw.WriteLiterally(": ")
72
75
  if ellipsis, ok := lastParam.Type.(*ast.Ellipsis); ok {
73
- c.WriteTypeExpr(ellipsis.Elt)
74
- c.tsw.WriteLiterally("[]")
76
+ // Get the actual variadic parameter type from the type checker
77
+ // This gives us the slice type (e.g., []interface{})
78
+ variadicType := c.pkg.TypesInfo.TypeOf(lastParam.Type)
79
+ if sliceType, isSlice := variadicType.(*types.Slice); isSlice {
80
+ // For variadic parameters, write the element type followed by []
81
+ // This handles interface{} properly by generating "any[]" instead of "null | any[]"
82
+ c.WriteGoType(sliceType.Elem(), GoTypeContextVariadicParam)
83
+ c.tsw.WriteLiterally("[]")
84
+ } else {
85
+ // Fallback to the original AST-based approach for edge cases
86
+ c.WriteTypeExpr(ellipsis.Elt)
87
+ c.tsw.WriteLiterally("[]")
88
+ }
75
89
  }
76
90
  } else {
77
91
  // Handle regular parameter list for function declarations
@@ -88,8 +102,8 @@ func (c *GoToTSCompiler) WriteFieldList(a *ast.FieldList, isArguments bool) {
88
102
  }
89
103
  c.tsw.WriteLiterally(c.sanitizeIdentifier(name.Name))
90
104
  c.tsw.WriteLiterally(": ")
91
- typ := c.pkg.TypesInfo.TypeOf(field.Type)
92
- c.WriteGoType(typ, GoTypeContextGeneral) // Use WriteGoType for parameter type
105
+ // Use WriteTypeExpr to preserve qualified names like os.FileInfo
106
+ c.WriteTypeExpr(field.Type)
93
107
  }
94
108
  } else {
95
109
  // For struct fields and other non-argument fields
@@ -0,0 +1,80 @@
1
+ package compiler
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/sirupsen/logrus"
7
+ )
8
+
9
+ func TestReadGsPackageMetadata(t *testing.T) {
10
+ // Create a basic compiler configuration
11
+ config := &Config{
12
+ OutputPath: "./test_output",
13
+ Dir: ".",
14
+ }
15
+
16
+ // Create a logger (set to warn level to reduce noise in tests)
17
+ logger := logrus.New()
18
+ logger.SetLevel(logrus.WarnLevel)
19
+ le := logrus.NewEntry(logger)
20
+
21
+ // Create a compiler
22
+ comp, err := NewCompiler(config, le, nil)
23
+ if err != nil {
24
+ t.Fatalf("Failed to create compiler: %v", err)
25
+ }
26
+
27
+ // Test reading metadata from the bytes package
28
+ metadata, err := comp.ReadGsPackageMetadata("gs/bytes")
29
+ if err != nil {
30
+ t.Fatalf("Failed to read metadata: %v", err)
31
+ }
32
+
33
+ // Check that we found the expected dependency
34
+ if len(metadata.Dependencies) == 0 {
35
+ t.Errorf("Expected at least one dependency, got none")
36
+ }
37
+
38
+ // Check for the specific "iter" dependency
39
+ foundIter := false
40
+ for _, dep := range metadata.Dependencies {
41
+ if dep == "iter" {
42
+ foundIter = true
43
+ break
44
+ }
45
+ }
46
+
47
+ if !foundIter {
48
+ t.Errorf("Expected to find 'iter' dependency, got dependencies: %v", metadata.Dependencies)
49
+ }
50
+ }
51
+
52
+ func TestReadGsPackageMetadataNonExistent(t *testing.T) {
53
+ // Create a basic compiler configuration
54
+ config := &Config{
55
+ OutputPath: "./test_output",
56
+ Dir: ".",
57
+ }
58
+
59
+ // Create a logger (set to warn level to reduce noise in tests)
60
+ logger := logrus.New()
61
+ logger.SetLevel(logrus.WarnLevel)
62
+ le := logrus.NewEntry(logger)
63
+
64
+ // Create a compiler
65
+ comp, err := NewCompiler(config, le, nil)
66
+ if err != nil {
67
+ t.Fatalf("Failed to create compiler: %v", err)
68
+ }
69
+
70
+ // Test reading metadata from a non-existent package
71
+ metadata, err := comp.ReadGsPackageMetadata("gs/nonexistent")
72
+ if err != nil {
73
+ t.Fatalf("Expected no error for non-existent package, got: %v", err)
74
+ }
75
+
76
+ // Should return empty metadata for non-existent packages
77
+ if len(metadata.Dependencies) != 0 {
78
+ t.Errorf("Expected empty dependencies for non-existent package, got: %v", metadata.Dependencies)
79
+ }
80
+ }
package/compiler/lit.go CHANGED
@@ -76,12 +76,7 @@ func (c *GoToTSCompiler) WriteBasicLit(exp *ast.BasicLit) {
76
76
  // Check if the raw string contains backslashes that would be problematic in template literals
77
77
  if strings.Contains(content, `\`) {
78
78
  // Convert to a regular string literal with proper escaping
79
- // Replace backslashes with double backslashes for TypeScript
80
- content = strings.ReplaceAll(content, `\`, `\\`)
81
- // Replace double quotes with escaped double quotes
82
- content = strings.ReplaceAll(content, `"`, `\"`)
83
- // Write as a regular string literal
84
- c.tsw.WriteLiterallyf(`"%s"`, content)
79
+ c.tsw.WriteLiterallyf("%q", content)
85
80
  } else {
86
81
  // No backslashes, safe to use template literal
87
82
  // Escape invalid \x, \u, and \U sequences that would cause TS1125 errors
@@ -4,6 +4,8 @@ import (
4
4
  "fmt"
5
5
  "path/filepath"
6
6
  "strings"
7
+
8
+ "golang.org/x/tools/go/packages"
7
9
  )
8
10
 
9
11
  // ComputeModulePath computes the root of the output typescript module.
@@ -21,10 +23,14 @@ func translateGoPathToTypescriptPath(goImportPath string) string {
21
23
  return fmt.Sprintf("@goscript/%s", goImportPath)
22
24
  }
23
25
 
24
- // packageNameFromGoPath attempts to determine the package name from the last segment of the go path.
25
- func packageNameFromGoPath(goPkgPath string) string {
26
- pts := strings.Split(goPkgPath, "/")
27
- return pts[len(pts)-1]
26
+ // getActualPackageName returns the actual Go package name from package information.
27
+ // If the package is not found in the imports map, returns an error instead of falling back.
28
+ // This handles cases where the package name differs from the last segment of the import path.
29
+ func getActualPackageName(importPath string, importsMap map[string]*packages.Package) (string, error) {
30
+ if pkg, exists := importsMap[importPath]; exists && pkg.Name != "" {
31
+ return pkg.Name, nil
32
+ }
33
+ return "", fmt.Errorf("package %s not found in imports map", importPath)
28
34
  }
29
35
 
30
36
  // TranslateGoFilePathToTypescriptFilePath converts the go package path and typescript filename to output path within the typescript output dir
package/compiler/spec.go CHANGED
@@ -499,7 +499,7 @@ func (c *GoToTSCompiler) WriteInterfaceTypeSpec(a *ast.TypeSpec, t *ast.Interfac
499
499
  // It extracts the Go import path (e.g., `"path/to/pkg"`) and determines the
500
500
  // import alias/name for TypeScript. If the Go import has an explicit name
501
501
  // (e.g., `alias "path/to/pkg"`), that alias is used. Otherwise, the package
502
- // name is derived from the Go path.
502
+ // name is derived from the actual Go package name, not the import path.
503
503
  //
504
504
  // The Go path is then translated to a TypeScript module path using
505
505
  // `translateGoPathToTypescriptPath`.
@@ -516,9 +516,22 @@ func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec) {
516
516
  }
517
517
 
518
518
  goPath := a.Path.Value[1 : len(a.Path.Value)-1]
519
- impName := packageNameFromGoPath(goPath)
519
+
520
+ // Determine the import name to use in TypeScript
521
+ var impName string
520
522
  if a.Name != nil && a.Name.Name != "" {
523
+ // Explicit alias provided: import alias "path/to/pkg"
521
524
  impName = a.Name.Name
525
+ } else {
526
+ // No explicit alias, use the actual package name from type information
527
+ // This handles cases where package name differs from the last path segment
528
+ if actualName, err := getActualPackageName(goPath, c.pkg.Imports); err == nil {
529
+ impName = actualName
530
+ } else {
531
+ // Fallback to last segment of path if package not found in type information
532
+ pts := strings.Split(goPath, "/")
533
+ impName = pts[len(pts)-1]
534
+ }
522
535
  }
523
536
 
524
537
  // All Go package imports are mapped to the @goscript/ scope.
@@ -526,7 +539,7 @@ func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec) {
526
539
  // handwritten versions (in .goscript-assets) or transpiled versions (in goscript).
527
540
  var tsImportPath string
528
541
  if goPath == "github.com/aperturerobotics/goscript/builtin" {
529
- tsImportPath = "@goscript/builtin/builtin.js"
542
+ tsImportPath = "@goscript/builtin/index.js"
530
543
  } else {
531
544
  tsImportPath = "@goscript/" + goPath
532
545
  }