Skip to content

Commit

Permalink
Fix for file with comments at end (grafana#853)
Browse files Browse the repository at this point in the history
Previously if a script was ending in a comment or babel transforms it in
a way that it ends in comment it might also not have new line. In this
case if the files is compiled as import it would fail because imports
get a closure around it which was added without newlines :(.
  • Loading branch information
mstoykov authored Nov 28, 2018
1 parent 87fb708 commit f7b8215
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func NewBundleFromArchive(arc *lib.Archive, rtOpts lib.RuntimeOptions) (*Bundle,
return nil, errors.Errorf("expected bundle type 'js', got '%s'", arc.Type)
}

initctx := NewInitContext(goja.New(), compiler, new(context.Context), arc.FS, arc.Pwd)
pgm, err := initctx.compileImport(string(arc.Data), arc.Filename)
pgm, _, err := compiler.Compile(string(arc.Data), arc.Filename, "", "", true)
if err != nil {
return nil, err
}
initctx := NewInitContext(goja.New(), compiler, new(context.Context), arc.FS, arc.Pwd)
initctx.files = arc.Files

env := arc.Env
Expand Down
2 changes: 1 addition & 1 deletion js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {
}

func (i *InitContext) compileImport(src, filename string) (*goja.Program, error) {
pgm, _, err := i.compiler.Compile(src, filename, "(function(){", "})()", true)
pgm, _, err := i.compiler.Compile(src, filename, "(function(){\n", "\n})()\n", true)
return pgm, err
}

Expand Down
2 changes: 1 addition & 1 deletion js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestInitContextRequire(t *testing.T) {
Filename: "/script.js",
Data: []byte(`import "/file.js"; export default function() {}`),
}, fs, lib.RuntimeOptions{})
assert.EqualError(t, err, "Error: aaaa at /file.js:1:19(4)")
assert.EqualError(t, err, "Error: aaaa at /file.js:2:7(4)")
})

imports := map[string]struct {
Expand Down
2 changes: 1 addition & 1 deletion js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestOptionsPropagationToScript(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expScriptOptions, r1.GetOptions())

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{Env: map[string]string{"expectedSetupTimeout": "1s"}})
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{Env: map[string]string{"expectedSetupTimeout": "3s"}})

require.NoError(t, err)
require.Equal(t, expScriptOptions, r2.GetOptions())
Expand Down
2 changes: 1 addition & 1 deletion release notes/upcoming.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Description of feature.

## Bugs fixed!

* Category: description of bug. (#PR)
* JS: Fix a babel transformation issue that caused closing brackets to sometimes be commented out. (#853)

0 comments on commit f7b8215

Please sign in to comment.