diff --git a/js/compiler/compiler_test.go b/js/compiler/compiler_test.go index 6f8ae53b782..30ee33742be 100644 --- a/js/compiler/compiler_test.go +++ b/js/compiler/compiler_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/dop251/goja" + "github.com/dop251/goja/parser" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -158,6 +159,34 @@ func TestCompile(t *testing.T) { assert.Contains(t, err.Error(), `SyntaxError: script.js: Unexpected token (1:3) > 1 | 1+(=>2)()`) }) + + t.Run("enhanced Invalid", func(t *testing.T) { + t.Parallel() + c := New(testutils.NewLogger(t)) + src := `1+(function() { return 2; )()` + c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced + _, _, 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("enhanced", func(t *testing.T) { + t.Parallel() + c := New(testutils.NewLogger(t)) + c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced + 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) + }) } func TestCorruptSourceMap(t *testing.T) {