Skip to content

Commit

Permalink
Merge pull request robertkrimen#192 from deoxxa/support-inline-source…
Browse files Browse the repository at this point in the history
…-maps

add support for inline source maps
  • Loading branch information
deoxxa committed May 11, 2016
2 parents c562e46 + 99d478d commit ef5bb30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package parser

import (
"bytes"
"encoding/base64"
"errors"
"io"
"io/ioutil"
Expand Down Expand Up @@ -161,6 +162,19 @@ func ParseFileWithSourceMap(fileSet *file.FileSet, filename string, javascriptSo
return nil, err
}

if sourcemapSource == nil {
lines := bytes.Split(src, []byte("\n"))
lastLine := lines[len(lines)-1]
if bytes.HasPrefix(lastLine, []byte("//# sourceMappingURL=data:application/json")) {
bits := bytes.SplitN(lastLine, []byte(","), 2)
if len(bits) == 2 {
if d, err := base64.StdEncoding.DecodeString(string(bits[1])); err == nil {
sourcemapSource = d
}
}
}
}

sm, err := ReadSourceMap(filename, sourcemapSource)
if err != nil {
return nil, err
Expand Down
23 changes: 23 additions & 0 deletions sourcemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
testSourcemapCodeOriginal = "function functionA(argA, argB) {\n functionB(argA, argB);\n}\n\nfunction functionB(argA, argB) {\n functionExternal(argA, argB);\n}"
testSourcemapCodeMangled = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}"
testSourcemapContent = `{"version":3,"sources":["hello.js"],"names":["functionA","argA","argB","functionB","functionExternal"],"mappings":"AAAA,QAASA,WAAUC,KAAMC,MACvBC,UAAUF,KAAMC,MAGlB,QAASC,WAAUF,KAAMC,MACvBE,iBAAiBH,KAAMC"}`
testSourcemapInline = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImhlbGxvLmpzIl0sIm5hbWVzIjpbImZ1bmN0aW9uQSIsImFyZ0EiLCJhcmdCIiwiZnVuY3Rpb25CIiwiZnVuY3Rpb25FeHRlcm5hbCJdLCJtYXBwaW5ncyI6IkFBQUEsUUFBU0EsV0FBVUMsS0FBTUMsTUFDdkJDLFVBQVVGLEtBQU1DLE1BR2xCLFFBQVNDLFdBQVVGLEtBQU1DLE1BQ3ZCRSxpQkFBaUJILEtBQU1DIn0="
testSourcemapOriginalStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:3)\n at functionA (hello.js:2:3)\n at <anonymous>:1:1\n"
testSourcemapMangledStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:1:82)\n at functionA (hello.js:1:31)\n at <anonymous>:1:1\n"
testSourcemapMappedStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:2)\n at functionA (hello.js:2:2)\n at <anonymous>:1:1\n"
Expand Down Expand Up @@ -79,6 +80,28 @@ func TestSourceMapMangledWithSourcemap(t *testing.T) {
})
}

func TestSourceMapMangledWithInlineSourcemap(t *testing.T) {
tt(t, func() {
vm := New()

s, err := vm.CompileWithSourceMap("hello.js", testSourcemapInline, nil)
if err != nil {
panic(err)
}

if _, err := vm.Run(s); err != nil {
panic(err)
}

_, err = vm.Run(`functionA()`)
if err == nil {
panic("error should not be nil")
}

is(err.(*Error).String(), testSourcemapMappedStack)
})
}

func TestSourceMapContextPosition(t *testing.T) {
tt(t, func() {
vm := New()
Expand Down

0 comments on commit ef5bb30

Please sign in to comment.