-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TypeScript and ES6+ support using esbuild.
With the introduction of the "enhanced" compatibility mode, the test source code is transformed using esbuild instead of Babel. Source files with the extension ".ts" are loaded by esbuild's TypeScript loader, which results in partial TypeScript support. This esbuild removes exactly the type information, but does not provide type safety. Source files other than ".ts" are loaded by esbuild's JavaScript loader, which results in the support of a more modern JavaScript dialect than goja.
- Loading branch information
Showing
121 changed files
with
88,121 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { User, newUser } from "./user.ts"; | ||
|
||
export default () => { | ||
const user: User = newUser("John"); | ||
console.log(user); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface User { | ||
name: string; | ||
id: number; | ||
} | ||
|
||
class UserAccount implements User { | ||
name: string; | ||
id: number; | ||
|
||
constructor(name: string) { | ||
this.name = name; | ||
this.id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); | ||
} | ||
} | ||
|
||
function newUser(name: string): User { | ||
return new UserAccount(name); | ||
} | ||
|
||
export { User, newUser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package compiler | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/dop251/goja/file" | ||
"github.com/dop251/goja/parser" | ||
"github.com/evanw/esbuild/pkg/api" | ||
) | ||
|
||
func esbuildTransform(src, filename string) (code string, srcMap []byte, err error) { | ||
opts := api.TransformOptions{ | ||
Sourcefile: filename, | ||
Loader: api.LoaderJS, | ||
Target: api.ES2017, | ||
Format: api.FormatCommonJS, | ||
Sourcemap: api.SourceMapExternal, | ||
SourcesContent: api.SourcesContentExclude, | ||
LegalComments: api.LegalCommentsNone, | ||
Platform: api.PlatformNeutral, | ||
LogLevel: api.LogLevelSilent, | ||
Charset: api.CharsetUTF8, | ||
} | ||
|
||
if filepath.Ext(filename) == ".ts" { | ||
opts.Loader = api.LoaderTS | ||
} | ||
|
||
result := api.Transform(src, opts) | ||
|
||
if hasError, err := esbuildCheckError(&result); hasError { | ||
return "", nil, err | ||
} | ||
|
||
return string(result.Code), result.Map, nil | ||
} | ||
|
||
func esbuildCheckError(result *api.TransformResult) (bool, error) { | ||
if len(result.Errors) == 0 { | ||
return false, nil | ||
} | ||
|
||
msg := result.Errors[0] | ||
err := &parser.Error{Message: msg.Text} | ||
|
||
if msg.Location != nil { | ||
err.Position = file.Position{ | ||
Filename: msg.Location.File, | ||
Line: msg.Location.Line, | ||
Column: msg.Location.Column, | ||
} | ||
} | ||
|
||
return true, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package compiler | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/dop251/goja/parser" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_esbuildTransform_js(t *testing.T) { | ||
t.Parallel() | ||
|
||
code, srcMap, err := esbuildTransform(`export default function(name) { return "Hello, " + name }`, "script.js") | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, srcMap) | ||
require.NotEmpty(t, code) | ||
} | ||
|
||
func Test_esbuildTransform_ts(t *testing.T) { | ||
t.Parallel() | ||
|
||
script := `export function hello(name:string) : string { return "Hello, " + name}` | ||
|
||
code, srcMap, err := esbuildTransform(script, "script.ts") | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, srcMap) | ||
require.NotEmpty(t, code) | ||
} | ||
|
||
func Test_esbuildTransform_error(t *testing.T) { | ||
t.Parallel() | ||
|
||
script := `export function hello(name:string) : string { return "Hello, " + name}` | ||
|
||
_, _, err := esbuildTransform(script, "script.js") | ||
|
||
require.Error(t, err) | ||
|
||
var perr *parser.Error | ||
|
||
require.True(t, errors.As(err, &perr)) | ||
require.NotNil(t, perr.Position) | ||
require.Equal(t, "script.js", perr.Position.Filename) | ||
require.Equal(t, 1, perr.Position.Line) | ||
require.Equal(t, 26, perr.Position.Column) | ||
require.Equal(t, "Expected \")\" but found \":\"", perr.Message) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
vendor/github.com/evanw/esbuild/internal/api_helpers/use_timer.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.