Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80765 views
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
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
13
var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
14
15
exports['test that we can instantiate with a string or an object'] = function (assert, util) {
16
assert.doesNotThrow(function () {
17
var map = new SourceMapConsumer(util.testMap);
18
});
19
assert.doesNotThrow(function () {
20
var map = new SourceMapConsumer(JSON.stringify(util.testMap));
21
});
22
};
23
24
exports['test that the `sources` field has the original sources'] = function (assert, util) {
25
var map = new SourceMapConsumer(util.testMap);
26
var sources = map.sources;
27
28
assert.equal(sources[0], '/the/root/one.js');
29
assert.equal(sources[1], '/the/root/two.js');
30
assert.equal(sources.length, 2);
31
};
32
33
exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {
34
var map = new SourceMapConsumer(util.testMap);
35
var mapping;
36
37
mapping = map.originalPositionFor({
38
line: 2,
39
column: 1
40
});
41
assert.equal(mapping.source, '/the/root/two.js');
42
43
mapping = map.originalPositionFor({
44
line: 1,
45
column: 1
46
});
47
assert.equal(mapping.source, '/the/root/one.js');
48
};
49
50
exports['test mapping tokens back exactly'] = function (assert, util) {
51
var map = new SourceMapConsumer(util.testMap);
52
53
util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
54
util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
55
util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
56
util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
57
util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
58
util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
59
util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
60
61
util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
62
util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
63
util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
64
util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
65
util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
66
util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
67
};
68
69
exports['test mapping tokens fuzzy'] = function (assert, util) {
70
var map = new SourceMapConsumer(util.testMap);
71
72
// Finding original positions
73
util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);
74
util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);
75
util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);
76
77
// Finding generated positions
78
util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);
79
util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);
80
util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);
81
};
82
83
exports['test mappings and end of lines'] = function (assert, util) {
84
var smg = new SourceMapGenerator({
85
file: 'foo.js'
86
});
87
smg.addMapping({
88
original: { line: 1, column: 1 },
89
generated: { line: 1, column: 1 },
90
source: 'bar.js'
91
});
92
smg.addMapping({
93
original: { line: 2, column: 2 },
94
generated: { line: 2, column: 2 },
95
source: 'bar.js'
96
});
97
98
var map = SourceMapConsumer.fromSourceMap(smg);
99
100
// When finding original positions, mappings end at the end of the line.
101
util.assertMapping(2, 1, null, null, null, null, map, assert, true)
102
103
// When finding generated positions, mappings do not end at the end of the line.
104
util.assertMapping(1, 1, 'bar.js', 2, 1, null, map, assert, null, true);
105
};
106
107
exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {
108
assert.doesNotThrow(function () {
109
var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));
110
});
111
};
112
113
exports['test eachMapping'] = function (assert, util) {
114
var map = new SourceMapConsumer(util.testMap);
115
var previousLine = -Infinity;
116
var previousColumn = -Infinity;
117
map.eachMapping(function (mapping) {
118
assert.ok(mapping.generatedLine >= previousLine);
119
120
if (mapping.source) {
121
assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0);
122
}
123
124
if (mapping.generatedLine === previousLine) {
125
assert.ok(mapping.generatedColumn >= previousColumn);
126
previousColumn = mapping.generatedColumn;
127
}
128
else {
129
previousLine = mapping.generatedLine;
130
previousColumn = -Infinity;
131
}
132
});
133
};
134
135
exports['test iterating over mappings in a different order'] = function (assert, util) {
136
var map = new SourceMapConsumer(util.testMap);
137
var previousLine = -Infinity;
138
var previousColumn = -Infinity;
139
var previousSource = "";
140
map.eachMapping(function (mapping) {
141
assert.ok(mapping.source >= previousSource);
142
143
if (mapping.source === previousSource) {
144
assert.ok(mapping.originalLine >= previousLine);
145
146
if (mapping.originalLine === previousLine) {
147
assert.ok(mapping.originalColumn >= previousColumn);
148
previousColumn = mapping.originalColumn;
149
}
150
else {
151
previousLine = mapping.originalLine;
152
previousColumn = -Infinity;
153
}
154
}
155
else {
156
previousSource = mapping.source;
157
previousLine = -Infinity;
158
previousColumn = -Infinity;
159
}
160
}, null, SourceMapConsumer.ORIGINAL_ORDER);
161
};
162
163
exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {
164
var map = new SourceMapConsumer(util.testMap);
165
var context = {};
166
map.eachMapping(function () {
167
assert.equal(this, context);
168
}, context);
169
};
170
171
exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {
172
var map = new SourceMapConsumer(util.testMapWithSourcesContent);
173
var sourcesContent = map.sourcesContent;
174
175
assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');
176
assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');
177
assert.equal(sourcesContent.length, 2);
178
};
179
180
exports['test that we can get the original sources for the sources'] = function (assert, util) {
181
var map = new SourceMapConsumer(util.testMapWithSourcesContent);
182
var sources = map.sources;
183
184
assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
185
assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');
186
assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
187
assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');
188
assert.throws(function () {
189
map.sourceContentFor("");
190
}, Error);
191
assert.throws(function () {
192
map.sourceContentFor("/the/root/three.js");
193
}, Error);
194
assert.throws(function () {
195
map.sourceContentFor("three.js");
196
}, Error);
197
};
198
199
exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {
200
var map = new SourceMapGenerator({
201
sourceRoot: 'foo/bar',
202
file: 'baz.js'
203
});
204
map.addMapping({
205
original: { line: 1, column: 1 },
206
generated: { line: 2, column: 2 },
207
source: 'bang.coffee'
208
});
209
map.addMapping({
210
original: { line: 5, column: 5 },
211
generated: { line: 6, column: 6 },
212
source: 'bang.coffee'
213
});
214
map = new SourceMapConsumer(map.toString());
215
216
// Should handle without sourceRoot.
217
var pos = map.generatedPositionFor({
218
line: 1,
219
column: 1,
220
source: 'bang.coffee'
221
});
222
223
assert.equal(pos.line, 2);
224
assert.equal(pos.column, 2);
225
226
// Should handle with sourceRoot.
227
var pos = map.generatedPositionFor({
228
line: 1,
229
column: 1,
230
source: 'foo/bar/bang.coffee'
231
});
232
233
assert.equal(pos.line, 2);
234
assert.equal(pos.column, 2);
235
};
236
237
exports['test sourceRoot + originalPositionFor'] = function (assert, util) {
238
var map = new SourceMapGenerator({
239
sourceRoot: 'foo/bar',
240
file: 'baz.js'
241
});
242
map.addMapping({
243
original: { line: 1, column: 1 },
244
generated: { line: 2, column: 2 },
245
source: 'bang.coffee'
246
});
247
map = new SourceMapConsumer(map.toString());
248
249
var pos = map.originalPositionFor({
250
line: 2,
251
column: 2,
252
});
253
254
// Should always have the prepended source root
255
assert.equal(pos.source, 'foo/bar/bang.coffee');
256
assert.equal(pos.line, 1);
257
assert.equal(pos.column, 1);
258
};
259
260
exports['test github issue #56'] = function (assert, util) {
261
var map = new SourceMapGenerator({
262
sourceRoot: 'http://',
263
file: 'www.example.com/foo.js'
264
});
265
map.addMapping({
266
original: { line: 1, column: 1 },
267
generated: { line: 2, column: 2 },
268
source: 'www.example.com/original.js'
269
});
270
map = new SourceMapConsumer(map.toString());
271
272
var sources = map.sources;
273
assert.equal(sources.length, 1);
274
assert.equal(sources[0], 'http://www.example.com/original.js');
275
};
276
277
exports['test github issue #43'] = function (assert, util) {
278
var map = new SourceMapGenerator({
279
sourceRoot: 'http://example.com',
280
file: 'foo.js'
281
});
282
map.addMapping({
283
original: { line: 1, column: 1 },
284
generated: { line: 2, column: 2 },
285
source: 'http://cdn.example.com/original.js'
286
});
287
map = new SourceMapConsumer(map.toString());
288
289
var sources = map.sources;
290
assert.equal(sources.length, 1,
291
'Should only be one source.');
292
assert.equal(sources[0], 'http://cdn.example.com/original.js',
293
'Should not be joined with the sourceRoot.');
294
};
295
296
exports['test absolute path, but same host sources'] = function (assert, util) {
297
var map = new SourceMapGenerator({
298
sourceRoot: 'http://example.com/foo/bar',
299
file: 'foo.js'
300
});
301
map.addMapping({
302
original: { line: 1, column: 1 },
303
generated: { line: 2, column: 2 },
304
source: '/original.js'
305
});
306
map = new SourceMapConsumer(map.toString());
307
308
var sources = map.sources;
309
assert.equal(sources.length, 1,
310
'Should only be one source.');
311
assert.equal(sources[0], 'http://example.com/original.js',
312
'Source should be relative the host of the source root.');
313
};
314
315
exports['test github issue #64'] = function (assert, util) {
316
var map = new SourceMapConsumer({
317
"version": 3,
318
"file": "foo.js",
319
"sourceRoot": "http://example.com/",
320
"sources": ["/a"],
321
"names": [],
322
"mappings": "AACA",
323
"sourcesContent": ["foo"]
324
});
325
326
assert.equal(map.sourceContentFor("a"), "foo");
327
assert.equal(map.sourceContentFor("/a"), "foo");
328
};
329
330
exports['test bug 885597'] = function (assert, util) {
331
var map = new SourceMapConsumer({
332
"version": 3,
333
"file": "foo.js",
334
"sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",
335
"sources": ["/a"],
336
"names": [],
337
"mappings": "AACA",
338
"sourcesContent": ["foo"]
339
});
340
341
var s = map.sources[0];
342
assert.equal(map.sourceContentFor(s), "foo");
343
};
344
345
exports['test github issue #72, duplicate sources'] = function (assert, util) {
346
var map = new SourceMapConsumer({
347
"version": 3,
348
"file": "foo.js",
349
"sources": ["source1.js", "source1.js", "source3.js"],
350
"names": [],
351
"mappings": ";EAAC;;IAEE;;MEEE",
352
"sourceRoot": "http://example.com"
353
});
354
355
var pos = map.originalPositionFor({
356
line: 2,
357
column: 2
358
});
359
assert.equal(pos.source, 'http://example.com/source1.js');
360
assert.equal(pos.line, 1);
361
assert.equal(pos.column, 1);
362
363
var pos = map.originalPositionFor({
364
line: 4,
365
column: 4
366
});
367
assert.equal(pos.source, 'http://example.com/source1.js');
368
assert.equal(pos.line, 3);
369
assert.equal(pos.column, 3);
370
371
var pos = map.originalPositionFor({
372
line: 6,
373
column: 6
374
});
375
assert.equal(pos.source, 'http://example.com/source3.js');
376
assert.equal(pos.line, 5);
377
assert.equal(pos.column, 5);
378
};
379
380
exports['test github issue #72, duplicate names'] = function (assert, util) {
381
var map = new SourceMapConsumer({
382
"version": 3,
383
"file": "foo.js",
384
"sources": ["source.js"],
385
"names": ["name1", "name1", "name3"],
386
"mappings": ";EAACA;;IAEEA;;MAEEE",
387
"sourceRoot": "http://example.com"
388
});
389
390
var pos = map.originalPositionFor({
391
line: 2,
392
column: 2
393
});
394
assert.equal(pos.name, 'name1');
395
assert.equal(pos.line, 1);
396
assert.equal(pos.column, 1);
397
398
var pos = map.originalPositionFor({
399
line: 4,
400
column: 4
401
});
402
assert.equal(pos.name, 'name1');
403
assert.equal(pos.line, 3);
404
assert.equal(pos.column, 3);
405
406
var pos = map.originalPositionFor({
407
line: 6,
408
column: 6
409
});
410
assert.equal(pos.name, 'name3');
411
assert.equal(pos.line, 5);
412
assert.equal(pos.column, 5);
413
};
414
415
exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {
416
var smg = new SourceMapGenerator({
417
sourceRoot: 'http://example.com/',
418
file: 'foo.js'
419
});
420
smg.addMapping({
421
original: { line: 1, column: 1 },
422
generated: { line: 2, column: 2 },
423
source: 'bar.js'
424
});
425
smg.addMapping({
426
original: { line: 2, column: 2 },
427
generated: { line: 4, column: 4 },
428
source: 'baz.js',
429
name: 'dirtMcGirt'
430
});
431
smg.setSourceContent('baz.js', 'baz.js content');
432
433
var smc = SourceMapConsumer.fromSourceMap(smg);
434
assert.equal(smc.file, 'foo.js');
435
assert.equal(smc.sourceRoot, 'http://example.com/');
436
assert.equal(smc.sources.length, 2);
437
assert.equal(smc.sources[0], 'http://example.com/bar.js');
438
assert.equal(smc.sources[1], 'http://example.com/baz.js');
439
assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');
440
441
var pos = smc.originalPositionFor({
442
line: 2,
443
column: 2
444
});
445
assert.equal(pos.line, 1);
446
assert.equal(pos.column, 1);
447
assert.equal(pos.source, 'http://example.com/bar.js');
448
assert.equal(pos.name, null);
449
450
pos = smc.generatedPositionFor({
451
line: 1,
452
column: 1,
453
source: 'http://example.com/bar.js'
454
});
455
assert.equal(pos.line, 2);
456
assert.equal(pos.column, 2);
457
458
pos = smc.originalPositionFor({
459
line: 4,
460
column: 4
461
});
462
assert.equal(pos.line, 2);
463
assert.equal(pos.column, 2);
464
assert.equal(pos.source, 'http://example.com/baz.js');
465
assert.equal(pos.name, 'dirtMcGirt');
466
467
pos = smc.generatedPositionFor({
468
line: 2,
469
column: 2,
470
source: 'http://example.com/baz.js'
471
});
472
assert.equal(pos.line, 4);
473
assert.equal(pos.column, 4);
474
};
475
});
476
477