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 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
exports['test .add()'] = function (assert, util) {
17
var node = new SourceNode(null, null, null);
18
19
// Adding a string works.
20
node.add('function noop() {}');
21
22
// Adding another source node works.
23
node.add(new SourceNode(null, null, null));
24
25
// Adding an array works.
26
node.add(['function foo() {',
27
new SourceNode(null, null, null,
28
'return 10;'),
29
'}']);
30
31
// Adding other stuff doesn't.
32
assert.throws(function () {
33
node.add({});
34
});
35
assert.throws(function () {
36
node.add(function () {});
37
});
38
};
39
40
exports['test .prepend()'] = function (assert, util) {
41
var node = new SourceNode(null, null, null);
42
43
// Prepending a string works.
44
node.prepend('function noop() {}');
45
assert.equal(node.children[0], 'function noop() {}');
46
assert.equal(node.children.length, 1);
47
48
// Prepending another source node works.
49
node.prepend(new SourceNode(null, null, null));
50
assert.equal(node.children[0], '');
51
assert.equal(node.children[1], 'function noop() {}');
52
assert.equal(node.children.length, 2);
53
54
// Prepending an array works.
55
node.prepend(['function foo() {',
56
new SourceNode(null, null, null,
57
'return 10;'),
58
'}']);
59
assert.equal(node.children[0], 'function foo() {');
60
assert.equal(node.children[1], 'return 10;');
61
assert.equal(node.children[2], '}');
62
assert.equal(node.children[3], '');
63
assert.equal(node.children[4], 'function noop() {}');
64
assert.equal(node.children.length, 5);
65
66
// Prepending other stuff doesn't.
67
assert.throws(function () {
68
node.prepend({});
69
});
70
assert.throws(function () {
71
node.prepend(function () {});
72
});
73
};
74
75
exports['test .toString()'] = function (assert, util) {
76
assert.equal((new SourceNode(null, null, null,
77
['function foo() {',
78
new SourceNode(null, null, null, 'return 10;'),
79
'}'])).toString(),
80
'function foo() {return 10;}');
81
};
82
83
exports['test .join()'] = function (assert, util) {
84
assert.equal((new SourceNode(null, null, null,
85
['a', 'b', 'c', 'd'])).join(', ').toString(),
86
'a, b, c, d');
87
};
88
89
exports['test .walk()'] = function (assert, util) {
90
var node = new SourceNode(null, null, null,
91
['(function () {\n',
92
' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
93
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
94
'}());']);
95
var expected = [
96
{ str: '(function () {\n', source: null, line: null, column: null },
97
{ str: ' ', source: null, line: null, column: null },
98
{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },
99
{ str: ';\n', source: null, line: null, column: null },
100
{ str: ' ', source: null, line: null, column: null },
101
{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },
102
{ str: ';\n', source: null, line: null, column: null },
103
{ str: '}());', source: null, line: null, column: null },
104
];
105
var i = 0;
106
node.walk(function (chunk, loc) {
107
assert.equal(expected[i].str, chunk);
108
assert.equal(expected[i].source, loc.source);
109
assert.equal(expected[i].line, loc.line);
110
assert.equal(expected[i].column, loc.column);
111
i++;
112
});
113
};
114
115
exports['test .replaceRight'] = function (assert, util) {
116
var node;
117
118
// Not nested
119
node = new SourceNode(null, null, null, 'hello world');
120
node.replaceRight(/world/, 'universe');
121
assert.equal(node.toString(), 'hello universe');
122
123
// Nested
124
node = new SourceNode(null, null, null,
125
[new SourceNode(null, null, null, 'hey sexy mama, '),
126
new SourceNode(null, null, null, 'want to kill all humans?')]);
127
node.replaceRight(/kill all humans/, 'watch Futurama');
128
assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
129
};
130
131
exports['test .toStringWithSourceMap()'] = function (assert, util) {
132
var node = new SourceNode(null, null, null,
133
['(function () {\n',
134
' ',
135
new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
136
new SourceNode(1, 8, 'a.js', '()'),
137
';\n',
138
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
139
'}());']);
140
var map = node.toStringWithSourceMap({
141
file: 'foo.js'
142
}).map;
143
144
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
145
map = new SourceMapConsumer(map.toString());
146
147
var actual;
148
149
actual = map.originalPositionFor({
150
line: 1,
151
column: 4
152
});
153
assert.equal(actual.source, null);
154
assert.equal(actual.line, null);
155
assert.equal(actual.column, null);
156
157
actual = map.originalPositionFor({
158
line: 2,
159
column: 2
160
});
161
assert.equal(actual.source, 'a.js');
162
assert.equal(actual.line, 1);
163
assert.equal(actual.column, 0);
164
assert.equal(actual.name, 'originalCall');
165
166
actual = map.originalPositionFor({
167
line: 3,
168
column: 2
169
});
170
assert.equal(actual.source, 'b.js');
171
assert.equal(actual.line, 2);
172
assert.equal(actual.column, 0);
173
174
actual = map.originalPositionFor({
175
line: 3,
176
column: 16
177
});
178
assert.equal(actual.source, null);
179
assert.equal(actual.line, null);
180
assert.equal(actual.column, null);
181
182
actual = map.originalPositionFor({
183
line: 4,
184
column: 2
185
});
186
assert.equal(actual.source, null);
187
assert.equal(actual.line, null);
188
assert.equal(actual.column, null);
189
};
190
191
exports['test .fromStringWithSourceMap()'] = function (assert, util) {
192
var node = SourceNode.fromStringWithSourceMap(
193
util.testGeneratedCode,
194
new SourceMapConsumer(util.testMap));
195
196
var result = node.toStringWithSourceMap({
197
file: 'min.js'
198
});
199
var map = result.map;
200
var code = result.code;
201
202
assert.equal(code, util.testGeneratedCode);
203
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
204
map = map.toJSON();
205
assert.equal(map.version, util.testMap.version);
206
assert.equal(map.file, util.testMap.file);
207
assert.equal(map.mappings, util.testMap.mappings);
208
};
209
210
exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) {
211
var node = SourceNode.fromStringWithSourceMap(
212
util.testGeneratedCode,
213
new SourceMapConsumer(util.emptyMap));
214
var result = node.toStringWithSourceMap({
215
file: 'min.js'
216
});
217
var map = result.map;
218
var code = result.code;
219
220
assert.equal(code, util.testGeneratedCode);
221
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
222
map = map.toJSON();
223
assert.equal(map.version, util.emptyMap.version);
224
assert.equal(map.file, util.emptyMap.file);
225
assert.equal(map.mappings.length, util.emptyMap.mappings.length);
226
assert.equal(map.mappings, util.emptyMap.mappings);
227
};
228
229
exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) {
230
var input = new SourceNode(null, null, null, [
231
"(function() {\n",
232
" var Test = {};\n",
233
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"),
234
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n",
235
"}());\n",
236
"/* Generated Source */"]);
237
input = input.toStringWithSourceMap({
238
file: 'foo.js'
239
});
240
241
var node = SourceNode.fromStringWithSourceMap(
242
input.code,
243
new SourceMapConsumer(input.map.toString()));
244
245
var result = node.toStringWithSourceMap({
246
file: 'foo.js'
247
});
248
var map = result.map;
249
var code = result.code;
250
251
assert.equal(code, input.code);
252
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
253
map = map.toJSON();
254
var inputMap = input.map.toJSON();
255
util.assertEqualMaps(assert, map, inputMap);
256
};
257
258
exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) {
259
var input = new SourceNode(null, null, null, [
260
new SourceNode(1, 0, "a.js", "(function"),
261
new SourceNode(1, 0, "a.js", "() {\n"),
262
" ",
263
new SourceNode(1, 0, "a.js", "var Test = "),
264
new SourceNode(1, 0, "b.js", "{};\n"),
265
new SourceNode(2, 0, "b.js", "Test"),
266
new SourceNode(2, 0, "b.js", ".A", "A"),
267
new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"),
268
"}());\n",
269
"/* Generated Source */"
270
]);
271
input = input.toStringWithSourceMap({
272
file: 'foo.js'
273
});
274
275
var correctMap = new SourceMapGenerator({
276
file: 'foo.js'
277
});
278
correctMap.addMapping({
279
generated: { line: 1, column: 0 },
280
source: 'a.js',
281
original: { line: 1, column: 0 }
282
});
283
correctMap.addMapping({
284
generated: { line: 2, column: 0 }
285
});
286
correctMap.addMapping({
287
generated: { line: 2, column: 2 },
288
source: 'a.js',
289
original: { line: 1, column: 0 }
290
});
291
correctMap.addMapping({
292
generated: { line: 2, column: 13 },
293
source: 'b.js',
294
original: { line: 1, column: 0 }
295
});
296
correctMap.addMapping({
297
generated: { line: 3, column: 0 },
298
source: 'b.js',
299
original: { line: 2, column: 0 }
300
});
301
correctMap.addMapping({
302
generated: { line: 3, column: 4 },
303
source: 'b.js',
304
name: 'A',
305
original: { line: 2, column: 0 }
306
});
307
correctMap.addMapping({
308
generated: { line: 3, column: 6 },
309
source: 'b.js',
310
name: 'A',
311
original: { line: 2, column: 20 }
312
});
313
correctMap.addMapping({
314
generated: { line: 4, column: 0 }
315
});
316
317
var inputMap = input.map.toJSON();
318
correctMap = correctMap.toJSON();
319
util.assertEqualMaps(assert, correctMap, inputMap);
320
};
321
322
exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
323
var aNode = new SourceNode(1, 1, 'a.js', 'a');
324
aNode.setSourceContent('a.js', 'someContent');
325
var node = new SourceNode(null, null, null,
326
['(function () {\n',
327
' ', aNode,
328
' ', new SourceNode(1, 1, 'b.js', 'b'),
329
'}());']);
330
node.setSourceContent('b.js', 'otherContent');
331
var map = node.toStringWithSourceMap({
332
file: 'foo.js'
333
}).map;
334
335
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
336
map = new SourceMapConsumer(map.toString());
337
338
assert.equal(map.sources.length, 2);
339
assert.equal(map.sources[0], 'a.js');
340
assert.equal(map.sources[1], 'b.js');
341
assert.equal(map.sourcesContent.length, 2);
342
assert.equal(map.sourcesContent[0], 'someContent');
343
assert.equal(map.sourcesContent[1], 'otherContent');
344
};
345
346
exports['test walkSourceContents'] = function (assert, util) {
347
var aNode = new SourceNode(1, 1, 'a.js', 'a');
348
aNode.setSourceContent('a.js', 'someContent');
349
var node = new SourceNode(null, null, null,
350
['(function () {\n',
351
' ', aNode,
352
' ', new SourceNode(1, 1, 'b.js', 'b'),
353
'}());']);
354
node.setSourceContent('b.js', 'otherContent');
355
var results = [];
356
node.walkSourceContents(function (sourceFile, sourceContent) {
357
results.push([sourceFile, sourceContent]);
358
});
359
assert.equal(results.length, 2);
360
assert.equal(results[0][0], 'a.js');
361
assert.equal(results[0][1], 'someContent');
362
assert.equal(results[1][0], 'b.js');
363
assert.equal(results[1][1], 'otherContent');
364
};
365
});
366
367