forked from rome/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceMapGenerator.ts
More file actions
295 lines (253 loc) · 7.46 KB
/
Copy pathSourceMapGenerator.ts
File metadata and controls
295 lines (253 loc) · 7.46 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Portions Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/
import {Mapping, Mappings, ParsedMappings, SourceMap} from "./types";
import * as base64 from "./base64";
import {compareByGeneratedPositionsInflated, toRelativeUrl} from "./util";
import ArraySet from "./ArraySet";
import MappingList from "./MappingList";
import {
Number0,
Number1,
ob1Get0,
ob1Get1,
ob1Inc,
ob1Number0,
ob1Number1,
} from "@internal/ob1";
import SourceMapConsumer, {getParsedMappingKey} from "./SourceMapConsumer";
import {VoidCallback} from "@internal/typescript-helpers";
type MaterializeCallback = VoidCallback;
export default class SourceMapGenerator {
constructor(
args: {
file: string;
sourceRoot?: string;
},
) {
this.file = args.file;
this.sourceRoot = args.sourceRoot;
this.sourcesContents = new Map();
this.map = undefined;
this.sources = new ArraySet();
this.names = new ArraySet();
this.mappings = new MappingList();
this.materializeCallbacks = [];
}
public file: string;
private materializeCallbacks: Array<MaterializeCallback>;
private sourceRoot: undefined | string;
private sources: ArraySet;
private names: ArraySet;
private mappings: MappingList;
private sourcesContents: Map<string, string>;
private map: undefined | SourceMap;
private assertUnlocked() {
if (this.map !== undefined) {
throw new Error(
"Source map has already been materialized, serialize() should be your final call",
);
}
}
public addMaterializer(fn: MaterializeCallback) {
this.materializeCallbacks.push(fn);
}
/**
* Add a single mapping from 'original source line and column to the generated
* source's line and column for this source map being created. The mapping
* object should have the following properties:
*
* - generated: An object with the generated line and column positions.
* - original: An object with the original line and column positions.
* - source: The original source file (relative to the sourceRoot).
* - name: An optional original token name for this mapping.
*/
public addMapping(mapping: Mapping): void {
this.assertUnlocked();
const {name, source} = mapping;
this.validatePosition(
"generated",
mapping.generated.line,
mapping.generated.column,
);
if (mapping.original) {
this.validatePosition(
"original",
mapping.original.line,
mapping.original.column,
);
}
if (source !== undefined) {
this.sources.add(source);
}
if (name !== undefined) {
this.names.add(name);
}
this.mappings.add(mapping);
}
/**
* Set the source content for a source file.
*/
public setSourceContent(
source: string,
sourceContent: undefined | string,
): void {
this.assertUnlocked();
if (this.sourceRoot !== undefined) {
source = toRelativeUrl(this.sourceRoot, source);
}
if (sourceContent !== undefined) {
// Add the source content to the _sourcesContents map.
this.sourcesContents.set(source, sourceContent);
} else {
// Remove the source file from the _sourcesContents map.
this.sourcesContents.delete(source);
}
}
private validatePosition(key: string, line: Number1, column: Number0): void {
if (ob1Get1(line) <= 0) {
throw new Error(`${key} line should be >= 1 but is ${line}`);
}
if (ob1Get0(column) < 0) {
throw new Error(`${key} column should be >= 0 but is ${column}`);
}
}
/**
* Serialize the accumulated mappings in to the stream of base 64 VLQs
* specified by the source map format.
*/
private serializeMappings(): string {
let previousGeneratedColumn: Number0 = ob1Number0;
let previousGeneratedLine: Number1 = ob1Number1;
let previousOriginalColumn: Number0 = ob1Number0;
let previousOriginalLine: Number1 = ob1Number1;
let previousName: number = 0;
let previousSource: number = 0;
let result: string = "";
const mappings = this.mappings.toArray();
for (let i = 0; i < mappings.length; i++) {
const mapping = mappings[i];
let next = "";
if (mapping.generated.line !== previousGeneratedLine) {
previousGeneratedColumn = ob1Number0;
while (mapping.generated.line !== previousGeneratedLine) {
next += ";";
previousGeneratedLine = ob1Inc(previousGeneratedLine);
}
} else if (i > 0) {
if (!compareByGeneratedPositionsInflated(mapping, mappings[i - 1])) {
continue;
}
next += ",";
}
next += base64.encodeVLQ(
ob1Get0(mapping.generated.column) - ob1Get0(previousGeneratedColumn),
);
previousGeneratedColumn = mapping.generated.column;
if (mapping.source !== undefined) {
const sourceIdx = this.sources.indexOf(mapping.source);
next += base64.encodeVLQ(sourceIdx - previousSource);
previousSource = sourceIdx;
if (mapping.original) {
next += base64.encodeVLQ(
ob1Get1(mapping.original.line) - ob1Get1(previousOriginalLine),
);
previousOriginalLine = mapping.original.line;
next += base64.encodeVLQ(
ob1Get0(mapping.original.column) - ob1Get0(previousOriginalColumn),
);
previousOriginalColumn = mapping.original.column;
if (mapping.name !== undefined) {
const nameIdx = this.names.indexOf(mapping.name);
next += base64.encodeVLQ(nameIdx - previousName);
previousName = nameIdx;
}
}
// TODO: else, assert mapping.name is undefined since it can't be encoded without an original position
}
// TODO: else, assert mapping.original is undefined since it can't be encoded without a source
result += next;
}
return result;
}
private generateSourcesContent(
sources: Array<string>,
sourceRoot: undefined | string,
): Array<string> {
return sources.map((source) => {
if (sourceRoot !== undefined) {
source = toRelativeUrl(sourceRoot, source);
}
const content = this.sourcesContents.get(source);
if (content === undefined) {
throw new Error("Expected content");
}
return content;
});
}
private materialize() {
for (const fn of this.materializeCallbacks) {
fn();
}
this.materializeCallbacks = [];
}
/**
* Externalize the source map.
*/
public serialize(): SourceMap {
if (this.map !== undefined) {
return this.map;
}
this.materialize();
const sources = this.sources.toArray();
this.map = {
version: 3,
file: this.file,
names: this.names.toArray(),
mappings: this.serializeMappings(),
sourceRoot: this.sourceRoot,
sources,
sourcesContent: this.generateSourcesContent(sources, this.sourceRoot),
};
return this.map;
}
public toComment(): string {
const jsonMap = this.toJSON();
const base64Map = new Buffer(jsonMap).toString("base64");
const comment = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`;
return comment;
}
public toConsumer(): SourceMapConsumer {
return new SourceMapConsumer(
this.file,
() => {
const parsedMappings: ParsedMappings = new Map();
for (const mapping of this.getMappings()) {
parsedMappings.set(
getParsedMappingKey(
mapping.generated.line,
mapping.generated.column,
),
mapping,
);
}
return parsedMappings;
},
);
}
private getMappings(): Mappings {
this.materialize();
return this.mappings.toArray();
}
public toJSON(): string {
return JSON.stringify(this.serialize());
}
}