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

Update babel to 7.11.6. #1663

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestNewBundle(t *testing.T) {
t.Run("Invalid", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", "\x00")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SyntaxError: file:///script.js: Unexpected character '\x00' (1:0)\n> 1 | \x00\n")
assert.Contains(t, err.Error(), "SyntaxError: /file:/script.js: Unexpected character '\x00' (1:0)")
})
t.Run("Error", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `throw new Error("aaaa");`)
Expand Down
24 changes: 20 additions & 4 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,35 @@ import (
rice "github.com/GeertJohan/go.rice"
"github.com/dop251/goja"
"github.com/dop251/goja/parser"
jslib "github.com/loadimpact/k6/js/lib"
"github.com/loadimpact/k6/lib"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"

"github.com/loadimpact/k6/lib"
)

var (
DefaultOpts = map[string]interface{}{
"presets": []string{"latest"},
// Babel wants a structure like [["env", {}]]
"presets": [][]interface{}{{"es2015", map[string]interface{}{"exclude": []string{
"transform-unicode-regex",
"transform-unicode-escapes",
"proposal-unicode-property-regex",
"transform-regenerator",
"transform-async-to-generator",
"proposal-dynamic-import",
"proposal-async-generator-functions",
}}}},
"ignore": []string{"/(node_modules|bower_components)/"},
// Potential perf gain
// https://babeljs.io/docs/en/options#cloneinputast
"cloneInputAst": false,
"ast": false,
"sourceMaps": false,
"babelrc": false,
"compact": false,
"retainLines": true,
"highlightCode": false,
}

once sync.Once // nolint:gochecknoglobals
globalBabel *babel // nolint:gochecknoglobals
)
Expand Down Expand Up @@ -110,6 +122,10 @@ func newBabel() (*babel, error) {
return
}

if _, corejsErr := vm.RunProgram(jslib.GetCoreJS()); corejsErr != nil {
return
}

this := vm.Get("Babel")
bObj := this.ToObject(vm)
globalBabel = &babel{vm: vm, this: this}
Expand Down
10 changes: 4 additions & 6 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestTransform(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, strings.Join([]string{
`"use strict";function add(a, b) {`,
` return a + b;`,
` return a + b;`,
`};`,
``,
`var res = add(1, 2);`,
Expand Down Expand Up @@ -109,8 +109,7 @@ func TestCompile(t *testing.T) {
src := `1+(function() { return 2; )()`
_, _, err := c.Compile(src, "script.js", "", "", true, lib.CompatibilityModeExtended)
assert.IsType(t, &goja.Exception{}, err)
assert.Contains(t, err.Error(), `SyntaxError: script.js: Unexpected token (1:26)
> 1 | 1+(function() { return 2; )()`)
assert.Contains(t, err.Error(), `SyntaxError: /script.js: Unexpected token (1:26)`)
})
})
t.Run("ES6", func(t *testing.T) {
Expand Down Expand Up @@ -146,10 +145,9 @@ func TestCompile(t *testing.T) {
})

t.Run("Invalid", func(t *testing.T) {
_, _, err := c.Compile(`1+(=>2)()`, "script.js", "", "", true, lib.CompatibilityModeExtended)
_, _, err := c.Compile(`1+(=>2)()`, "/script.js", "", "", true, lib.CompatibilityModeExtended)
assert.IsType(t, &goja.Exception{}, err)
assert.Contains(t, err.Error(), `SyntaxError: script.js: Unexpected token (1:3)
> 1 | 1+(=>2)()`)
assert.Contains(t, err.Error(), `SyntaxError: /script.js: Unexpected token (1:3)`)
})

t.Run("Invalid for goja but not babel", func(t *testing.T) {
Expand Down
26 changes: 1 addition & 25 deletions js/compiler/lib/babel.min.js

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions js/compiler/rice-box.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestInitContextRequire(t *testing.T) {
assert.NoError(t, afero.WriteFile(fs, "/file.js", []byte{0x00}, 0o755))
_, err := getSimpleBundle(t, "/script.js", `import "/file.js"; export default function() {}`, fs)
require.Error(t, err)
assert.Contains(t, err.Error(), "SyntaxError: file:///file.js: Unexpected character '\x00' (1:0)\n> 1 | \x00\n")
assert.Contains(t, err.Error(), "SyntaxError: /file:/file.js: Unexpected character '\x00' (1:0)")
})
t.Run("Error", func(t *testing.T) {
fs := afero.NewMemMapFs()
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRunES6String(t *testing.T) {
t.Run("Invalid", func(t *testing.T) {
_, err := runES6String(t, goja.New(), `let a = #;`)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SyntaxError: __string__: Unexpected character '#' (1:8)\n> 1 | let a = #;\n")
assert.Contains(t, err.Error(), "SyntaxError: /__string__: Unexpected token (1:8)")
})
}

Expand Down