Skip to content
Merged
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ linters:
- wsl
- gomnd
- goerr113 # most of the errors here are meant for humans
- nlreturn
fast: false
5 changes: 3 additions & 2 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 "\<U+2029>"
_, _, 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")
}
})
})
}