goscript 0.0.80 → 0.0.81

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.
@@ -1515,7 +1515,7 @@ func (a *Analysis) addImportsForPromotedMethods(pkg *packages.Package) {
1515
1515
  if len(packagesToAdd) > 0 {
1516
1516
  fileImports := make(map[string]*fileImport)
1517
1517
  for pkgName, pkgObj := range packagesToAdd {
1518
- tsImportPath := "@goscript/" + pkgObj.Path()
1518
+ tsImportPath := translateGoImportPathToTypescriptModulePath(pkgObj.Path())
1519
1519
  fileImports[pkgName] = &fileImport{
1520
1520
  importPath: tsImportPath,
1521
1521
  importVars: make(map[string]struct{}),
@@ -474,11 +474,7 @@ func (c *PackageCompiler) generateIndexFile(compiledFiles []string) error {
474
474
  // Check if this is a protobuf file
475
475
  if strings.HasSuffix(fileName, ".pb") {
476
476
  // For protobuf files, add a simple re-export
477
- pbTsFileName := fileName + ".ts"
478
- if err := func() error {
479
- var _ string = pbTsFileName
480
- return c.writeProtobufExports(indexFile, fileName)
481
- }(); err != nil {
477
+ if err := c.writeProtobufExports(indexFile, fileName); err != nil {
482
478
  return err
483
479
  }
484
480
  continue
@@ -555,8 +551,8 @@ func (c *PackageCompiler) generateIndexFile(compiledFiles []string) error {
555
551
  // Write exports if this file has exported symbols
556
552
  if len(valueSymbols) > 0 {
557
553
  slices.Sort(valueSymbols)
558
- exportLine := fmt.Sprintf("export { %s } from \"./%s.js\"\n",
559
- strings.Join(valueSymbols, ", "), fileName)
554
+ exportLine := fmt.Sprintf("export { %s } from %q\n",
555
+ strings.Join(valueSymbols, ", "), translateGeneratedFileToImportPath(fileName))
560
556
  if _, err := indexFile.WriteString(exportLine); err != nil {
561
557
  return err
562
558
  }
@@ -566,8 +562,8 @@ func (c *PackageCompiler) generateIndexFile(compiledFiles []string) error {
566
562
  if len(structSymbols) > 0 {
567
563
  slices.Sort(structSymbols)
568
564
  // Export classes as values (which makes them available as both types and values in TypeScript)
569
- exportLine := fmt.Sprintf("export { %s } from \"./%s.js\"\n",
570
- strings.Join(structSymbols, ", "), fileName)
565
+ exportLine := fmt.Sprintf("export { %s } from %q\n",
566
+ strings.Join(structSymbols, ", "), translateGeneratedFileToImportPath(fileName))
571
567
  if _, err := indexFile.WriteString(exportLine); err != nil {
572
568
  return err
573
569
  }
@@ -575,8 +571,8 @@ func (c *PackageCompiler) generateIndexFile(compiledFiles []string) error {
575
571
 
576
572
  if len(typeSymbols) > 0 {
577
573
  slices.Sort(typeSymbols)
578
- exportLine := fmt.Sprintf("export type { %s } from \"./%s.js\"\n",
579
- strings.Join(typeSymbols, ", "), fileName)
574
+ exportLine := fmt.Sprintf("export type { %s } from %q\n",
575
+ strings.Join(typeSymbols, ", "), translateGeneratedFileToImportPath(fileName))
580
576
  if _, err := indexFile.WriteString(exportLine); err != nil {
581
577
  return err
582
578
  }
@@ -664,7 +660,10 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
664
660
  goWriter := NewGoToTSCompiler(c.codeWriter, c.pkg, c.Analysis, c.fullPath)
665
661
 
666
662
  // Add import for the goscript runtime using namespace import and alias
667
- c.codeWriter.WriteLinef("import * as $ from %q", "@goscript/builtin/index.js")
663
+ c.codeWriter.WriteLinef(
664
+ "import * as $ from %q",
665
+ translateTypescriptModulePathToIndexImportPath("@goscript/builtin"),
666
+ )
668
667
 
669
668
  // Check if there are any .pb.go files in this package and add imports for them
670
669
  if err := c.addProtobufImports(); err != nil {
@@ -691,8 +690,8 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
691
690
  }
692
691
  // Sort functions for consistent output
693
692
  slices.Sort(sanitizedFunctions)
694
- c.codeWriter.WriteLinef("import { %s } from \"./%s.gs.js\";",
695
- strings.Join(sanitizedFunctions, ", "), sourceFile)
693
+ c.codeWriter.WriteLinef("import { %s } from %q;",
694
+ strings.Join(sanitizedFunctions, ", "), translateGeneratedGoFileToImportPath(sourceFile))
696
695
  }
697
696
  }
698
697
  }
@@ -709,7 +708,7 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
709
708
  for _, sourceFile := range sourceFiles {
710
709
  typeImports := typeImports[sourceFile]
711
710
  if len(typeImports) > 0 {
712
- // Filter out protobuf types - they should be imported from .pb.js files, not .gs.js files
711
+ // Filter out protobuf types - they should be imported from .pb.ts files, not .gs.ts files
713
712
  var nonProtobufTypes []string
714
713
  for _, typeName := range typeImports {
715
714
  // Check if this type is a protobuf type by looking at its type info
@@ -736,8 +735,8 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
736
735
  }
737
736
  // Sort types for consistent output
738
737
  slices.Sort(sanitizedTypes)
739
- c.codeWriter.WriteLinef("import { %s } from \"./%s.gs.js\";",
740
- strings.Join(sanitizedTypes, ", "), sourceFile)
738
+ c.codeWriter.WriteLinef("import { %s } from %q;",
739
+ strings.Join(sanitizedTypes, ", "), translateGeneratedGoFileToImportPath(sourceFile))
741
740
  }
742
741
  }
743
742
  }
@@ -762,8 +761,8 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
762
761
  }
763
762
  // Sort variables for consistent output
764
763
  slices.Sort(sanitizedVariables)
765
- c.codeWriter.WriteLinef("import { %s } from \"./%s.gs.js\";",
766
- strings.Join(sanitizedVariables, ", "), sourceFile)
764
+ c.codeWriter.WriteLinef("import { %s } from %q;",
765
+ strings.Join(sanitizedVariables, ", "), translateGeneratedGoFileToImportPath(sourceFile))
767
766
  }
768
767
  }
769
768
  }
@@ -779,7 +778,7 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
779
778
  slices.Sort(syntheticPkgNames)
780
779
  for _, pkgName := range syntheticPkgNames {
781
780
  imp := syntheticImports[pkgName]
782
- c.codeWriter.WriteImport(pkgName, imp.importPath+"/index.js")
781
+ c.codeWriter.WriteImport(pkgName, translateTypescriptModulePathToIndexImportPath(imp.importPath))
783
782
  }
784
783
  }
785
784
 
package/compiler/decl.go CHANGED
@@ -574,7 +574,7 @@ func (c *GoToTSCompiler) writeLinknameFunction(decl *ast.FuncDecl, info *linknam
574
574
  func (c *GoToTSCompiler) findImportAlias(pkgPath string) string {
575
575
  // The importPath in analysis.Imports is stored as the TypeScript path (e.g., @goscript/pkg/path)
576
576
  // We need to convert the Go package path to match
577
- tsPath := "@goscript/" + pkgPath
577
+ tsPath := translateGoImportPathToTypescriptModulePath(pkgPath)
578
578
 
579
579
  for alias, imp := range c.analysis.Imports {
580
580
  if imp.importPath == tsPath {
@@ -5,6 +5,7 @@ import (
5
5
  "os"
6
6
  "path/filepath"
7
7
  "slices"
8
+ "strings"
8
9
  "testing"
9
10
 
10
11
  "github.com/sirupsen/logrus"
@@ -171,3 +172,127 @@ func TestCompilePackagesCopiesHandwrittenDependencies(t *testing.T) {
171
172
  t.Fatalf("Expected handwritten dependency at %s: %v", cmpPath, err)
172
173
  }
173
174
  }
175
+
176
+ func TestCompilePackagesEmitTypeScriptImportSpecifiers(t *testing.T) {
177
+ tempDir, err := os.MkdirTemp("", "goscript-ts-imports")
178
+ if err != nil {
179
+ t.Fatalf("Failed to create temp dir: %v", err)
180
+ }
181
+ defer os.RemoveAll(tempDir)
182
+
183
+ wd, err := os.Getwd()
184
+ if err != nil {
185
+ t.Fatalf("Failed to get working directory: %v", err)
186
+ }
187
+
188
+ config := &Config{
189
+ OutputPath: tempDir,
190
+ Dir: filepath.Dir(wd),
191
+ AllDependencies: true,
192
+ DisableEmitBuiltin: false,
193
+ }
194
+
195
+ logger := logrus.New()
196
+ logger.SetLevel(logrus.WarnLevel)
197
+ le := logrus.NewEntry(logger)
198
+
199
+ comp, err := NewCompiler(config, le, nil)
200
+ if err != nil {
201
+ t.Fatalf("Failed to create compiler: %v", err)
202
+ }
203
+
204
+ _, err = comp.CompilePackages(
205
+ context.Background(),
206
+ "github.com/aperturerobotics/goscript/tests/tests/package_import",
207
+ "github.com/aperturerobotics/goscript/tests/tests/map_value_field_access_cross_file",
208
+ )
209
+ if err != nil {
210
+ t.Fatalf("Compilation failed: %v", err)
211
+ }
212
+
213
+ assertFileContains(t,
214
+ filepath.Join(
215
+ tempDir,
216
+ "@goscript",
217
+ "github.com",
218
+ "aperturerobotics",
219
+ "goscript",
220
+ "tests",
221
+ "tests",
222
+ "package_import",
223
+ "package_import.gs.ts",
224
+ ),
225
+ []string{
226
+ `@goscript/builtin/index.ts`,
227
+ `@goscript/github.com/aperturerobotics/goscript/tests/tests/package_import/subpkg/index.ts`,
228
+ },
229
+ []string{
230
+ ".js",
231
+ },
232
+ )
233
+
234
+ assertFileContains(t,
235
+ filepath.Join(
236
+ tempDir,
237
+ "@goscript",
238
+ "github.com",
239
+ "aperturerobotics",
240
+ "goscript",
241
+ "tests",
242
+ "tests",
243
+ "package_import",
244
+ "subpkg",
245
+ "index.ts",
246
+ ),
247
+ []string{
248
+ `"./subpkg.gs.ts"`,
249
+ },
250
+ []string{
251
+ ".gs.js",
252
+ },
253
+ )
254
+
255
+ assertFileContains(t,
256
+ filepath.Join(
257
+ tempDir,
258
+ "@goscript",
259
+ "github.com",
260
+ "aperturerobotics",
261
+ "goscript",
262
+ "tests",
263
+ "tests",
264
+ "map_value_field_access_cross_file",
265
+ "read.gs.ts",
266
+ ),
267
+ []string{
268
+ `"./types.gs.ts"`,
269
+ `@goscript/builtin/index.ts`,
270
+ },
271
+ []string{
272
+ ".gs.js",
273
+ "index.js",
274
+ },
275
+ )
276
+ }
277
+
278
+ func assertFileContains(t *testing.T, path string, want []string, wantAbsent []string) {
279
+ t.Helper()
280
+
281
+ data, err := os.ReadFile(path)
282
+ if err != nil {
283
+ t.Fatalf("Failed to read %s: %v", path, err)
284
+ }
285
+ text := string(data)
286
+
287
+ for _, s := range want {
288
+ if !strings.Contains(text, s) {
289
+ t.Fatalf("Expected %s to contain %q\n%s", path, s, text)
290
+ }
291
+ }
292
+
293
+ for _, s := range wantAbsent {
294
+ if strings.Contains(text, s) {
295
+ t.Fatalf("Expected %s to omit %q\n%s", path, s, text)
296
+ }
297
+ }
298
+ }
@@ -15,6 +15,8 @@ func ComputeModulePath(outputRoot, goPkg string) string {
15
15
 
16
16
  var typeScriptGoStubPrefix = "@ts/"
17
17
 
18
+ const builtinGoImportPath = "github.com/aperturerobotics/goscript/builtin"
19
+
18
20
  // translateGoPathToTypescriptPath translates a go package import path to a typescript import path.
19
21
  func translateGoPathToTypescriptPath(goImportPath string) string {
20
22
  if strings.HasPrefix(goImportPath, typeScriptGoStubPrefix) {
@@ -23,6 +25,34 @@ func translateGoPathToTypescriptPath(goImportPath string) string {
23
25
  return fmt.Sprintf("@goscript/%s", goImportPath)
24
26
  }
25
27
 
28
+ // translateGoImportPathToTypescriptModulePath maps a Go import path to a TypeScript module path.
29
+ func translateGoImportPathToTypescriptModulePath(goImportPath string) string {
30
+ if goImportPath == builtinGoImportPath {
31
+ return "@goscript/builtin"
32
+ }
33
+ return translateGoPathToTypescriptPath(goImportPath)
34
+ }
35
+
36
+ // translateTypescriptModulePathToIndexImportPath maps a TypeScript module path to its index.ts import path.
37
+ func translateTypescriptModulePathToIndexImportPath(modulePath string) string {
38
+ return modulePath + "/index.ts"
39
+ }
40
+
41
+ // translateGeneratedFileToImportPath maps a generated file stem to its .ts import path.
42
+ func translateGeneratedFileToImportPath(fileName string) string {
43
+ return fmt.Sprintf("./%s.ts", fileName)
44
+ }
45
+
46
+ // translateGeneratedGoFileToImportPath maps a generated Go file stem to its .gs.ts import path.
47
+ func translateGeneratedGoFileToImportPath(fileName string) string {
48
+ return fmt.Sprintf("./%s.gs.ts", fileName)
49
+ }
50
+
51
+ // translateGeneratedProtobufFileToImportPath maps a protobuf file stem to its .pb.ts import path.
52
+ func translateGeneratedProtobufFileToImportPath(fileName string) string {
53
+ return fmt.Sprintf("./%s.pb.ts", fileName)
54
+ }
55
+
26
56
  // getActualPackageName returns the actual Go package name from package information.
27
57
  // If the package is not found in the imports map, returns an error instead of falling back.
28
58
  // This handles cases where the package name differs from the last segment of the import path.
@@ -294,7 +294,8 @@ func (c *FileCompiler) addProtobufImports() error {
294
294
  }
295
295
  }
296
296
 
297
- c.codeWriter.WriteLinef("import { %s } from \"./%s.pb.js\";", strings.Join(uniq, ", "), pbBaseName)
297
+ c.codeWriter.WriteLinef("import { %s } from %q;",
298
+ strings.Join(uniq, ", "), translateGeneratedProtobufFileToImportPath(pbBaseName))
298
299
  break
299
300
  }
300
301
  }
package/compiler/spec.go CHANGED
@@ -650,12 +650,7 @@ func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec) {
650
650
  // All Go package imports are mapped to the @goscript/ scope.
651
651
  // The TypeScript compiler will resolve these using tsconfig paths to either
652
652
  // handwritten versions (in .goscript-assets) or transpiled versions (in goscript).
653
- var tsImportPath string
654
- if goPath == "github.com/aperturerobotics/goscript/builtin" {
655
- tsImportPath = "@goscript/builtin/index.js"
656
- } else {
657
- tsImportPath = "@goscript/" + goPath
658
- }
653
+ tsImportPath := translateGoImportPathToTypescriptModulePath(goPath)
659
654
 
660
655
  c.analysis.Imports[impName] = &fileImport{
661
656
  importPath: tsImportPath,
@@ -671,7 +666,7 @@ func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec) {
671
666
  }
672
667
  }
673
668
 
674
- c.tsw.WriteImport(impName, tsImportPath+"/index.js")
669
+ c.tsw.WriteImport(impName, translateTypescriptModulePathToIndexImportPath(tsImportPath))
675
670
  }
676
671
 
677
672
  func (c *GoToTSCompiler) writeClonedFieldInitializer(fieldName string, fieldType types.Type, isEmbedded bool) {
@@ -110,7 +110,10 @@ func (c *FileCompiler) CompileToWriter(w io.Writer) error {
110
110
  goWriter := NewGoToTSCompiler(c.codeWriter, c.pkg, c.Analysis, c.fullPath)
111
111
 
112
112
  // Add import for the goscript runtime using namespace import and alias
113
- c.codeWriter.WriteLinef("import * as $ from %q", "@goscript/builtin/index.js")
113
+ c.codeWriter.WriteLinef(
114
+ "import * as $ from %q",
115
+ translateTypescriptModulePathToIndexImportPath("@goscript/builtin"),
116
+ )
114
117
 
115
118
  c.codeWriter.WriteLine("") // Add a newline after imports
116
119
 
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.80",
4
+ "version": "0.0.81",
5
5
  "author": {
6
6
  "name": "Aperture Robotics LLC.",
7
7
  "email": "support@aperture.us",