Skip to content

Commit d2406c6

Browse files
committed
Add more tests and fix corner cases.
1 parent f5da1d5 commit d2406c6

3 files changed

Lines changed: 72 additions & 50 deletions

File tree

consumer.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package sourcemap
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"net/url"
87
"path"
@@ -24,7 +23,10 @@ func Parse(mapURL string, b []byte) (*Consumer, error) {
2423
}
2524

2625
if smap.Version != 3 {
27-
return nil, errors.New("sourcemap: only 3rd version is supported")
26+
return nil, fmt.Errorf(
27+
"sourcemap: got version=%d, but only 3rd version is supported",
28+
smap.Version,
29+
)
2830
}
2931

3032
var sourceRootURL *url.URL
@@ -82,22 +84,25 @@ func (c *Consumer) Source(genLine, genCol int) (source, name string, line, col i
8284
match := &c.mappings[i]
8385

8486
// Fuzzy match.
85-
if match.genCol > genCol && i > 0 {
87+
if match.genLine > genLine || match.genCol > genCol {
88+
if i == 0 {
89+
return
90+
}
8691
match = &c.mappings[i-1]
8792
}
8893

8994
if match.sourcesInd >= 0 {
9095
source = c.absSource(c.smap.Sources[match.sourcesInd])
9196
}
9297
if match.namesInd >= 0 {
93-
iv := c.smap.Names[match.namesInd]
94-
switch v := iv.(type) {
98+
v := c.smap.Names[match.namesInd]
99+
switch v := v.(type) {
95100
case string:
96101
name = v
97102
case float64:
98103
name = strconv.FormatFloat(v, 'f', -1, 64)
99104
default:
100-
name = fmt.Sprint(iv)
105+
name = fmt.Sprint(v)
101106
}
102107
}
103108
line = match.sourceLine

consumer_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sourcemap_test
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"net/http"
67
"strings"
@@ -20,11 +21,10 @@ func init() {
2021
}
2122
defer resp.Body.Close()
2223

23-
b, err := ioutil.ReadAll(resp.Body)
24+
jqSourceMapBytes, err = ioutil.ReadAll(resp.Body)
2425
if err != nil {
2526
panic(err)
2627
}
27-
jqSourceMapBytes = b
2828
}
2929

3030
type sourceMapTest struct {
@@ -36,22 +36,32 @@ type sourceMapTest struct {
3636
wantedCol int
3737
}
3838

39+
func (test *sourceMapTest) String() string {
40+
return fmt.Sprintf("line=%d col=%d in file=%s", test.genLine, test.genCol, test.wantedSource)
41+
}
42+
3943
func (test *sourceMapTest) assert(t *testing.T, smap *sourcemap.Consumer) {
4044
source, name, line, col, ok := smap.Source(test.genLine, test.genCol)
4145
if !ok {
42-
t.Fatalf("Source not found for %d, %d", test.genLine, test.genCol)
46+
if test.wantedSource == "" &&
47+
test.wantedName == "" &&
48+
test.wantedLine == 0 &&
49+
test.wantedCol == 0 {
50+
return
51+
}
52+
t.Fatalf("Source not found for %s", test)
4353
}
4454
if source != test.wantedSource {
45-
t.Fatalf("got %q, wanted %q", source, test.wantedSource)
55+
t.Fatalf("file: got %q, wanted %q (%s)", source, test.wantedSource, test)
4656
}
4757
if name != test.wantedName {
48-
t.Fatalf("got %q, wanted %q", name, test.wantedName)
58+
t.Fatalf("func: got %q, wanted %q (%s)", name, test.wantedName, test)
4959
}
5060
if line != test.wantedLine {
51-
t.Fatalf("got %q, wanted %q", line, test.wantedLine)
61+
t.Fatalf("line: got %d, wanted %d (%s)", line, test.wantedLine, test)
5262
}
5363
if col != test.wantedCol {
54-
t.Fatalf("got %q, wanted %q", col, test.wantedCol)
64+
t.Fatalf("column: got %d, wanted %d (%s)", col, test.wantedCol, test)
5565
}
5666
}
5767

@@ -162,9 +172,14 @@ func TestJQuerySourceMap(t *testing.T) {
162172
}
163173

164174
tests := []*sourceMapTest{
175+
{1, 1, "", "", 0, 0},
176+
{4, 0, "", "", 0, 0},
177+
{4, 1, "http://code.jquery.com/jquery-2.0.3.js", "", 14, 0},
178+
{4, 10, "http://code.jquery.com/jquery-2.0.3.js", "window", 14, 11},
165179
{5, 6789, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4360, 27},
166180
{5, 10006, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4676, 8},
167181
{4, 553, "http://code.jquery.com/jquery-2.0.3.js", "ready", 93, 9},
182+
{999999, 0, "", "", 0, 0},
168183
}
169184
for _, test := range tests {
170185
test.assert(t, smap)

sourcemap.go

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,21 @@ type mappings struct {
3131
rd *strings.Reader
3232
dec *base64vlq.Decoder
3333

34-
genLine int
35-
genCol int
36-
sourcesInd int
37-
sourceLine int
38-
sourceCol int
39-
namesInd int
34+
hasName bool
35+
value mapping
4036

4137
values []mapping
42-
value *mapping
4338
}
4439

4540
func parseMappings(s string) ([]mapping, error) {
4641
rd := strings.NewReader(s)
4742
m := &mappings{
4843
rd: rd,
4944
dec: base64vlq.NewDecoder(rd),
50-
51-
genLine: 1,
52-
sourceLine: 1,
5345
}
54-
m.pushValue()
46+
m.value.genLine = 1
47+
m.value.sourceLine = 1
48+
5549
err := m.parse()
5650
if err != nil {
5751
return nil, err
@@ -60,24 +54,27 @@ func parseMappings(s string) ([]mapping, error) {
6054
}
6155

6256
func (m *mappings) parse() error {
63-
next := parseGenCol
57+
var prev, next fn
58+
next = parseGenCol
6459
for {
6560
c, err := m.rd.ReadByte()
6661
if err == io.EOF {
62+
m.pushValue(prev)
6763
return nil
68-
} else if err != nil {
64+
}
65+
if err != nil {
6966
return err
7067
}
7168

7269
switch c {
7370
case ',':
74-
m.pushValue()
71+
m.pushValue(prev)
7572
next = parseGenCol
7673
case ';':
77-
m.pushValue()
74+
m.pushValue(prev)
7875

79-
m.genLine++
80-
m.genCol = 0
76+
m.value.genLine++
77+
m.value.genCol = 0
8178

8279
next = parseGenCol
8380
default:
@@ -86,6 +83,7 @@ func (m *mappings) parse() error {
8683
return err
8784
}
8885

86+
prev = next
8987
next, err = next(m)
9088
if err != nil {
9189
return err
@@ -99,8 +97,7 @@ func parseGenCol(m *mappings) (fn, error) {
9997
if err != nil {
10098
return nil, err
10199
}
102-
m.genCol += n
103-
m.value.genCol = m.genCol
100+
m.value.genCol += n
104101
return parseSourcesInd, nil
105102
}
106103

@@ -109,8 +106,7 @@ func parseSourcesInd(m *mappings) (fn, error) {
109106
if err != nil {
110107
return nil, err
111108
}
112-
m.sourcesInd += n
113-
m.value.sourcesInd = m.sourcesInd
109+
m.value.sourcesInd += n
114110
return parseSourceLine, nil
115111
}
116112

@@ -119,8 +115,7 @@ func parseSourceLine(m *mappings) (fn, error) {
119115
if err != nil {
120116
return nil, err
121117
}
122-
m.sourceLine += n
123-
m.value.sourceLine = m.sourceLine
118+
m.value.sourceLine += n
124119
return parseSourceCol, nil
125120
}
126121

@@ -129,8 +124,7 @@ func parseSourceCol(m *mappings) (fn, error) {
129124
if err != nil {
130125
return nil, err
131126
}
132-
m.sourceCol += n
133-
m.value.sourceCol = m.sourceCol
127+
m.value.sourceCol += n
134128
return parseNamesInd, nil
135129
}
136130

@@ -139,19 +133,27 @@ func parseNamesInd(m *mappings) (fn, error) {
139133
if err != nil {
140134
return nil, err
141135
}
142-
m.namesInd += n
143-
m.value.namesInd = m.namesInd
136+
m.hasName = true
137+
m.value.namesInd += n
144138
return parseGenCol, nil
145139
}
146140

147-
func (m *mappings) pushValue() {
148-
m.values = append(m.values, mapping{
149-
genLine: m.genLine,
150-
genCol: 0,
151-
sourcesInd: -1,
152-
sourceLine: 0,
153-
sourceCol: 0,
154-
namesInd: -1,
155-
})
156-
m.value = &m.values[len(m.values)-1]
141+
func (m *mappings) pushValue(prev fn) {
142+
if m.value.sourceLine == 1 && m.value.sourceCol == 0 {
143+
return
144+
}
145+
146+
if m.hasName {
147+
m.values = append(m.values, m.value)
148+
m.hasName = false
149+
} else {
150+
m.values = append(m.values, mapping{
151+
genLine: m.value.genLine,
152+
genCol: m.value.genCol,
153+
sourcesInd: m.value.sourcesInd,
154+
sourceLine: m.value.sourceLine,
155+
sourceCol: m.value.sourceCol,
156+
namesInd: -1,
157+
})
158+
}
157159
}

0 commit comments

Comments
 (0)