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