Skip to content

Commit 2318ccb

Browse files
committed
Overhauled README.md and added examples directory
1 parent 954fb5f commit 2318ccb

11 files changed

+176
-80
lines changed

README.md

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,18 @@
1-
# Goja Typescript Transpiler and Evaluator (with AMD module support)
2-
This package provides a simple interface using [github.com/dop251/goja](github.com/dop251/goja) under the hood to allow you to transpile Typescript to Javascript in Go. In addition it provides an evaluator with a built-in AMD module loader which allows you to run Typescript code against a compiled typescript bundle. This package has no direct dependencies besides testing utilities and has a 95% test coverage rate.
1+
![](assets/banner.png)
32

4-
Feel free to contribute. This package is fresh and may experience some changes before it's first tagged release.
3+
This package provides a thin wrapper around [goja](https://github.com/dop251/goja) (a native Javascript runtime for Go). There are no direct dependencies besides goja, and [testify](https://github.com/stretchr/testify) (for testing only). This package supports the following features:
4+
* Typescript compilation and evaluation.
5+
* A context-aware evaluation API to support cancellation.
6+
* AMD-style modules using the built-in [Almond module loader](https://github.com/requirejs/almond).
7+
* Custom Typescript version registration with built-in support for versions 3.8.3, 3.9.9, 4.1.2, 4.1.3, 4.1.4, 4.1.5, 4.2.2, 4.2.3, 4.2.4, and 4.7.2.
8+
* 90%+ test coverage
59

6-
## Transpiling Examples
7-
### Transpile Strings
8-
```go
9-
output, err := typescript.TranspileString("let a: number = 10;", nil)
10-
// output: var a = 10;
11-
```
10+
## Installation
1211

13-
### Transpile Reader
14-
```go
15-
output, err := typescript.Transpile(reader, nil)
16-
```
12+
go get github.com/clarkmcc/go-typescript
1713

18-
### Custom Typescript Compile Options
19-
You can optionally specify alternative compiler options that are used by Typescript. Any of the options [https://www.typescriptlang.org/docs/handbook/compiler-options.html](https://www.typescriptlang.org/docs/handbook/compiler-options.html) can be added.
20-
```go
21-
output, err = typescript.TranspileString(script, typescript.WithCompileOptions(map[string]interface{}{
22-
"module": "none",
23-
"strict": true,
24-
}))
25-
```
26-
27-
### Custom Typescript Version
28-
You can optionally specify which typescript version you want to compile using. These versions are based on the Git tags from the Typescript repository. If you're using a version that is supported in this package, you'll need to import the version package as a side-effect and will automatically be registered to the default registry.
29-
```go
30-
import _ "github.com/clarkmcc/go-typescript/versions/v4.2.2"
31-
32-
func main() {
33-
output, err := typescript.Transpile(reader, typescript.WithVersion("v4.2.2"))
34-
}
35-
```
36-
37-
### Custom Typescript Source
38-
You may want to use a custom typescript version.
39-
40-
```go
41-
func main() {
42-
output, err := typescript.TranspileString("let a:number = 10;",
43-
WithTypescriptSource("/* source code for typescript*/"))
44-
}
45-
```
46-
47-
## Evaluate Examples
48-
### Basic Evaluation
49-
You can evaluate pure Javascript code with:
50-
51-
```go
52-
result, err := Evaluate(strings.NewReader("var a = 10;")) // returns 10;
53-
```
54-
55-
### Transpile and Evaluate
56-
Or you can transpile first:
57-
58-
```go
59-
result, err := Evaluate(strings.NewReader("let a: number = 10;"), WithTranspile()) // returns 10;
60-
```
61-
62-
### Run Script with AMD Modules
63-
You can load in an AMD module bundle, then execute a Typescript script with access to the modules.
64-
65-
```go
66-
// This is the module we're going to import
67-
modules := strings.TrimSpace(`
68-
define("myModule", ["exports"], function (exports, core_1) {
69-
Object.defineProperty(exports, "__esModule", { value: true });
70-
exports.multiply = void 0;
71-
var multiply = function (a, b) { return a * b; };
72-
exports.multiply = multiply;
73-
});
74-
`)
75-
76-
// This is the script we're going to transpile and evaluate
77-
script := "import { multiply } from 'myModule'; multiply(5, 5)"
78-
79-
// Returns 25
80-
result, err := EvaluateCtx(context.Background(), strings.NewReader(script),
81-
WithAlmondModuleLoader(),
82-
WithTranspile(),
83-
WithEvaluateBefore(strings.NewReader(amdModuleScript)))
84-
```
14+
## Examples
15+
* [Transpile Typescript](examples/typescript_evaluate_test.go)
16+
* [Transpile and Evaluate Typescript](examples/typescript_evaluate_test.go)
17+
* [AMD Modules](examples/typescript_amd_modules_test.go)
18+
* [Context Cancellation](examples/typescript_context_test.go)

assets/banner.png

104 KB
Loading

evaluate_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"github.com/dop251/goja"
8+
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
"io"
1011
"strings"
@@ -94,6 +95,17 @@ func TestEvaluateCtx(t *testing.T) {
9495
}))
9596
require.NoError(t, err)
9697
})
98+
99+
t.Run("pre-transpile hook", func(t *testing.T) {
100+
s1 := "let a: number = 10"
101+
_, err := Evaluate(strings.NewReader(s1),
102+
WithTranspile(),
103+
WithScriptPreTranspileHook(func(s2 string) (string, error) {
104+
assert.Equal(t, s1, s2)
105+
return s2, nil
106+
}))
107+
assert.NoError(t, err)
108+
})
97109
}
98110

