Skip to content

Commit

Permalink
Merge pull request #2 from clarkmcc/release-0.2
Browse files Browse the repository at this point in the history
Release v0.2.0 - Added more Typescript tags, optimized version registry
  • Loading branch information
clarkmcc committed Mar 27, 2021
2 parents 10fdc21 + 9e61e89 commit a5193e1
Show file tree
Hide file tree
Showing 24 changed files with 228 additions and 24 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,33 @@ output, err = typescript.TranspileString(script, nil, typescript.WithCompileOpti
```

### Custom Typescript Version
You can optionally specify which typescript version you want to compile using. These versions are based on the Git tags from the Typescript repository.
#### Default Registry
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.
```go
output, err := typescript.Transpile(reader, nil, typescript.WithVersion("v4.2.2"))
import _ "github.com/clarkmcc/go-typescript/versions/v4.2.2"

func main() {
output, err := typescript.Transpile(reader, nil, typescript.WithVersion("v4.2.2"))
}
```

#### Custom Registry
You may want to use a custom version registry rather than the default registry.

```go
import version "github.com/clarkmcc/go-typescript/versions/v4.2.2"

func main() {
registry := versions.NewRegistry()
registry.MustRegister("v4.2.3", version.Source)

output, err := typescript.TranspileString("let a:number = 10;", &typescript.Config{
TypescriptSource: program,
})
}
```

#### Custom Version
Need a different typescript version than the tags we support in this repo? No problem, you can load your own:

```go
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/clarkmcc/go-typescript/utils"
"github.com/clarkmcc/go-typescript/versions"
_ "github.com/clarkmcc/go-typescript/versions/v4.2.3"
"github.com/dop251/goja"
)

Expand Down
79 changes: 79 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package typescript

import (
"fmt"
"github.com/clarkmcc/go-typescript/versions"
_ "github.com/clarkmcc/go-typescript/versions/v3.8.3"
_ "github.com/clarkmcc/go-typescript/versions/v3.9.9"
_ "github.com/clarkmcc/go-typescript/versions/v4.1.2"
_ "github.com/clarkmcc/go-typescript/versions/v4.1.3"
_ "github.com/clarkmcc/go-typescript/versions/v4.1.4"
_ "github.com/clarkmcc/go-typescript/versions/v4.1.5"
_ "github.com/clarkmcc/go-typescript/versions/v4.2.2"
v423 "github.com/clarkmcc/go-typescript/versions/v4.2.3"
"github.com/stretchr/testify/require"
"testing"
)
Expand All @@ -13,3 +22,73 @@ func TestConfig_Initialize(t *testing.T) {
_, err = cfg.Runtime.RunString(fmt.Sprintf("%v('not a valid base64 string')", cfg.decoderName))
require.Error(t, err)
}

func TestVersionLoading(t *testing.T) {
t.Run("v3.8.3", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v3.8.3"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v3.9.9", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v3.9.9"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.1.2", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.1.2"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.1.3", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.1.3"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.1.4", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.1.4"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.1.5", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.1.5"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.2.2", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.2.2"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
t.Run("v4.2.3", func(t *testing.T) {
output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: versions.DefaultRegistry.MustGet("v4.2.3"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
})
}

func TestCustomRegistry(t *testing.T) {
registry := versions.NewRegistry()
registry.MustRegister("v4.2.3", v423.Source)

output, err := TranspileString("let a: number = 10;", &Config{
TypescriptSource: registry.MustGet("v4.2.3"),
})
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
}
1 change: 1 addition & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
_ "embed"
typescript "github.com/clarkmcc/go-typescript"
_ "github.com/clarkmcc/go-typescript/versions/v4.2.2"
"log"
)

Expand Down
4 changes: 4 additions & 0 deletions transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/dop251/goja"
"io"
"io/ioutil"
"strings"
Expand All @@ -26,6 +27,9 @@ func TranspileCtx(ctx context.Context, script io.Reader, cfg *Config, opts ...Op
if cfg == nil {
cfg = NewDefaultConfig()
}
if cfg.Runtime == nil {
cfg.Runtime = goja.New()
}
for _, fn := range opts {
fn(cfg)
}
Expand Down
8 changes: 8 additions & 0 deletions versions/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func (r *Registry) Register(tag string, source string) error {
return nil
}

// MustRegister calls Register and panics if we're unable to register the version.
func (r *Registry) MustRegister(tag string, source string) {
err := r.Register(tag, source)
if err != nil {
panic(err)
}
}

// Get attempts to return the typescript source for the specified tag if it exists, otherwise
// it returns an error with a list of typescript versions that are supported by this registry.
func (r *Registry) Get(tag string) (*goja.Program, error) {
Expand Down
1 change: 1 addition & 0 deletions versions/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func TestRegistry_Get(t *testing.T) {
t.Run("KnownTag", func(t *testing.T) {
DefaultRegistry.MustRegister("v4.2.3", "")
_, err := DefaultRegistry.Get("v4.2.3")
require.NoError(t, err)
})
Expand Down
13 changes: 13 additions & 0 deletions versions/v3.8.3/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v3_8_3

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v3.8.3.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v3.8.3", Source)
}
1 change: 1 addition & 0 deletions versions/v3.8.3/v3.8.3.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v3.9.9/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v3_9_9

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v3.9.9.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v3.9.9", Source)
}
1 change: 1 addition & 0 deletions versions/v3.9.9/v3.9.9.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v4.1.2/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_1_2

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.1.2.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.1.2", Source)
}
1 change: 1 addition & 0 deletions versions/v4.1.2/v4.1.2.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v4.1.3/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_1_3

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.1.3.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.1.3", Source)
}
1 change: 1 addition & 0 deletions versions/v4.1.3/v4.1.3.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v4.1.4/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_1_4

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.1.4.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.1.4", Source)
}
1 change: 1 addition & 0 deletions versions/v4.1.4/v4.1.4.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v4.1.5/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_1_5

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.1.5.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.1.5", Source)
}
1 change: 1 addition & 0 deletions versions/v4.1.5/v4.1.5.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions versions/v4.2.2/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_2_2

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.2.2.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.2.2", Source)
}
File renamed without changes.
13 changes: 13 additions & 0 deletions versions/v4.2.3/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v4_2_3

import (
_ "embed"
"github.com/clarkmcc/go-typescript/versions"
)

//go:embed v4.2.3.js
var Source string

func init() {
versions.DefaultRegistry.MustRegister("v4.2.3", Source)
}
File renamed without changes.
22 changes: 0 additions & 22 deletions versions/versions.go

This file was deleted.

0 comments on commit a5193e1

Please sign in to comment.