-
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.
Merge pull request #3738 from grafana/feature/3703-typescript-support
TypeScript and ES6+ support using esbuild.
- Loading branch information
Showing
126 changed files
with
88,394 additions
and
48 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,5 @@ | ||
import exec from "k6/execution"; | ||
|
||
export default function () { | ||
exec.test.abort("failed"); | ||
} |
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.ESNext, | ||
Format: api.FormatCommonJS, | ||
Sourcemap: api.SourceMapExternal, | ||
SourcesContent: api.SourcesContentInclude, | ||
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,97 @@ | ||
package compiler | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/dop251/goja" | ||
"github.com/dop251/goja/parser" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.k6.io/k6/lib" | ||
"go.k6.io/k6/lib/testutils" | ||
) | ||
|
||
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) | ||
} | ||
|
||
func TestCompile_experimental_enhanced(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("experimental_enhanced Invalid", func(t *testing.T) { | ||
t.Parallel() | ||
c := New(testutils.NewLogger(t)) | ||
src := `1+(function() { return 2; )()` | ||
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced | ||
_, _, err := c.Compile(src, "script.js", false) | ||
assert.IsType(t, &parser.Error{}, err) | ||
assert.Contains(t, err.Error(), `script.js: Line 1:26 Unexpected ")"`) | ||
}) | ||
t.Run("experimental_enhanced", func(t *testing.T) { | ||
t.Parallel() | ||
c := New(testutils.NewLogger(t)) | ||
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced | ||
pgm, code, err := c.Compile(`import "something"`, "script.js", true) | ||
require.NoError(t, err) | ||
assert.Equal(t, `var import_something = require("something"); | ||
`, code) | ||
rt := goja.New() | ||
var requireCalled bool | ||
require.NoError(t, rt.Set("require", func(s string) { | ||
assert.Equal(t, "something", s) | ||
requireCalled = true | ||
})) | ||
_, err = rt.RunProgram(pgm) | ||
require.NoError(t, err) | ||
require.True(t, requireCalled) | ||
}) | ||
t.Run("experimental_enhanced sourcemap", func(t *testing.T) { | ||
t.Parallel() | ||
c := New(testutils.NewLogger(t)) | ||
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced | ||
c.Options.SourceMapLoader = func(_ string) ([]byte, error) { return nil, nil } | ||
_, code, err := c.Compile(`import "something"`, "script.js", true) | ||
require.NoError(t, err) | ||
assert.Equal(t, `var import_something = require("something"); | ||
//# sourceMappingURL=k6://internal-should-not-leak/file.map`, code) | ||
}) | ||
} |
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
Oops, something went wrong.