99111
var _ io.Reader = &failingReader{}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define("myModule", ["exports"], function (exports) {
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.multiply = void 0;
4+
var multiply = function (a, b) { return a * b; };
5+
exports.multiply = multiply;
6+
});

examples/typescript-example.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Person {
2+
name: string;
3+
4+
constructor(name: string) {
5+
this.name = name
6+
}
7+
8+
public greet(): string {
9+
return `Hello ${this.name}!`
10+
}
11+
}
12+
13+
const me = new Person("John Doe")
14+
me.greet()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package examples
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"github.com/clarkmcc/go-typescript"
7+
"strings"
8+
)
9+
10+
//go:embed javascript-example-module.js
11+
var module string
12+
13+
func ExampleTypescriptAMDModule() {
14+
result, err := typescript.Evaluate(strings.NewReader(`import { multiply } from 'myModule'; multiply(5, 5)`),
15+
typescript.WithTranspile(),
16+
typescript.WithAlmondModuleLoader(),
17+
typescript.WithEvaluateBefore(strings.NewReader(module)))
18+
if err != nil {
19+
panic(err)
20+
}
21+
fmt.Println(result.ToInteger())
22+
// Output:25
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"fmt"
7+
"github.com/clarkmcc/go-typescript"
8+
"strings"
9+
)
10+
11+
//go:embed typescript-example.ts
12+
var script3 string
13+
14+
func ExampleContext() {
15+
ctx, cancel := context.WithCancel(context.Background())
16+
cancel()
17+
18+
_, err := typescript.TranspileCtx(ctx, strings.NewReader(script3))
19+
if err == nil {
20+
panic("expected error")
21+
}
22+
fmt.Println(err)
23+
// Output:running typescript compiler: context halt at <eval>:1:1(0)
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package examples
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"github.com/clarkmcc/go-typescript"
7+
"strings"
8+
)
9+
10+
//go:embed typescript-example.ts
11+
var script2 string
12+
13+
func ExampleTypescriptEvaluate() {
14+
// Transpile the typescript and return evaluated result
15+
result, err := typescript.Evaluate(strings.NewReader(script2), typescript.WithTranspile())
16+
if err != nil {
17+
panic(err)
18+
}
19+
fmt.Println(result.String())
20+
// Output:Hello John Doe!
21+
}

examples/typescript_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package examples
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"github.com/clarkmcc/go-typescript"
7+
"strings"
8+
)
9+
10+
//go:embed typescript-example.ts
11+
var script1 string
12+
13+
var expected = `
14+
var Person = /** @class */ (function () {
15+
function Person(name) {
16+
this.name = name;
17+
}
18+
Person.prototype.greet = function () {
19+
return "Hello ".concat(this.name, "!");
20+
};
21+
return Person;
22+
}());
23+
var me = new Person("John Doe");
24+
me.greet();`
25+
26+
func ExampleTranspile() {
27+
// Only transpile the typescript and return transpiled Javascript, don't evaluate
28+
transpiled, err := typescript.TranspileString(script1)
29+
if err != nil {
30+
panic(err)
31+
}
32+
if transpiled != expected {
33+
panic("unexpected transpile result")
34+
}
35+
fmt.Println(strings.TrimSpace(transpiled))
36+
}

utils/random_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2022 Print Tracker, LLC - All Rights Reserved
2+
//
3+
// Unauthorized copying of this file, via any medium is strictly prohibited
4+
// as this source code is proprietary and confidential. Dissemination of this
5+
// information or reproduction of this material is strictly forbidden unless
6+
// prior written permission is obtained from Print Tracker, LLC.
7+
8+
package utils
9+
10+
import (
11+
"github.com/stretchr/testify/assert"
12+
"testing"
13+
)
14+
15+
func TestRandomString(t *testing.T) {
16+
s1 := RandomString()
17+
assert.Len(t, s1, 24)
18+
s2 := RandomString()
19+
assert.NotEqual(t, s1, s2)
20+
}

0 commit comments

Comments
 (0)