-
Notifications
You must be signed in to change notification settings - Fork 20
/
consumer.go
262 lines (225 loc) · 5.02 KB
/
consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package sourcemap
import (
"encoding/json"
"fmt"
"net/url"
"path"
"sort"
)
type sourceMap struct {
Version int `json:"version"`
File string `json:"file"`
SourceRoot string `json:"sourceRoot"`
Sources []string `json:"sources"`
SourcesContent []string `json:"sourcesContent"`
Names []json.RawMessage `json:"names,string"`
Mappings string `json:"mappings"`
mappings []mapping
}
type v3 struct {
sourceMap
Sections []section `json:"sections"`
}
func (m *sourceMap) parse(sourcemapURL string) error {
if err := checkVersion(m.Version); err != nil {
return err
}
var sourceRootURL *url.URL
if m.SourceRoot != "" {
u, err := url.Parse(m.SourceRoot)
if err != nil {
return err
}
if u.IsAbs() {
sourceRootURL = u
}
} else if sourcemapURL != "" {
u, err := url.Parse(sourcemapURL)
if err != nil {
return err
}
if u.IsAbs() {
u.Path = path.Dir(u.Path)
sourceRootURL = u
}
}
for i, src := range m.Sources {
m.Sources[i] = m.absSource(sourceRootURL, src)
}
mappings, err := parseMappings(m.Mappings)
if err != nil {
return err
}
m.mappings = mappings
// Free memory.
m.Mappings = ""
return nil
}
func (m *sourceMap) absSource(root *url.URL, source string) string {
if path.IsAbs(source) {
return source
}
if u, err := url.Parse(source); err == nil && u.IsAbs() {
return source
}
if root != nil {
u := *root
u.Path = path.Join(u.Path, source)
return u.String()
}
if m.SourceRoot != "" {
return path.Join(m.SourceRoot, source)
}
return source
}
func (m *sourceMap) name(idx int) string {
if idx >= len(m.Names) {
return ""
}
raw := m.Names[idx]
if len(raw) == 0 {
return ""
}
if raw[0] == '"' && raw[len(raw)-1] == '"' {
var str string
if err := json.Unmarshal(raw, &str); err == nil {
return str
}
}
return string(raw)
}
type section struct {
Offset struct {
Line int `json:"line"`
Column int `json:"column"`
} `json:"offset"`
Map *sourceMap `json:"map"`
}
type Consumer struct {
sourcemapURL string
file string
sections []section
}
func Parse(sourcemapURL string, b []byte) (*Consumer, error) {
v3 := new(v3)
err := json.Unmarshal(b, v3)
if err != nil {
return nil, err
}
if err := checkVersion(v3.Version); err != nil {
return nil, err
}
if len(v3.Sections) == 0 {
v3.Sections = append(v3.Sections, section{
Map: &v3.sourceMap,
})
}
for _, s := range v3.Sections {
err := s.Map.parse(sourcemapURL)
if err != nil {
return nil, err
}
}
reverse(v3.Sections)
return &Consumer{
sourcemapURL: sourcemapURL,
file: v3.File,
sections: v3.Sections,
}, nil
}
func (c *Consumer) SourcemapURL() string {
return c.sourcemapURL
}
// File returns an optional name of the generated code
// that this source map is associated with.
func (c *Consumer) File() string {
return c.file
}
// Source returns the original source, name, line, and column information
// for the generated source's line and column positions.
func (c *Consumer) Source(
genLine, genColumn int,
) (source, name string, line, column int, ok bool) {
for i := range c.sections {
s := &c.sections[i]
if s.Offset.Line < genLine ||
(s.Offset.Line+1 == genLine && s.Offset.Column <= genColumn) {
genLine -= s.Offset.Line
genColumn -= s.Offset.Column
return c.source(s.Map, genLine, genColumn)
}
}
return
}
func (c *Consumer) source(
m *sourceMap, genLine, genColumn int,
) (source, name string, line, column int, ok bool) {
if len(m.mappings) == 0 {
return
}
i := sort.Search(len(m.mappings), func(i int) bool {
m := &m.mappings[i]
if int(m.genLine) == genLine {
return int(m.genColumn) >= genColumn
}
return int(m.genLine) >= genLine
})
var match *mapping
// Mapping not found
if i == len(m.mappings) {
// lets see if the line is correct but the column is bigger
match = &m.mappings[i-1]
if int(match.genLine) != genLine {
return
}
} else {
match = &m.mappings[i]
// Fuzzy match.
if int(match.genLine) > genLine || int(match.genColumn) > genColumn {
if i == 0 {
return
}
match = &m.mappings[i-1]
}
}
if match.sourcesInd >= 0 {
source = m.Sources[match.sourcesInd]
}
if match.namesInd >= 0 {
name = m.name(int(match.namesInd))
}
line = int(match.sourceLine)
column = int(match.sourceColumn)
ok = true
return
}
// SourceContent returns the original source content for the source.
func (c *Consumer) SourceContent(source string) string {
for i := range c.sections {
s := &c.sections[i]
for i, src := range s.Map.Sources {
if src == source {
if i < len(s.Map.SourcesContent) {
return s.Map.SourcesContent[i]
}
break
}
}
}
return ""
}
func checkVersion(version int) error {
if version == 3 || version == 0 {
return nil
}
return fmt.Errorf(
"sourcemap: got version=%d, but only 3rd version is supported",
version,
)
}
func reverse(ss []section) {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
}