goscript 0.0.4 → 0.0.6

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.
@@ -15,7 +15,7 @@ const args = process.argv.slice(2);
15
15
 
16
16
  // Construct the go run command with the absolute path to the goscript executable
17
17
  // Use path.join for robustness
18
- const goscriptCmd = `go run "${path.join(projectRoot, "cmd", "goscript")}"`;
18
+ const goscriptCmd = `go run ./cmd/goscript`;
19
19
 
20
20
  // Combine the goscript command with the arguments
21
21
  const command = `${goscriptCmd} ${args.join(" ")}`;
@@ -24,7 +24,7 @@ const command = `${goscriptCmd} ${args.join(" ")}`;
24
24
  const child = spawn(command, {
25
25
  shell: true, // Use shell to correctly parse the command string
26
26
  stdio: 'inherit', // Inherit stdin, stdout, and stderr
27
- cwd: process.cwd(), // Execute in the current working directory where the script is run
27
+ cwd: projectRoot, // Execute in the current working directory where the script is run
28
28
  });
29
29
 
30
30
  child.on('error', (error) => {
@@ -3,12 +3,11 @@ package compiler
3
3
  import (
4
4
  "fmt"
5
5
  "go/ast"
6
- "go/token" // Added token import for containsAsyncOperations
6
+ "go/token"
7
7
  "go/types"
8
8
 
9
+ gstypes "github.com/paralin/goscript/compiler/types"
9
10
  "golang.org/x/tools/go/packages"
10
-
11
- gstypes "github.com/paralin/goscript/types"
12
11
  )
13
12
 
14
13
  // GoToTSCompiler compiles Go code to TypeScript code.
@@ -8,7 +8,7 @@ import (
8
8
  gtypes "go/types"
9
9
  "strconv"
10
10
 
11
- gstypes "github.com/paralin/goscript/types"
11
+ gstypes "github.com/paralin/goscript/compiler/types"
12
12
  )
13
13
 
14
14
  // WriteTypeExpr writes an expression that represents a type.
@@ -7,7 +7,7 @@ import (
7
7
  gtypes "go/types"
8
8
  "strings"
9
9
 
10
- gstypes "github.com/paralin/goscript/types"
10
+ gstypes "github.com/paralin/goscript/compiler/types"
11
11
  "github.com/sanity-io/litter"
12
12
  "golang.org/x/tools/go/packages"
13
13
  )
@@ -0,0 +1,10 @@
1
+ package compiler
2
+
3
+ import (
4
+ "path/filepath"
5
+ )
6
+
7
+ // ComputeModulePath computes the root of the output typescript module.
8
+ func ComputeModulePath(outputRoot, goPkg string) string {
9
+ return filepath.Join(outputRoot, goPkg)
10
+ }
@@ -6,7 +6,6 @@ import (
6
6
  "os"
7
7
  "path/filepath"
8
8
 
9
- "github.com/paralin/goscript/output"
10
9
  "github.com/sirupsen/logrus"
11
10
  "golang.org/x/tools/go/packages"
12
11
  )
@@ -29,7 +28,7 @@ func NewPackageCompiler(
29
28
  le: le,
30
29
  pkg: pkg,
31
30
  compilerConf: compilerConf,
32
- outputPath: output.ComputeModulePath(compilerConf.OutputPathRoot, pkg.PkgPath),
31
+ outputPath: ComputeModulePath(compilerConf.OutputPathRoot, pkg.PkgPath),
33
32
  }
34
33
 
35
34
  return res, nil
@@ -0,0 +1,65 @@
1
+ package types
2
+
3
+ import (
4
+ "go/token"
5
+ )
6
+
7
+ var tokenMap = map[token.Token]string{
8
+ token.ADD: "+",
9
+ token.SUB: "-",
10
+ token.MUL: "*",
11
+ token.QUO: "/",
12
+ token.REM: "%",
13
+ token.OR: "|",
14
+ token.XOR: "^",
15
+ token.SHL: "<<",
16
+ token.SHR: ">>",
17
+
18
+ token.ADD_ASSIGN: "+=",
19
+ token.SUB_ASSIGN: "-=",
20
+ token.MUL_ASSIGN: "*=",
21
+ token.QUO_ASSIGN: "/=",
22
+ token.REM_ASSIGN: "%=",
23
+
24
+ token.AND_ASSIGN: "&=",
25
+ token.OR_ASSIGN: "|=",
26
+ token.XOR_ASSIGN: "^=", // TODO: check if this works
27
+ token.SHL_ASSIGN: "<<=",
28
+ token.SHR_ASSIGN: ">>=",
29
+ token.AND_NOT_ASSIGN: "&^=",
30
+
31
+ token.LAND: "&&",
32
+ token.LOR: "||",
33
+ // token.ARROW: ""
34
+ token.INC: "++",
35
+ token.DEC: "--",
36
+ token.EQL: "==",
37
+ token.LSS: "<",
38
+ token.GTR: ">",
39
+ token.ASSIGN: "=",
40
+ token.NOT: "!",
41
+
42
+ token.NEQ: "!=",
43
+ token.LEQ: "<=",
44
+ token.GEQ: ">=",
45
+ token.DEFINE: "=", // :=
46
+ token.ELLIPSIS: "...", // TODO
47
+
48
+ token.LPAREN: "(",
49
+ token.LBRACK: "[",
50
+ token.LBRACE: "{",
51
+ token.COMMA: ",",
52
+ token.PERIOD: ".",
53
+
54
+ token.RPAREN: ")",
55
+ token.RBRACK: "]",
56
+ token.RBRACE: "}",
57
+ token.SEMICOLON: ";",
58
+ token.COLON: ":",
59
+ }
60
+
61
+ // TokenToTs looks up the typescript version of a token.
62
+ func TokenToTs(tok token.Token) (string, bool) {
63
+ t, ok := tokenMap[tok]
64
+ return t, ok
65
+ }
@@ -0,0 +1,46 @@
1
+ package types
2
+
3
+ // goToTypescriptPrimitives maps Go primitive types to their TypeScript equivalents.
4
+ //
5
+ // Assumptions:
6
+ // - Target environment is similar to GOOS=js GOARCH=wasm, where `int` and `uint` are 32 bits.
7
+ // - 32-bit Go integers fit safely within the JS/TypeScript `number` type.
8
+ // - 64-bit integers (`int64`, `uint64`) require TypeScript `bigint` (ES2020+).
9
+ // - Only primitive types are handled here. Composite types (pointers, slices, maps, structs, etc.)
10
+ // are not handled by this mapping.
11
+ var goToTypescriptPrimitives = map[string]string{
12
+ // Boolean
13
+ "bool": "boolean",
14
+
15
+ // Strings
16
+ "string": "string",
17
+
18
+ // Signed Integers
19
+ "int": "number",
20
+ "int8": "number",
21
+ "int16": "number",
22
+ "int32": "number",
23
+ "rune": "number", // alias for int32
24
+ "int64": "bigint", // Requires TypeScript target >= ES2020
25
+
26
+ // Unsigned Integers
27
+ "uint": "number",
28
+ "uint8": "number", // byte is an alias for uint8
29
+ "byte": "number",
30
+ "uint16": "number",
31
+ "uint32": "number",
32
+ "uint64": "bigint", // Requires TypeScript target >= ES2020
33
+
34
+ // Floating Point Numbers
35
+ "float32": "number",
36
+ "float64": "number",
37
+ }
38
+
39
+ // GoBuiltinToTypescript returns the TypeScript equivalent of a Go primitive type name.
40
+ // Returns the TypeScript type and true if found, or an empty string and false otherwise.
41
+ //
42
+ // Only primitive types listed in goToTypescriptPrimitives are handled.
43
+ func GoBuiltinToTypescript(typeName string) (string, bool) {
44
+ val, ok := goToTypescriptPrimitives[typeName]
45
+ return val, ok
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goscript",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Go to TypeScript compiler",
5
5
  "bin": {
6
6
  "goscript": "./cmd/goscript/main.js"