diff --git a/.golangci.yml b/.golangci.yml index 6deb24bc8e..1739f39e76 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -60,4 +60,5 @@ linters: - wsl - gomnd - goerr113 # most of the errors here are meant for humans + - nlreturn fast: false diff --git a/js/compiler/compiler.go b/js/compiler/compiler.go index 719ee33efb..f72de6a817 100644 --- a/js/compiler/compiler.go +++ b/js/compiler/compiler.go @@ -70,7 +70,7 @@ func (c *Compiler) Transform(src, filename string) (code string, srcmap *SourceM return b.Transform(c.logger, src, filename) } -// Compile the program in the given CompatibilityMode, optionally running pre and post code. +// Compile the program in the given CompatibilityMode, wrapping it between pre and post code func (c *Compiler) Compile(src, filename, pre, post string, strict bool, compatMode lib.CompatibilityMode) (*goja.Program, string, error) { code := pre + src + post @@ -81,7 +81,8 @@ func (c *Compiler) Compile(src, filename, pre, post string, if err != nil { return nil, code, err } - return c.Compile(code, filename, pre, post, strict, compatMode) + // the compatibility mode "decreases" here as we shouldn't transform twice + return c.Compile(code, filename, pre, post, strict, lib.CompatibilityModeBase) } return nil, code, err } diff --git a/js/compiler/compiler_test.go b/js/compiler/compiler_test.go index 44a7cce49f..f00e8e5208 100644 --- a/js/compiler/compiler_test.go +++ b/js/compiler/compiler_test.go @@ -22,8 +22,10 @@ package compiler import ( "strings" "testing" + "time" "github.com/dop251/goja" + "github.com/dop251/goja/parser" "github.com/stretchr/testify/assert" "github.com/loadimpact/k6/lib" @@ -149,5 +151,26 @@ func TestCompile(t *testing.T) { assert.Contains(t, err.Error(), `SyntaxError: script.js: Unexpected token (1:3) > 1 | 1+(=>2)()`) }) + + t.Run("Invalid for goja but not babel", func(t *testing.T) { + ch := make(chan struct{}) + go func() { + defer close(ch) + // This is a string with U+2029 Paragraph separator in it + // the important part is that goja won't parse it but babel will transform it but still + // goja won't be able to parse the result it is actually "\" + _, _, err := c.Compile(string([]byte{0x22, 0x5c, 0xe2, 0x80, 0xa9, 0x22}), "script.js", "", "", true, lib.CompatibilityModeExtended) + assert.IsType(t, parser.ErrorList{}, err) + assert.Contains(t, err.Error(), ` Unexpected token ILLEGAL`) + }() + + select { + case <-ch: + // everything is fine + case <-time.After(time.Second): + // it took too long + t.Fatal("takes too long") + } + }) }) }