Skip to content

Commit

Permalink
Merge pull request #2109 from grafana/updateGoja
Browse files Browse the repository at this point in the history
Update goja to support destructuring and arrow functions
  • Loading branch information
mstoykov authored Aug 9, 2021
2 parents 606992e + 7f94c91 commit cb6295a
Show file tree
Hide file tree
Showing 38 changed files with 3,434 additions and 1,754 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/PuerkitoBio/goquery v1.6.1
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5
github.com/andybalholm/brotli v1.0.3
github.com/dop251/goja v0.0.0-20210712101704-705acef95ba3
github.com/dop251/goja v0.0.0-20210804101310-32956a348b49
github.com/fatih/color v1.12.0
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/golang/protobuf v1.4.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 h1:Izz0+t1Z5nI16/II7vuEo/nHjodOg0p7+OiDpjX5t1E=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dop251/goja v0.0.0-20210712101704-705acef95ba3 h1:9ILKyhZZQ3D/7Hk5io+HA7HVg9mgGn0gocu6+n6kNgQ=
github.com/dop251/goja v0.0.0-20210712101704-705acef95ba3/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja v0.0.0-20210804101310-32956a348b49 h1:CtSi0QlA2Hy+nOh8JAZoiEBLW5pliAiKJ3l1Iq1472I=
github.com/dop251/goja v0.0.0-20210804101310-32956a348b49/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
6 changes: 0 additions & 6 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ func TestNewBundle(t *testing.T) {
"Modules", "base", `export default function() {};`,
"file:///script.js: Line 1:1 Unexpected reserved word",
},
// Arrow functions are not supported
{
"ArrowFuncs", "base",
`module.exports.default = function() {}; () => {};`,
"file:///script.js: Line 1:42 Unexpected token ) (and 1 more errors)",
},
// Promises are not supported
{
"Promise", "base",
Expand Down
14 changes: 7 additions & 7 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ var (
// es2015 https://github.com/babel/babel/blob/v6.26.0/packages/babel-preset-es2015/src/index.js
[]interface{}{"transform-es2015-template-literals", map[string]interface{}{"loose": false, "spec": false}},
"transform-es2015-literals",
"transform-es2015-function-name",
[]interface{}{"transform-es2015-arrow-functions", map[string]interface{}{"spec": false}},
// "transform-es2015-function-name", // in goja
// []interface{}{"transform-es2015-arrow-functions", map[string]interface{}{"spec": false}}, // in goja
// "transform-es2015-block-scoped-functions", // in goja
[]interface{}{"transform-es2015-classes", map[string]interface{}{"loose": false}},
"transform-es2015-object-super",
// "transform-es2015-shorthand-properties", // in goja
"transform-es2015-duplicate-keys",
[]interface{}{"transform-es2015-computed-properties", map[string]interface{}{"loose": false}},
// "transform-es2015-duplicate-keys", // in goja
// []interface{}{"transform-es2015-computed-properties", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-for-of", // in goja
// "transform-es2015-sticky-regex", // in goja
// "transform-es2015-unicode-regex", // in goja
// "check-es2015-constants", // in goja
[]interface{}{"transform-es2015-spread", map[string]interface{}{"loose": false}},
"transform-es2015-parameters",
[]interface{}{"transform-es2015-destructuring", map[string]interface{}{"loose": false}},
// []interface{}{"transform-es2015-spread", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-parameters", // in goja
// []interface{}{"transform-es2015-destructuring", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-block-scoping", // in goja
// "transform-es2015-typeof-symbol", // in goja
// all the other module plugins are just dropped
Expand Down
14 changes: 7 additions & 7 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestTransform(t *testing.T) {
t.Run("double-arrow", func(t *testing.T) {
src, _, err := c.Transform("()=> true", "test.js")
assert.NoError(t, err)
assert.Equal(t, `"use strict";(function () {return true;});`, src)
assert.Equal(t, `"use strict";() => true;`, src)
// assert.Equal(t, 3, srcmap.Version)
// assert.Equal(t, "test.js", srcmap.File)
// assert.Equal(t, "aAAA,qBAAK,IAAL", srcmap.Mappings)
Expand Down Expand Up @@ -111,18 +111,18 @@ func TestCompile(t *testing.T) {
})
})
t.Run("ES6", func(t *testing.T) {
pgm, code, err := c.Compile(`1+(()=>2)()`, "script.js", "", "", true, lib.CompatibilityModeExtended)
pgm, code, err := c.Compile(`3**2`, "script.js", "", "", true, lib.CompatibilityModeExtended)
require.NoError(t, err)
assert.Equal(t, `"use strict";1 + function () {return 2;}();`, code)
assert.Equal(t, `"use strict";Math.pow(3, 2);`, code)
v, err := goja.New().RunProgram(pgm)
if assert.NoError(t, err) {
assert.Equal(t, int64(3), v.Export())
assert.Equal(t, int64(9), v.Export())
}

t.Run("Wrap", func(t *testing.T) {
pgm, code, err := c.Compile(`fn(1+(()=>2)())`, "script.js", "(function(fn){", "})", true, lib.CompatibilityModeExtended)
pgm, code, err := c.Compile(`fn(3**2)`, "script.js", "(function(fn){", "})", true, lib.CompatibilityModeExtended)
require.NoError(t, err)
assert.Equal(t, `(function(fn){"use strict";fn(1 + function () {return 2;}());})`, code)
assert.Equal(t, `(function(fn){"use strict";fn(Math.pow(3, 2));})`, code)
rt := goja.New()
v, err := rt.RunProgram(pgm)
if assert.NoError(t, err) {
Expand All @@ -133,7 +133,7 @@ func TestCompile(t *testing.T) {
out = v.Export()
}))
assert.NoError(t, err)
assert.Equal(t, int64(3), out)
assert.Equal(t, int64(9), out)
}
}
})
Expand Down
Loading

0 comments on commit cb6295a

Please sign in to comment.