Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80766 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 SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
13
var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
14
var SourceNode = require('../../lib/source-map/source-node').SourceNode;
15
16
function forEachNewline(fn) {
17
return function (assert, util) {
18
['\n', '\r\n'].forEach(fn.bind(null, assert, util));
19
}
20
}
21
22
exports['test .add()'] = function (assert, util) {
23
var node = new SourceNode(null, null, null);
24
25
// Adding a string works.
26
node.add('function noop() {}');
27
28
// Adding another source node works.
29
node.add(new SourceNode(null, null, null));
30
31
// Adding an array works.
32
node.add(['function foo() {',
33
new SourceNode(null, null, null,
34
'return 10;'),
35
'}']);
36
37
// Adding other stuff doesn't.
38
assert.throws(function () {
39
node.add({});
40
});
41
assert.throws(function () {
42
node.add(function () {});
43
});
44
};
45
46
exports['test .prepend()'] = function (assert, util) {
47
var node = new SourceNode(null, null, null);
48
49
// Prepending a string works.
50
node.prepend('function noop() {}');
51
assert.equal(node.children[0], 'function noop() {}');
52
assert.equal(node.children.length, 1);
53
54
// Prepending another source node works.
55
node.prepend(new SourceNode(null, null, null));
56
assert.equal(node.children[0], '');
57
assert.equal(node.children[1], 'function noop() {}');
58
assert.equal(node.children.length, 2);
59
60
// Prepending an array works.
61
node.prepend(['function foo() {',
62
new SourceNode(null, null, null,
63
'return 10;'),
64
'}']);
65
assert.equal(node.children[0], 'function foo() {');
66
assert.equal(node.children[1], 'return 10;');
67
assert.equal(node.children[2], '}');
68
assert.equal(node.children[3], '');
69
assert.equal(node.children[4], 'function noop() {}');
70
assert.equal(node.children.length, 5);
71
72
// Prepending other stuff doesn't.
73
assert.throws(function () {
74
node.prepend({});
75
});
76
assert.throws(function () {
77
node.prepend(function () {});
78
});
79
};
80
81
exports['test .toString()'] = function (assert, util) {
82
assert.equal((new SourceNode(null, null, null,
83
['function foo() {',
84
new SourceNode(null, null, null, 'return 10;'),
85
'}'])).toString(),
86
'function foo() {return 10;}');
87
};
88
89
exports['test .join()'] = function (assert, util) {
90
assert.equal((new SourceNode(null, null, null,
91
['a', 'b', 'c', 'd'])).join(', ').toString(),
92
'a, b, c, d');
93
};
94
95
exports['test .walk()'] = function (assert, util) {
96
var node = new SourceNode(null, null, null,
97
['(function () {\n',
98
' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
99
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
100
'}());']);
101
var expected = [
102
{ str: '(function () {\n', source: null, line: null, column: null },
103
{ str: ' ', source: null, line: null, column: null },
104
{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },
105
{ str: ';\n', source: null, line: null, column: null },
106
{ str: ' ', source: null, line: null, column: null },
107
{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },
108
{ str: ';\n', source: null, line: null, column: null },
109
{ str: '}());', source: null, line: null, column: null },
110
];
111
var i = 0;
112
node.walk(function (chunk, loc) {
113
assert.equal(expected[i].str, chunk);
114
assert.equal(expected[i].source, loc.source);
115
assert.equal(expected[i].line, loc.line);
116
assert.equal(expected[i].column, loc.column);
117
i++;
118
});
119
};
120
121
exports['test .replaceRight'] = function (assert, util) {
122
var node;
123
124
// Not nested
125
node = new SourceNode(null, null, null, 'hello world');
126
node.replaceRight(/world/, 'universe');
127
assert.equal(node.toString(), 'hello universe');
128
129
// Nested
130
node = new SourceNode(null, null, null,
131
[new SourceNode(null, null, null, 'hey sexy mama, '),
132
new SourceNode(null, null, null, 'want to kill all humans?')]);
133
node.replaceRight(/kill all humans/, 'watch Futurama');
134
assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
135
};
136
137
exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
138
var node = new SourceNode(null, null, null,
139
['(function () {' + nl,
140
' ',
141
new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
142
new SourceNode(1, 8, 'a.js', '()'),
143
';' + nl,
144
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,
145
'}());']);
146
var result = node.toStringWithSourceMap({
147
file: 'foo.js'
148
});
149
150
assert.equal(result.code, [
151
'(function () {',
152
' someCall();',
153
' if (foo) bar();',
154
'}());'
155
].join(nl));
156
157
var map = result.map;
158
var mapWithoutOptions = node.toStringWithSourceMap().map;
159
160
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
161
assert.ok(mapWithoutOptions instanceof SourceMapGenerator, 'mapWithoutOptions instanceof SourceMapGenerator');
162
mapWithoutOptions._file = 'foo.js';
163
util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());
164
165
map = new SourceMapConsumer(map.toString());
166
167
var actual;
168
169
actual = map.originalPositionFor({
170
line: 1,
171
column: 4
172
});
173
assert.equal(actual.source, null);
174
assert.equal(actual.line, null);
175
assert.equal(actual.column, null);
176
177
actual = map.originalPositionFor({
178
line: 2,
179
column: 2
180
});
181
assert.equal(actual.source, 'a.js');
182
assert.equal(actual.line, 1);
183
assert.equal(actual.column, 0);
184
assert.equal(actual.name, 'originalCall');
185
186
actual = map.originalPositionFor({
187
line: 3,
188
column: 2
189
});
190
assert.equal(actual.source, 'b.js');
191
assert.equal(actual.line, 2);
192
assert.equal(actual.column, 0);
193
194
actual = map.originalPositionFor({
195
line: 3,
196
column: 16
197
});
198
assert.equal(actual.source, null);
199
assert.equal(actual.line, null);
200
assert.equal(actual.column, null);
201
202
actual = map.originalPositionFor({
203
line: 4,
204
column: 2
205
});
206
assert.equal(actual.source, null);
207
assert.equal(actual.line, null);
208
assert.equal(actual.column, null);
209
});
210
211
exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
212
var testCode = util.testGeneratedCode.replace(/\n/g, nl);
213
var node = SourceNode.fromStringWithSourceMap(
214
testCode,
215
new SourceMapConsumer(util.testMap));
216
217
var result = node.toStringWithSourceMap({
218
file: 'min.js'
219
});
220
var map = result.map;
221
var code = result.code;
222
223
assert.equal(code, testCode);
224
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
225
map = map.toJSON();
226
assert.equal(map.version, util.testMap.version);
227
assert.equal(map.file, util.testMap.file);
228
assert.equal(map.mappings, util.testMap.mappings);
229
});
230
231
exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {
232
var node = SourceNode.fromStringWithSourceMap(
233
util.testGeneratedCode.replace(/\n/g, nl),
234
new SourceMapConsumer(util.emptyMap));
235
var result = node.toStringWithSourceMap({
236
file: 'min.js'
237
});
238
var map = result.map;
239
var code = result.code;
240
241
assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));
242
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
243
map = map.toJSON();
244
assert.equal(map.version, util.emptyMap.version);
245
assert.equal(map.file, util.emptyMap.file);
246
assert.equal(map.mappings.length, util.emptyMap.mappings.length);
247
assert.equal(map.mappings, util.emptyMap.mappings);
248
});
249
250
exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {
251
var input = new SourceNode(null, null, null, [
252
"(function() {" + nl,
253
" var Test = {};" + nl,
254
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),
255
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,
256
"}());" + nl,
257
"/* Generated Source */"]);
258
input = input.toStringWithSourceMap({
259
file: 'foo.js'
260
});
261
262
var node = SourceNode.fromStringWithSourceMap(
263
input.code,
264
new SourceMapConsumer(input.map.toString()));
265
266
var result = node.toStringWithSourceMap({
267
file: 'foo.js'
268
});
269
var map = result.map;
270
var code = result.code;
271
272
assert.equal(code, input.code);
273
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
274
map = map.toJSON();
275
var inputMap = input.map.toJSON();
276
util.assertEqualMaps(assert, map, inputMap);
277
});
278
279
exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {
280
var input = new SourceNode(null, null, null, [
281
new SourceNode(1, 0, "a.js", "(function"),
282
new SourceNode(1, 0, "a.js", "() {" + nl),
283
" ",
284
new SourceNode(1, 0, "a.js", "var Test = "),
285
new SourceNode(1, 0, "b.js", "{};" + nl),
286
new SourceNode(2, 0, "b.js", "Test"),
287
new SourceNode(2, 0, "b.js", ".A", "A"),
288
new SourceNode(2, 20, "b.js", " = { value: ", "A"),
289
"1234",
290
new SourceNode(2, 40, "b.js", " };" + nl, "A"),
291
"}());" + nl,
292
"/* Generated Source */"
293
]);
294
input = input.toStringWithSourceMap({
295
file: 'foo.js'
296
});
297
298
assert.equal(input.code, [
299
"(function() {",
300
" var Test = {};",
301
"Test.A = { value: 1234 };",
302
"}());",
303
"/* Generated Source */"
304
].join(nl))
305
306
var correctMap = new SourceMapGenerator({
307
file: 'foo.js'
308
});
309
correctMap.addMapping({
310
generated: { line: 1, column: 0 },
311
source: 'a.js',
312
original: { line: 1, column: 0 }
313
});
314
// Here is no need for a empty mapping,
315
// because mappings ends at eol
316
correctMap.addMapping({
317
generated: { line: 2, column: 2 },
318
source: 'a.js',
319
original: { line: 1, column: 0 }
320
});
321
correctMap.addMapping({
322
generated: { line: 2, column: 13 },
323
source: 'b.js',
324
original: { line: 1, column: 0 }
325
});
326
correctMap.addMapping({
327
generated: { line: 3, column: 0 },
328
source: 'b.js',
329
original: { line: 2, column: 0 }
330
});
331
correctMap.addMapping({
332
generated: { line: 3, column: 4 },
333
source: 'b.js',
334
name: 'A',
335
original: { line: 2, column: 0 }
336
});
337
correctMap.addMapping({
338
generated: { line: 3, column: 6 },
339
source: 'b.js',
340
name: 'A',
341
original: { line: 2, column: 20 }
342
});
343
// This empty mapping is required,
344
// because there is a hole in the middle of the line
345
correctMap.addMapping({
346
generated: { line: 3, column: 18 }
347
});
348
correctMap.addMapping({
349
generated: { line: 3, column: 22 },
350
source: 'b.js',
351
name: 'A',
352
original: { line: 2, column: 40 }
353
});
354
// Here is no need for a empty mapping,
355
// because mappings ends at eol
356
357
var inputMap = input.map.toJSON();
358
correctMap = correctMap.toJSON();
359
util.assertEqualMaps(assert, inputMap, correctMap);
360
});
361
362
exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {
363
var input = new SourceNode(null, null, null, [
364
new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),
365
new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),
366
new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),
367
new SourceNode(2, 2, "b.js", "anotherLine();" + nl),
368
"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,
369
new SourceNode(3, 4, "c.js", "anotherLine();" + nl),
370
"/*" + nl + "Generated" + nl + "Source" + nl + "*/"
371
]);
372
input = input.toStringWithSourceMap({
373
file: 'foo.js'
374
});
375
376
assert.equal(input.code, [
377
"(function() {",
378
"var nextLine = 1;",
379
"anotherLine();",
380
"Test.call(this, 123);",
381
"this['stuff'] = 'v';",
382
"anotherLine();",
383
"/*",
384
"Generated",
385
"Source",
386
"*/",
387
"anotherLine();",
388
"/*",
389
"Generated",
390
"Source",
391
"*/"
392
].join(nl));
393
394
var correctMap = new SourceMapGenerator({
395
file: 'foo.js'
396
});
397
correctMap.addMapping({
398
generated: { line: 1, column: 0 },
399
source: 'a.js',
400
original: { line: 1, column: 0 }
401
});
402
correctMap.addMapping({
403
generated: { line: 2, column: 0 },
404
source: 'a.js',
405
original: { line: 1, column: 0 }
406
});
407
correctMap.addMapping({
408
generated: { line: 3, column: 0 },
409
source: 'a.js',
410
original: { line: 1, column: 0 }
411
});
412
correctMap.addMapping({
413
generated: { line: 4, column: 0 },
414
source: 'b.js',
415
original: { line: 2, column: 2 }
416
});
417
correctMap.addMapping({
418
generated: { line: 5, column: 0 },
419
source: 'b.js',
420
original: { line: 2, column: 2 }
421
});
422
correctMap.addMapping({
423
generated: { line: 6, column: 0 },
424
source: 'b.js',
425
original: { line: 2, column: 2 }
426
});
427
correctMap.addMapping({
428
generated: { line: 11, column: 0 },
429
source: 'c.js',
430
original: { line: 3, column: 4 }
431
});
432
433
var inputMap = input.map.toJSON();
434
correctMap = correctMap.toJSON();
435
util.assertEqualMaps(assert, inputMap, correctMap);
436
});
437
438
exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {
439
var node = new SourceNode(1, 0, 'empty.js', '');
440
var result = node.toStringWithSourceMap();
441
assert.equal(result.code, '');
442
};
443
444
exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
445
var aNode = new SourceNode(1, 1, 'a.js', 'a');
446
aNode.setSourceContent('a.js', 'someContent');
447
var node = new SourceNode(null, null, null,
448
['(function () {\n',
449
' ', aNode,
450
' ', new SourceNode(1, 1, 'b.js', 'b'),
451
'}());']);
452
node.setSourceContent('b.js', 'otherContent');
453
var map = node.toStringWithSourceMap({
454
file: 'foo.js'
455
}).map;
456
457
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
458
map = new SourceMapConsumer(map.toString());
459
460
assert.equal(map.sources.length, 2);
461
assert.equal(map.sources[0], 'a.js');
462
assert.equal(map.sources[1], 'b.js');
463
assert.equal(map.sourcesContent.length, 2);
464
assert.equal(map.sourcesContent[0], 'someContent');
465
assert.equal(map.sourcesContent[1], 'otherContent');
466
};
467
468
exports['test walkSourceContents'] = function (assert, util) {
469
var aNode = new SourceNode(1, 1, 'a.js', 'a');
470
aNode.setSourceContent('a.js', 'someContent');
471
var node = new SourceNode(null, null, null,
472
['(function () {\n',
473
' ', aNode,
474
' ', new SourceNode(1, 1, 'b.js', 'b'),
475
'}());']);
476
node.setSourceContent('b.js', 'otherContent');
477
var results = [];
478
node.walkSourceContents(function (sourceFile, sourceContent) {
479
results.push([sourceFile, sourceContent]);
480
});
481
assert.equal(results.length, 2);
482
assert.equal(results[0][0], 'a.js');
483
assert.equal(results[0][1], 'someContent');
484
assert.equal(results[1][0], 'b.js');
485
assert.equal(results[1][1], 'otherContent');
486
};
487
});
488
489