Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/*
2
* WARNING!
3
*
4
* Do not edit this file directly, it is built from the sources at
5
* https://github.com/mozilla/source-map/
6
*/
7
8
Components.utils.import('resource://test/Utils.jsm');
9
/* -*- Mode: js; js-indent-level: 2; -*- */
10
/*
11
* Copyright 2011 Mozilla Foundation and contributors
12
* Licensed under the New BSD license. See LICENSE or:
13
* http://opensource.org/licenses/BSD-3-Clause
14
*/
15
define("test/source-map/test-source-node", ["require", "exports", "module"], function (require, exports, module) {
16
17
var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
18
var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
19
var SourceNode = require('source-map/source-node').SourceNode;
20
21
function forEachNewline(fn) {
22
return function (assert, util) {
23
['\n', '\r\n'].forEach(fn.bind(null, assert, util));
24
}
25
}
26
27
exports['test .add()'] = function (assert, util) {
28
var node = new SourceNode(null, null, null);
29
30
// Adding a string works.
31
node.add('function noop() {}');
32
33
// Adding another source node works.
34
node.add(new SourceNode(null, null, null));
35
36
// Adding an array works.
37
node.add(['function foo() {',
38
new SourceNode(null, null, null,
39
'return 10;'),
40
'}']);
41
42
// Adding other stuff doesn't.
43
assert.throws(function () {
44
node.add({});
45
});
46
assert.throws(function () {
47
node.add(function () {});
48
});
49
};
50
51
exports['test .prepend()'] = function (assert, util) {
52
var node = new SourceNode(null, null, null);
53
54
// Prepending a string works.
55
node.prepend('function noop() {}');
56
assert.equal(node.children[0], 'function noop() {}');
57
assert.equal(node.children.length, 1);
58
59
// Prepending another source node works.
60
node.prepend(new SourceNode(null, null, null));
61
assert.equal(node.children[0], '');
62
assert.equal(node.children[1], 'function noop() {}');
63
assert.equal(node.children.length, 2);
64
65
// Prepending an array works.
66
node.prepend(['function foo() {',
67
new SourceNode(null, null, null,
68
'return 10;'),
69
'}']);
70
assert.equal(node.children[0], 'function foo() {');
71
assert.equal(node.children[1], 'return 10;');
72
assert.equal(node.children[2], '}');
73
assert.equal(node.children[3], '');
74
assert.equal(node.children[4], 'function noop() {}');
75
assert.equal(node.children.length, 5);
76
77
// Prepending other stuff doesn't.
78
assert.throws(function () {
79
node.prepend({});
80
});
81
assert.throws(function () {
82
node.prepend(function () {});
83
});
84
};
85
86
exports['test .toString()'] = function (assert, util) {
87
assert.equal((new SourceNode(null, null, null,
88
['function foo() {',
89
new SourceNode(null, null, null, 'return 10;'),
90
'}'])).toString(),
91
'function foo() {return 10;}');
92
};
93
94
exports['test .join()'] = function (assert, util) {
95
assert.equal((new SourceNode(null, null, null,
96
['a', 'b', 'c', 'd'])).join(', ').toString(),
97
'a, b, c, d');
98
};
99
100
exports['test .walk()'] = function (assert, util) {
101
var node = new SourceNode(null, null, null,
102
['(function () {\n',
103
' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
104
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
105
'}());']);
106
var expected = [
107
{ str: '(function () {\n', source: null, line: null, column: null },
108
{ str: ' ', source: null, line: null, column: null },
109
{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },
110
{ str: ';\n', source: null, line: null, column: null },
111
{ str: ' ', source: null, line: null, column: null },
112
{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },
113
{ str: ';\n', source: null, line: null, column: null },
114
{ str: '}());', source: null, line: null, column: null },
115
];
116
var i = 0;
117
node.walk(function (chunk, loc) {
118
assert.equal(expected[i].str, chunk);
119
assert.equal(expected[i].source, loc.source);
120
assert.equal(expected[i].line, loc.line);
121
assert.equal(expected[i].column, loc.column);
122
i++;
123
});
124
};
125
126
exports['test .replaceRight'] = function (assert, util) {
127
var node;
128
129
// Not nested
130
node = new SourceNode(null, null, null, 'hello world');
131
node.replaceRight(/world/, 'universe');
132
assert.equal(node.toString(), 'hello universe');
133
134
// Nested
135
node = new SourceNode(null, null, null,
136
[new SourceNode(null, null, null, 'hey sexy mama, '),
137
new SourceNode(null, null, null, 'want to kill all humans?')]);
138
node.replaceRight(/kill all humans/, 'watch Futurama');
139
assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
140
};
141
142
exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
143
var node = new SourceNode(null, null, null,
144
['(function () {' + nl,
145
' ',
146
new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
147
new SourceNode(1, 8, 'a.js', '()'),
148
';' + nl,
149
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,
150
'}());']);
151
var result = node.toStringWithSourceMap({
152
file: 'foo.js'
153
});
154
155
assert.equal(result.code, [
156
'(function () {',
157
' someCall();',
158
' if (foo) bar();',
159
'}());'
160
].join(nl));
161
162
var map = result.map;
163
var mapWithoutOptions = node.toStringWithSourceMap().map;
164
165
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
166
assert.ok(mapWithoutOptions instanceof SourceMapGenerator, 'mapWithoutOptions instanceof SourceMapGenerator');
167
assert.ok(!('file' in mapWithoutOptions));
168
mapWithoutOptions._file = 'foo.js';
169
util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());
170
171
map = new SourceMapConsumer(map.toString());
172
173
var actual;
174
175
actual = map.originalPositionFor({
176
line: 1,
177
column: 4
178
});
179
assert.equal(actual.source, null);
180
assert.equal(actual.line, null);
181
assert.equal(actual.column, null);
182
183
actual = map.originalPositionFor({
184
line: 2,
185
column: 2
186
});
187
assert.equal(actual.source, 'a.js');
188
assert.equal(actual.line, 1);
189
assert.equal(actual.column, 0);
190
assert.equal(actual.name, 'originalCall');
191
192
actual = map.originalPositionFor({
193
line: 3,
194
column: 2
195
});
196
assert.equal(actual.source, 'b.js');
197
assert.equal(actual.line, 2);
198
assert.equal(actual.column, 0);
199
200
actual = map.originalPositionFor({
201
line: 3,
202
column: 16
203
});
204
assert.equal(actual.source, null);
205
assert.equal(actual.line, null);
206
assert.equal(actual.column, null);
207
208
actual = map.originalPositionFor({
209
line: 4,
210
column: 2
211
});
212
assert.equal(actual.source, null);
213
assert.equal(actual.line, null);
214
assert.equal(actual.column, null);
215
});
216
217
exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
218
var testCode = util.testGeneratedCode.replace(/\n/g, nl);
219
var node = SourceNode.fromStringWithSourceMap(
220
testCode,
221
new SourceMapConsumer(util.testMap));
222
223
var result = node.toStringWithSourceMap({
224
file: 'min.js'
225
});
226
var map = result.map;
227
var code = result.code;
228
229
assert.equal(code, testCode);
230
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
231
map = map.toJSON();
232
assert.equal(map.version, util.testMap.version);
233
assert.equal(map.file, util.testMap.file);
234
assert.equal(map.mappings, util.testMap.mappings);
235
});
236
237
exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {
238
var node = SourceNode.fromStringWithSourceMap(
239
util.testGeneratedCode.replace(/\n/g, nl),
240
new SourceMapConsumer(util.emptyMap));
241
var result = node.toStringWithSourceMap({
242
file: 'min.js'
243
});
244
var map = result.map;
245
var code = result.code;
246
247
assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));
248
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
249
map = map.toJSON();
250
assert.equal(map.version, util.emptyMap.version);
251
assert.equal(map.file, util.emptyMap.file);
252
assert.equal(map.mappings.length, util.emptyMap.mappings.length);
253
assert.equal(map.mappings, util.emptyMap.mappings);
254
});
255
256
exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {
257
var input = new SourceNode(null, null, null, [
258
"(function() {" + nl,
259
" var Test = {};" + nl,
260
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),
261
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,
262
"}());" + nl,
263
"/* Generated Source */"]);
264
input = input.toStringWithSourceMap({
265
file: 'foo.js'
266
});
267
268
var node = SourceNode.fromStringWithSourceMap(
269
input.code,
270
new SourceMapConsumer(input.map.toString()));
271
272
var result = node.toStringWithSourceMap({
273
file: 'foo.js'
274
});
275
var map = result.map;
276
var code = result.code;
277
278
assert.equal(code, input.code);
279
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
280
map = map.toJSON();
281
var inputMap = input.map.toJSON();
282
util.assertEqualMaps(assert, map, inputMap);
283
});
284
285
exports['test .fromStringWithSourceMap() third argument'] = function (assert, util) {
286
// Assume the following directory structure:
287
//
288
// http://foo.org/
289
// bar.coffee
290
// app/
291
// coffee/
292
// foo.coffee
293
// coffeeBundle.js # Made from {foo,bar,baz}.coffee
294
// maps/
295
// coffeeBundle.js.map
296
// js/
297
// foo.js
298
// public/
299
// app.js # Made from {foo,coffeeBundle}.js
300
// app.js.map
301
//
302
// http://www.example.com/
303
// baz.coffee
304
305
var coffeeBundle = new SourceNode(1, 0, 'foo.coffee', 'foo(coffee);\n');
306
coffeeBundle.setSourceContent('foo.coffee', 'foo coffee');
307
coffeeBundle.add(new SourceNode(2, 0, '/bar.coffee', 'bar(coffee);\n'));
308
coffeeBundle.add(new SourceNode(3, 0, 'http://www.example.com/baz.coffee', 'baz(coffee);'));
309
coffeeBundle = coffeeBundle.toStringWithSourceMap({
310
file: 'foo.js',
311
sourceRoot: '..'
312
});
313
314
var foo = new SourceNode(1, 0, 'foo.js', 'foo(js);');
315
316
var test = function(relativePath, expectedSources) {
317
var app = new SourceNode();
318
app.add(SourceNode.fromStringWithSourceMap(
319
coffeeBundle.code,
320
new SourceMapConsumer(coffeeBundle.map.toString()),
321
relativePath));
322
app.add(foo);
323
var i = 0;
324
app.walk(function (chunk, loc) {
325
assert.equal(loc.source, expectedSources[i]);
326
i++;
327
});
328
app.walkSourceContents(function (sourceFile, sourceContent) {
329
assert.equal(sourceFile, expectedSources[0]);
330
assert.equal(sourceContent, 'foo coffee');
331
})
332
};
333
334
test('../coffee/maps', [
335
'../coffee/foo.coffee',
336
'/bar.coffee',
337
'http://www.example.com/baz.coffee',
338
'foo.js'
339
]);
340
341
// If the third parameter is omitted or set to the current working
342
// directory we get incorrect source paths:
343
344
test(undefined, [
345
'../foo.coffee',
346
'/bar.coffee',
347
'http://www.example.com/baz.coffee',
348
'foo.js'
349
]);
350
351
test('', [
352
'../foo.coffee',
353
'/bar.coffee',
354
'http://www.example.com/baz.coffee',
355
'foo.js'
356
]);
357
358
test('.', [
359
'../foo.coffee',
360
'/bar.coffee',
361
'http://www.example.com/baz.coffee',
362
'foo.js'
363
]);
364
365
test('./', [
366
'../foo.coffee',
367
'/bar.coffee',
368
'http://www.example.com/baz.coffee',
369
'foo.js'
370
]);
371
};
372
373
exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {
374
var input = new SourceNode(null, null, null, [
375
new SourceNode(1, 0, "a.js", "(function"),
376
new SourceNode(1, 0, "a.js", "() {" + nl),
377
" ",
378
new SourceNode(1, 0, "a.js", "var Test = "),
379
new SourceNode(1, 0, "b.js", "{};" + nl),
380
new SourceNode(2, 0, "b.js", "Test"),
381
new SourceNode(2, 0, "b.js", ".A", "A"),
382
new SourceNode(2, 20, "b.js", " = { value: ", "A"),
383
"1234",
384
new SourceNode(2, 40, "b.js", " };" + nl, "A"),
385
"}());" + nl,
386
"/* Generated Source */"
387
]);
388
input = input.toStringWithSourceMap({
389
file: 'foo.js'
390
});
391
392
assert.equal(input.code, [
393
"(function() {",
394
" var Test = {};",
395
"Test.A = { value: 1234 };",
396
"}());",
397
"/* Generated Source */"
398
].join(nl))
399
400
var correctMap = new SourceMapGenerator({
401
file: 'foo.js'
402
});
403
correctMap.addMapping({
404
generated: { line: 1, column: 0 },
405
source: 'a.js',
406
original: { line: 1, column: 0 }
407
});
408
// Here is no need for a empty mapping,
409
// because mappings ends at eol
410
correctMap.addMapping({
411
generated: { line: 2, column: 2 },
412
source: 'a.js',
413
original: { line: 1, column: 0 }
414
});
415
correctMap.addMapping({
416
generated: { line: 2, column: 13 },
417
source: 'b.js',
418
original: { line: 1, column: 0 }
419
});
420
correctMap.addMapping({
421
generated: { line: 3, column: 0 },
422
source: 'b.js',
423
original: { line: 2, column: 0 }
424
});
425
correctMap.addMapping({
426
generated: { line: 3, column: 4 },
427
source: 'b.js',
428
name: 'A',
429
original: { line: 2, column: 0 }
430
});
431
correctMap.addMapping({
432
generated: { line: 3, column: 6 },
433
source: 'b.js',
434
name: 'A',
435
original: { line: 2, column: 20 }
436
});
437
// This empty mapping is required,
438
// because there is a hole in the middle of the line
439
correctMap.addMapping({
440
generated: { line: 3, column: 18 }
441
});
442
correctMap.addMapping({
443
generated: { line: 3, column: 22 },
444
source: 'b.js',
445
name: 'A',
446
original: { line: 2, column: 40 }
447
});
448
// Here is no need for a empty mapping,
449
// because mappings ends at eol
450
451
var inputMap = input.map.toJSON();
452
correctMap = correctMap.toJSON();
453
util.assertEqualMaps(assert, inputMap, correctMap);
454
});
455
456
exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {
457
var input = new SourceNode(null, null, null, [
458
new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),
459
new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),
460
new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),
461
new SourceNode(2, 2, "b.js", "anotherLine();" + nl),
462
"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,
463
new SourceNode(3, 4, "c.js", "anotherLine();" + nl),
464
"/*" + nl + "Generated" + nl + "Source" + nl + "*/"
465
]);
466
input = input.toStringWithSourceMap({
467
file: 'foo.js'
468
});
469
470
assert.equal(input.code, [
471
"(function() {",
472
"var nextLine = 1;",
473
"anotherLine();",
474
"Test.call(this, 123);",
475
"this['stuff'] = 'v';",
476
"anotherLine();",
477
"/*",
478
"Generated",
479
"Source",
480
"*/",
481
"anotherLine();",
482
"/*",
483
"Generated",
484
"Source",
485
"*/"
486
].join(nl));
487
488
var correctMap = new SourceMapGenerator({
489
file: 'foo.js'
490
});
491
correctMap.addMapping({
492
generated: { line: 1, column: 0 },
493
source: 'a.js',
494
original: { line: 1, column: 0 }
495
});
496
correctMap.addMapping({
497
generated: { line: 2, column: 0 },
498
source: 'a.js',
499
original: { line: 1, column: 0 }
500
});
501
correctMap.addMapping({
502
generated: { line: 3, column: 0 },
503
source: 'a.js',
504
original: { line: 1, column: 0 }
505
});
506
correctMap.addMapping({
507
generated: { line: 4, column: 0 },
508
source: 'b.js',
509
original: { line: 2, column: 2 }
510
});
511
correctMap.addMapping({
512
generated: { line: 5, column: 0 },
513
source: 'b.js',
514
original: { line: 2, column: 2 }
515
});
516
correctMap.addMapping({
517
generated: { line: 6, column: 0 },
518
source: 'b.js',
519
original: { line: 2, column: 2 }
520
});
521
correctMap.addMapping({
522
generated: { line: 11, column: 0 },
523
source: 'c.js',
524
original: { line: 3, column: 4 }
525
});
526
527
var inputMap = input.map.toJSON();
528
correctMap = correctMap.toJSON();
529
util.assertEqualMaps(assert, inputMap, correctMap);
530
});
531
532
exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {
533
var node = new SourceNode(1, 0, 'empty.js', '');
534
var result = node.toStringWithSourceMap();
535
assert.equal(result.code, '');
536
};
537
538
exports['test .toStringWithSourceMap() with consecutive newlines'] = forEachNewline(function (assert, util, nl) {
539
var input = new SourceNode(null, null, null, [
540
"/***/" + nl + nl,
541
new SourceNode(1, 0, "a.js", "'use strict';" + nl),
542
new SourceNode(2, 0, "a.js", "a();"),
543
]);
544
input = input.toStringWithSourceMap({
545
file: 'foo.js'
546
});
547
548
assert.equal(input.code, [
549
"/***/",
550
"",
551
"'use strict';",
552
"a();",
553
].join(nl));
554
555
var correctMap = new SourceMapGenerator({
556
file: 'foo.js'
557
});
558
correctMap.addMapping({
559
generated: { line: 3, column: 0 },
560
source: 'a.js',
561
original: { line: 1, column: 0 }
562
});
563
correctMap.addMapping({
564
generated: { line: 4, column: 0 },
565
source: 'a.js',
566
original: { line: 2, column: 0 }
567
});
568
569
var inputMap = input.map.toJSON();
570
correctMap = correctMap.toJSON();
571
util.assertEqualMaps(assert, inputMap, correctMap);
572
});
573
574
exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
575
var aNode = new SourceNode(1, 1, 'a.js', 'a');
576
aNode.setSourceContent('a.js', 'someContent');
577
var node = new SourceNode(null, null, null,
578
['(function () {\n',
579
' ', aNode,
580
' ', new SourceNode(1, 1, 'b.js', 'b'),
581
'}());']);
582
node.setSourceContent('b.js', 'otherContent');
583
var map = node.toStringWithSourceMap({
584
file: 'foo.js'
585
}).map;
586
587
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
588
map = new SourceMapConsumer(map.toString());
589
590
assert.equal(map.sources.length, 2);
591
assert.equal(map.sources[0], 'a.js');
592
assert.equal(map.sources[1], 'b.js');
593
assert.equal(map.sourcesContent.length, 2);
594
assert.equal(map.sourcesContent[0], 'someContent');
595
assert.equal(map.sourcesContent[1], 'otherContent');
596
};
597
598
exports['test walkSourceContents'] = function (assert, util) {
599
var aNode = new SourceNode(1, 1, 'a.js', 'a');
600
aNode.setSourceContent('a.js', 'someContent');
601
var node = new SourceNode(null, null, null,
602
['(function () {\n',
603
' ', aNode,
604
' ', new SourceNode(1, 1, 'b.js', 'b'),
605
'}());']);
606
node.setSourceContent('b.js', 'otherContent');
607
var results = [];
608
node.walkSourceContents(function (sourceFile, sourceContent) {
609
results.push([sourceFile, sourceContent]);
610
});
611
assert.equal(results.length, 2);
612
assert.equal(results[0][0], 'a.js');
613
assert.equal(results[0][1], 'someContent');
614
assert.equal(results[1][0], 'b.js');
615
assert.equal(results[1][1], 'otherContent');
616
};
617
});
618
function run_test() {
619
runSourceMapTests('test/source-map/test-source-node', do_throw);
620
}
621
622