Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable typescript support by default #4200

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func runtimeOptionFlagSet(includeSysEnv bool) *pflag.FlagSet {
flags := pflag.NewFlagSet("", 0)
flags.SortFlags = false
flags.Bool("include-system-env-vars", includeSysEnv, "pass the real system environment variables to the runtime")
flags.String("compatibility-mode", "extended",
`JavaScript compiler compatibility mode, "extended" or "base" or "experimental_enhanced"
flags.String("compatibility-mode", "enhanced",
`JavaScript compiler compatibility mode, "extended" or "base" or "enhanced"
base: pure Sobek - Golang JS VM supporting ES6+
extended: base + sets "global" as alias for "globalThis"
experimental_enhanced: esbuild-based transpiling for TypeScript and ES6+ support
enhanced: esbuild-based transpiling for TypeScript and ES6+ support
`)
flags.StringP("type", "t", "", "override test type, \"js\" or \"archive\"")
flags.StringArrayP("env", "e", nil, "add/override environment variable with `VAR=value`")
Expand Down
8 changes: 4 additions & 4 deletions cmd/runtime_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func testRuntimeOptionsCase(t *testing.T, tc runtimeOptionsTestCase) {
func TestRuntimeOptions(t *testing.T) {
t.Parallel()
var (
defaultCompatMode = null.NewString("extended", false)
defaultCompatMode = null.NewString("enhanced", false)
baseCompatMode = null.NewString("base", true)
extendedCompatMode = null.NewString("extended", true)
enhancedCompatMode = null.NewString("experimental_enhanced", true)
enhancedCompatMode = null.NewString("enhanced", true)
defaultTracesOutput = null.NewString("none", false)
)

Expand Down Expand Up @@ -147,9 +147,9 @@ func TestRuntimeOptions(t *testing.T) {
TracesOutput: defaultTracesOutput,
},
},
"disabled sys env by default with experimental_enhanced compat mode": {
"disabled sys env by default with enhanced compat mode": {
useSysEnv: false,
systemEnv: map[string]string{"test1": "val1", "K6_COMPATIBILITY_MODE": "experimental_enhanced"},
systemEnv: map[string]string{"test1": "val1", "K6_COMPATIBILITY_MODE": "enhanced"},
expRTOpts: lib.RuntimeOptions{
IncludeSystemEnvVars: null.NewBool(false, false),
CompatibilityMode: enhancedCompatMode,
Expand Down
2 changes: 1 addition & 1 deletion cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ func TestTypeScriptSupport(t *testing.T) {
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "test.ts"), []byte(mainScript), 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "bar.ts"), []byte(depScript), 0o644))

ts.CmdArgs = []string{"k6", "run", "--compatibility-mode", "experimental_enhanced", "--quiet", "test.ts"}
ts.CmdArgs = []string{"k6", "run", "--compatibility-mode", "enhanced", "--quiet", "test.ts"}

cmd.ExecuteWithGlobalState(ts.GlobalState)

Expand Down
2 changes: 1 addition & 1 deletion js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestNewBundle(t *testing.T) {
}{
{
"InvalidCompat", "es1", `export default function() {};`,
`invalid compatibility mode "es1". Use: "extended", "base", "experimental_enhanced"`,
`invalid compatibility mode "es1". Use: "extended", "base", "enhanced"`,
},
}

Expand Down
2 changes: 1 addition & 1 deletion js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast
return prg, code, nil
}

if ps.compatibilityMode == lib.CompatibilityModeExperimentalEnhanced && strings.HasSuffix(filename, ".ts") {
if ps.compatibilityMode == lib.CompatibilityModeEnhanced && strings.HasSuffix(filename, ".ts") {
if err := ps.compiler.usage.Uint64(usageParsedTSFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedTSFilesKey)
}
Expand Down
14 changes: 7 additions & 7 deletions js/compiler/enhanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ func Test_esbuildTransform_ts(t *testing.T) {
require.NotEmpty(t, code)
}

func TestCompile_experimental_enhanced(t *testing.T) {
func TestCompile_enhanced(t *testing.T) {
t.Parallel()

t.Run("experimental_enhanced Invalid", func(t *testing.T) {
t.Run("enhanced Invalid", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
src := `1+(function() { return 2; )()`
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced
c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced
_, _, err := c.Parse(src, "script.ts", false, false)
assert.IsType(t, &parser.Error{}, err)
assert.Contains(t, err.Error(), `script.ts: Line 1:26 Unexpected ")"`)
})
t.Run("experimental_enhanced", func(t *testing.T) {
t.Run("enhanced", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced
c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced
prg, code, err := c.Parse(`let t :string = "something"; require(t);`, "script.ts", false, false)
require.NoError(t, err)
assert.Equal(t, `let t = "something";
Expand All @@ -67,10 +67,10 @@ require(t);
require.NoError(t, err)
require.True(t, requireCalled)
})
t.Run("experimental_enhanced sourcemap", func(t *testing.T) {
t.Run("enhanced sourcemap", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
c.Options.CompatibilityMode = lib.CompatibilityModeExperimentalEnhanced
c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced
c.Options.SourceMapLoader = func(_ string) ([]byte, error) { return nil, nil }
_, code, err := c.Parse(`let t :string = "something"; require(t);`, "script.ts", false, false)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions js/tc39/tc39_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func (ctx *tc39TestCtx) runFile(base, name string, vm *sobek.Runtime) error {

func (ctx *tc39TestCtx) compileOnly(src, name string, compatibilityMode lib.CompatibilityMode) (*sobek.Program, error) {
comp := ctx.compiler()
if compatibilityMode == lib.CompatibilityModeExperimentalEnhanced {
if compatibilityMode == lib.CompatibilityModeEnhanced {
code, _, err := compiler.StripTypes(src, name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -722,7 +722,7 @@ func (ctx *tc39TestCtx) runTC39Module(name, src string, includes []string, vm *s
func(specifier *url.URL, _ string) ([]byte, error) {
b, err := fs.ReadFile(os.DirFS("."), specifier.Path[1:])

if ctx.compatibilityMode == lib.CompatibilityModeExperimentalEnhanced {
if ctx.compatibilityMode == lib.CompatibilityModeEnhanced {
code, _, err := compiler.StripTypes(string(b), name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -797,7 +797,7 @@ func TestTC39(t *testing.T) {
}

runTestTC39(t, lib.CompatibilityModeExtended)
runTestTC39(t, lib.CompatibilityModeExperimentalEnhanced)
runTestTC39(t, lib.CompatibilityModeEnhanced)
}

func runTestTC39(t *testing.T, compatibilityMode lib.CompatibilityMode) {
Expand Down
6 changes: 3 additions & 3 deletions lib/compatibility_mode_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions lib/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (

// CompatibilityMode specifies the JS compatibility mode
//
// There are several CLIs called enumer, and several versions of them.
// The generated code can be produced exactly as follows:
//
// go run github.com/alvaroloes/[email protected] -type=CompatibilityMode -transform=snake \
// -trimprefix CompatibilityMode -output compatibility_mode_gen.go
//
//go:generate enumer -type=CompatibilityMode -transform=snake -trimprefix CompatibilityMode -output compatibility_mode_gen.go
type CompatibilityMode uint8

Expand All @@ -17,8 +23,8 @@ const (
CompatibilityModeExtended CompatibilityMode = iota + 1
// CompatibilityModeBase is standard Sobek, which means pure vanilla JS following ECMAScript standards.
CompatibilityModeBase
// CompatibilityModeExperimentalEnhanced achieves TypeScript and ES6+ compatibility with esbuild
CompatibilityModeExperimentalEnhanced
// CompatibilityModeEnhanced achieves TypeScript and ES6+ compatibility with esbuild
CompatibilityModeEnhanced
)

// RuntimeOptions are settings passed onto the Sobek JS runtime
Expand Down
Loading