source: trip-planner-front/node_modules/source-map/lib/source-map-generator.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 13.5 KB
RevLine 
[6a3a178]1/* -*- Mode: js; js-indent-level: 2; -*- */
2/*
3 * Copyright 2011 Mozilla Foundation and contributors
4 * Licensed under the New BSD license. See LICENSE or:
5 * http://opensource.org/licenses/BSD-3-Clause
6 */
7
8const base64VLQ = require("./base64-vlq");
9const util = require("./util");
10const ArraySet = require("./array-set").ArraySet;
11const MappingList = require("./mapping-list").MappingList;
12
13/**
14 * An instance of the SourceMapGenerator represents a source map which is
15 * being built incrementally. You may pass an object with the following
16 * properties:
17 *
18 * - file: The filename of the generated source.
19 * - sourceRoot: A root for all relative URLs in this source map.
20 */
21class SourceMapGenerator {
22 constructor(aArgs) {
23 if (!aArgs) {
24 aArgs = {};
25 }
26 this._file = util.getArg(aArgs, "file", null);
27 this._sourceRoot = util.getArg(aArgs, "sourceRoot", null);
28 this._skipValidation = util.getArg(aArgs, "skipValidation", false);
29 this._sources = new ArraySet();
30 this._names = new ArraySet();
31 this._mappings = new MappingList();
32 this._sourcesContents = null;
33 }
34
35 /**
36 * Creates a new SourceMapGenerator based on a SourceMapConsumer
37 *
38 * @param aSourceMapConsumer The SourceMap.
39 */
40 static fromSourceMap(aSourceMapConsumer) {
41 const sourceRoot = aSourceMapConsumer.sourceRoot;
42 const generator = new SourceMapGenerator({
43 file: aSourceMapConsumer.file,
44 sourceRoot
45 });
46 aSourceMapConsumer.eachMapping(function(mapping) {
47 const newMapping = {
48 generated: {
49 line: mapping.generatedLine,
50 column: mapping.generatedColumn
51 }
52 };
53
54 if (mapping.source != null) {
55 newMapping.source = mapping.source;
56 if (sourceRoot != null) {
57 newMapping.source = util.relative(sourceRoot, newMapping.source);
58 }
59
60 newMapping.original = {
61 line: mapping.originalLine,
62 column: mapping.originalColumn
63 };
64
65 if (mapping.name != null) {
66 newMapping.name = mapping.name;
67 }
68 }
69
70 generator.addMapping(newMapping);
71 });
72 aSourceMapConsumer.sources.forEach(function(sourceFile) {
73 let sourceRelative = sourceFile;
74 if (sourceRoot !== null) {
75 sourceRelative = util.relative(sourceRoot, sourceFile);
76 }
77
78 if (!generator._sources.has(sourceRelative)) {
79 generator._sources.add(sourceRelative);
80 }
81
82 const content = aSourceMapConsumer.sourceContentFor(sourceFile);
83 if (content != null) {
84 generator.setSourceContent(sourceFile, content);
85 }
86 });
87 return generator;
88 }
89
90 /**
91 * Add a single mapping from original source line and column to the generated
92 * source's line and column for this source map being created. The mapping
93 * object should have the following properties:
94 *
95 * - generated: An object with the generated line and column positions.
96 * - original: An object with the original line and column positions.
97 * - source: The original source file (relative to the sourceRoot).
98 * - name: An optional original token name for this mapping.
99 */
100 addMapping(aArgs) {
101 const generated = util.getArg(aArgs, "generated");
102 const original = util.getArg(aArgs, "original", null);
103 let source = util.getArg(aArgs, "source", null);
104 let name = util.getArg(aArgs, "name", null);
105
106 if (!this._skipValidation) {
107 this._validateMapping(generated, original, source, name);
108 }
109
110 if (source != null) {
111 source = String(source);
112 if (!this._sources.has(source)) {
113 this._sources.add(source);
114 }
115 }
116
117 if (name != null) {
118 name = String(name);
119 if (!this._names.has(name)) {
120 this._names.add(name);
121 }
122 }
123
124 this._mappings.add({
125 generatedLine: generated.line,
126 generatedColumn: generated.column,
127 originalLine: original != null && original.line,
128 originalColumn: original != null && original.column,
129 source,
130 name
131 });
132 }
133
134 /**
135 * Set the source content for a source file.
136 */
137 setSourceContent(aSourceFile, aSourceContent) {
138 let source = aSourceFile;
139 if (this._sourceRoot != null) {
140 source = util.relative(this._sourceRoot, source);
141 }
142
143 if (aSourceContent != null) {
144 // Add the source content to the _sourcesContents map.
145 // Create a new _sourcesContents map if the property is null.
146 if (!this._sourcesContents) {
147 this._sourcesContents = Object.create(null);
148 }
149 this._sourcesContents[util.toSetString(source)] = aSourceContent;
150 } else if (this._sourcesContents) {
151 // Remove the source file from the _sourcesContents map.
152 // If the _sourcesContents map is empty, set the property to null.
153 delete this._sourcesContents[util.toSetString(source)];
154 if (Object.keys(this._sourcesContents).length === 0) {
155 this._sourcesContents = null;
156 }
157 }
158 }
159
160 /**
161 * Applies the mappings of a sub-source-map for a specific source file to the
162 * source map being generated. Each mapping to the supplied source file is
163 * rewritten using the supplied source map. Note: The resolution for the
164 * resulting mappings is the minimium of this map and the supplied map.
165 *
166 * @param aSourceMapConsumer The source map to be applied.
167 * @param aSourceFile Optional. The filename of the source file.
168 * If omitted, SourceMapConsumer's file property will be used.
169 * @param aSourceMapPath Optional. The dirname of the path to the source map
170 * to be applied. If relative, it is relative to the SourceMapConsumer.
171 * This parameter is needed when the two source maps aren't in the same
172 * directory, and the source map to be applied contains relative source
173 * paths. If so, those relative source paths need to be rewritten
174 * relative to the SourceMapGenerator.
175 */
176 applySourceMap(aSourceMapConsumer, aSourceFile, aSourceMapPath) {
177 let sourceFile = aSourceFile;
178 // If aSourceFile is omitted, we will use the file property of the SourceMap
179 if (aSourceFile == null) {
180 if (aSourceMapConsumer.file == null) {
181 throw new Error(
182 "SourceMapGenerator.prototype.applySourceMap requires either an explicit source file, " +
183 'or the source map\'s "file" property. Both were omitted.'
184 );
185 }
186 sourceFile = aSourceMapConsumer.file;
187 }
188 const sourceRoot = this._sourceRoot;
189 // Make "sourceFile" relative if an absolute Url is passed.
190 if (sourceRoot != null) {
191 sourceFile = util.relative(sourceRoot, sourceFile);
192 }
193 // Applying the SourceMap can add and remove items from the sources and
194 // the names array.
195 const newSources = this._mappings.toArray().length > 0
196 ? new ArraySet()
197 : this._sources;
198 const newNames = new ArraySet();
199
200 // Find mappings for the "sourceFile"
201 this._mappings.unsortedForEach(function(mapping) {
202 if (mapping.source === sourceFile && mapping.originalLine != null) {
203 // Check if it can be mapped by the source map, then update the mapping.
204 const original = aSourceMapConsumer.originalPositionFor({
205 line: mapping.originalLine,
206 column: mapping.originalColumn
207 });
208 if (original.source != null) {
209 // Copy mapping
210 mapping.source = original.source;
211 if (aSourceMapPath != null) {
212 mapping.source = util.join(aSourceMapPath, mapping.source);
213 }
214 if (sourceRoot != null) {
215 mapping.source = util.relative(sourceRoot, mapping.source);
216 }
217 mapping.originalLine = original.line;
218 mapping.originalColumn = original.column;
219 if (original.name != null) {
220 mapping.name = original.name;
221 }
222 }
223 }
224
225 const source = mapping.source;
226 if (source != null && !newSources.has(source)) {
227 newSources.add(source);
228 }
229
230 const name = mapping.name;
231 if (name != null && !newNames.has(name)) {
232 newNames.add(name);
233 }
234
235 }, this);
236 this._sources = newSources;
237 this._names = newNames;
238
239 // Copy sourcesContents of applied map.
240 aSourceMapConsumer.sources.forEach(function(srcFile) {
241 const content = aSourceMapConsumer.sourceContentFor(srcFile);
242 if (content != null) {
243 if (aSourceMapPath != null) {
244 srcFile = util.join(aSourceMapPath, srcFile);
245 }
246 if (sourceRoot != null) {
247 srcFile = util.relative(sourceRoot, srcFile);
248 }
249 this.setSourceContent(srcFile, content);
250 }
251 }, this);
252 }
253
254 /**
255 * A mapping can have one of the three levels of data:
256 *
257 * 1. Just the generated position.
258 * 2. The Generated position, original position, and original source.
259 * 3. Generated and original position, original source, as well as a name
260 * token.
261 *
262 * To maintain consistency, we validate that any new mapping being added falls
263 * in to one of these categories.
264 */
265 _validateMapping(aGenerated, aOriginal, aSource, aName) {
266 // When aOriginal is truthy but has empty values for .line and .column,
267 // it is most likely a programmer error. In this case we throw a very
268 // specific error message to try to guide them the right way.
269 // For example: https://github.com/Polymer/polymer-bundler/pull/519
270 if (aOriginal && typeof aOriginal.line !== "number" && typeof aOriginal.column !== "number") {
271 throw new Error(
272 "original.line and original.column are not numbers -- you probably meant to omit " +
273 "the original mapping entirely and only map the generated position. If so, pass " +
274 "null for the original mapping instead of an object with empty or null values."
275 );
276 }
277
278 if (aGenerated && "line" in aGenerated && "column" in aGenerated
279 && aGenerated.line > 0 && aGenerated.column >= 0
280 && !aOriginal && !aSource && !aName) {
281 // Case 1.
282
283 } else if (aGenerated && "line" in aGenerated && "column" in aGenerated
284 && aOriginal && "line" in aOriginal && "column" in aOriginal
285 && aGenerated.line > 0 && aGenerated.column >= 0
286 && aOriginal.line > 0 && aOriginal.column >= 0
287 && aSource) {
288 // Cases 2 and 3.
289
290 } else {
291 throw new Error("Invalid mapping: " + JSON.stringify({
292 generated: aGenerated,
293 source: aSource,
294 original: aOriginal,
295 name: aName
296 }));
297 }
298 }
299
300 /**
301 * Serialize the accumulated mappings in to the stream of base 64 VLQs
302 * specified by the source map format.
303 */
304 _serializeMappings() {
305 let previousGeneratedColumn = 0;
306 let previousGeneratedLine = 1;
307 let previousOriginalColumn = 0;
308 let previousOriginalLine = 0;
309 let previousName = 0;
310 let previousSource = 0;
311 let result = "";
312 let next;
313 let mapping;
314 let nameIdx;
315 let sourceIdx;
316
317 const mappings = this._mappings.toArray();
318 for (let i = 0, len = mappings.length; i < len; i++) {
319 mapping = mappings[i];
320 next = "";
321
322 if (mapping.generatedLine !== previousGeneratedLine) {
323 previousGeneratedColumn = 0;
324 while (mapping.generatedLine !== previousGeneratedLine) {
325 next += ";";
326 previousGeneratedLine++;
327 }
328 } else if (i > 0) {
329 if (!util.compareByGeneratedPositionsInflated(mapping, mappings[i - 1])) {
330 continue;
331 }
332 next += ",";
333 }
334
335 next += base64VLQ.encode(mapping.generatedColumn
336 - previousGeneratedColumn);
337 previousGeneratedColumn = mapping.generatedColumn;
338
339 if (mapping.source != null) {
340 sourceIdx = this._sources.indexOf(mapping.source);
341 next += base64VLQ.encode(sourceIdx - previousSource);
342 previousSource = sourceIdx;
343
344 // lines are stored 0-based in SourceMap spec version 3
345 next += base64VLQ.encode(mapping.originalLine - 1
346 - previousOriginalLine);
347 previousOriginalLine = mapping.originalLine - 1;
348
349 next += base64VLQ.encode(mapping.originalColumn
350 - previousOriginalColumn);
351 previousOriginalColumn = mapping.originalColumn;
352
353 if (mapping.name != null) {
354 nameIdx = this._names.indexOf(mapping.name);
355 next += base64VLQ.encode(nameIdx - previousName);
356 previousName = nameIdx;
357 }
358 }
359
360 result += next;
361 }
362
363 return result;
364 }
365
366 _generateSourcesContent(aSources, aSourceRoot) {
367 return aSources.map(function(source) {
368 if (!this._sourcesContents) {
369 return null;
370 }
371 if (aSourceRoot != null) {
372 source = util.relative(aSourceRoot, source);
373 }
374 const key = util.toSetString(source);
375 return Object.prototype.hasOwnProperty.call(this._sourcesContents, key)
376 ? this._sourcesContents[key]
377 : null;
378 }, this);
379 }
380
381 /**
382 * Externalize the source map.
383 */
384 toJSON() {
385 const map = {
386 version: this._version,
387 sources: this._sources.toArray(),
388 names: this._names.toArray(),
389 mappings: this._serializeMappings()
390 };
391 if (this._file != null) {
392 map.file = this._file;
393 }
394 if (this._sourceRoot != null) {
395 map.sourceRoot = this._sourceRoot;
396 }
397 if (this._sourcesContents) {
398 map.sourcesContent = this._generateSourcesContent(map.sources, map.sourceRoot);
399 }
400
401 return map;
402 }
403
404 /**
405 * Render the source map being generated to a string.
406 */
407 toString() {
408 return JSON.stringify(this.toJSON());
409 }
410}
411
412SourceMapGenerator.prototype._version = 3;
413exports.SourceMapGenerator = SourceMapGenerator;
Note: See TracBrowser for help on using the repository browser.