Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 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 util = require('../../lib/source-map/util');
13
14
// This is a test mapping which maps functions from two different files
15
// (one.js and two.js) to a minified generated source.
16
//
17
// Here is one.js:
18
//
19
// ONE.foo = function (bar) {
20
// return baz(bar);
21
// };
22
//
23
// Here is two.js:
24
//
25
// TWO.inc = function (n) {
26
// return n + 1;
27
// };
28
//
29
// And here is the generated code (min.js):
30
//
31
// ONE.foo=function(a){return baz(a);};
32
// TWO.inc=function(a){return a+1;};
33
exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+
34
" TWO.inc=function(a){return a+1;};";
35
exports.testMap = {
36
version: 3,
37
file: 'min.js',
38
names: ['bar', 'baz', 'n'],
39
sources: ['one.js', 'two.js'],
40
sourceRoot: '/the/root',
41
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
42
};
43
exports.testMapNoSourceRoot = {
44
version: 3,
45
file: 'min.js',
46
names: ['bar', 'baz', 'n'],
47
sources: ['one.js', 'two.js'],
48
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
49
};
50
exports.testMapEmptySourceRoot = {
51
version: 3,
52
file: 'min.js',
53
names: ['bar', 'baz', 'n'],
54
sources: ['one.js', 'two.js'],
55
sourceRoot: '',
56
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
57
};
58
// This mapping is identical to above, but uses the indexed format instead.
59
exports.indexedTestMap = {
60
version: 3,
61
file: 'min.js',
62
sections: [
63
{
64
offset: {
65
line: 0,
66
column: 0
67
},
68
map: {
69
version: 3,
70
sources: [
71
"one.js"
72
],
73
sourcesContent: [
74
' ONE.foo = function (bar) {\n' +
75
' return baz(bar);\n' +
76
' };',
77
],
78
names: [
79
"bar",
80
"baz"
81
],
82
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID",
83
file: "min.js",
84
sourceRoot: "/the/root"
85
}
86
},
87
{
88
offset: {
89
line: 1,
90
column: 0
91
},
92
map: {
93
version: 3,
94
sources: [
95
"two.js"
96
],
97
sourcesContent: [
98
' TWO.inc = function (n) {\n' +
99
' return n + 1;\n' +
100
' };'
101
],
102
names: [
103
"n"
104
],
105
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
106
file: "min.js",
107
sourceRoot: "/the/root"
108
}
109
}
110
]
111
};
112
exports.indexedTestMapDifferentSourceRoots = {
113
version: 3,
114
file: 'min.js',
115
sections: [
116
{
117
offset: {
118
line: 0,
119
column: 0
120
},
121
map: {
122
version: 3,
123
sources: [
124
"one.js"
125
],
126
sourcesContent: [
127
' ONE.foo = function (bar) {\n' +
128
' return baz(bar);\n' +
129
' };',
130
],
131
names: [
132
"bar",
133
"baz"
134
],
135
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID",
136
file: "min.js",
137
sourceRoot: "/the/root"
138
}
139
},
140
{
141
offset: {
142
line: 1,
143
column: 0
144
},
145
map: {
146
version: 3,
147
sources: [
148
"two.js"
149
],
150
sourcesContent: [
151
' TWO.inc = function (n) {\n' +
152
' return n + 1;\n' +
153
' };'
154
],
155
names: [
156
"n"
157
],
158
mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
159
file: "min.js",
160
sourceRoot: "/different/root"
161
}
162
}
163
]
164
};
165
exports.testMapWithSourcesContent = {
166
version: 3,
167
file: 'min.js',
168
names: ['bar', 'baz', 'n'],
169
sources: ['one.js', 'two.js'],
170
sourcesContent: [
171
' ONE.foo = function (bar) {\n' +
172
' return baz(bar);\n' +
173
' };',
174
' TWO.inc = function (n) {\n' +
175
' return n + 1;\n' +
176
' };'
177
],
178
sourceRoot: '/the/root',
179
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
180
};
181
exports.testMapRelativeSources = {
182
version: 3,
183
file: 'min.js',
184
names: ['bar', 'baz', 'n'],
185
sources: ['./one.js', './two.js'],
186
sourcesContent: [
187
' ONE.foo = function (bar) {\n' +
188
' return baz(bar);\n' +
189
' };',
190
' TWO.inc = function (n) {\n' +
191
' return n + 1;\n' +
192
' };'
193
],
194
sourceRoot: '/the/root',
195
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
196
};
197
exports.emptyMap = {
198
version: 3,
199
file: 'min.js',
200
names: [],
201
sources: [],
202
mappings: ''
203
};
204
205
206
function assertMapping(generatedLine, generatedColumn, originalSource,
207
originalLine, originalColumn, name, map, assert,
208
dontTestGenerated, dontTestOriginal) {
209
if (!dontTestOriginal) {
210
var origMapping = map.originalPositionFor({
211
line: generatedLine,
212
column: generatedColumn
213
});
214
assert.equal(origMapping.name, name,
215
'Incorrect name, expected ' + JSON.stringify(name)
216
+ ', got ' + JSON.stringify(origMapping.name));
217
assert.equal(origMapping.line, originalLine,
218
'Incorrect line, expected ' + JSON.stringify(originalLine)
219
+ ', got ' + JSON.stringify(origMapping.line));
220
assert.equal(origMapping.column, originalColumn,
221
'Incorrect column, expected ' + JSON.stringify(originalColumn)
222
+ ', got ' + JSON.stringify(origMapping.column));
223
224
var expectedSource;
225
226
if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
227
expectedSource = originalSource;
228
} else if (originalSource) {
229
expectedSource = map.sourceRoot
230
? util.join(map.sourceRoot, originalSource)
231
: originalSource;
232
} else {
233
expectedSource = null;
234
}
235
236
assert.equal(origMapping.source, expectedSource,
237
'Incorrect source, expected ' + JSON.stringify(expectedSource)
238
+ ', got ' + JSON.stringify(origMapping.source));
239
}
240
241
if (!dontTestGenerated) {
242
var genMapping = map.generatedPositionFor({
243
source: originalSource,
244
line: originalLine,
245
column: originalColumn
246
});
247
assert.equal(genMapping.line, generatedLine,
248
'Incorrect line, expected ' + JSON.stringify(generatedLine)
249
+ ', got ' + JSON.stringify(genMapping.line));
250
assert.equal(genMapping.column, generatedColumn,
251
'Incorrect column, expected ' + JSON.stringify(generatedColumn)
252
+ ', got ' + JSON.stringify(genMapping.column));
253
}
254
}
255
exports.assertMapping = assertMapping;
256
257
function assertEqualMaps(assert, actualMap, expectedMap) {
258
assert.equal(actualMap.version, expectedMap.version, "version mismatch");
259
assert.equal(actualMap.file, expectedMap.file, "file mismatch");
260
assert.equal(actualMap.names.length,
261
expectedMap.names.length,
262
"names length mismatch: " +
263
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
264
for (var i = 0; i < actualMap.names.length; i++) {
265
assert.equal(actualMap.names[i],
266
expectedMap.names[i],
267
"names[" + i + "] mismatch: " +
268
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
269
}
270
assert.equal(actualMap.sources.length,
271
expectedMap.sources.length,
272
"sources length mismatch: " +
273
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
274
for (var i = 0; i < actualMap.sources.length; i++) {
275
assert.equal(actualMap.sources[i],
276
expectedMap.sources[i],
277
"sources[" + i + "] length mismatch: " +
278
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
279
}
280
assert.equal(actualMap.sourceRoot,
281
expectedMap.sourceRoot,
282
"sourceRoot mismatch: " +
283
actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
284
assert.equal(actualMap.mappings, expectedMap.mappings,
285
"mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
286
if (actualMap.sourcesContent) {
287
assert.equal(actualMap.sourcesContent.length,
288
expectedMap.sourcesContent.length,
289
"sourcesContent length mismatch");
290
for (var i = 0; i < actualMap.sourcesContent.length; i++) {
291
assert.equal(actualMap.sourcesContent[i],
292
expectedMap.sourcesContent[i],
293
"sourcesContent[" + i + "] mismatch");
294
}
295
}
296
}
297
exports.assertEqualMaps = assertEqualMaps;
298
299
});
300
301