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 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
exports.testMapWithSourcesContent = {
59
version: 3,
60
file: 'min.js',
61
names: ['bar', 'baz', 'n'],
62
sources: ['one.js', 'two.js'],
63
sourcesContent: [
64
' ONE.foo = function (bar) {\n' +
65
' return baz(bar);\n' +
66
' };',
67
' TWO.inc = function (n) {\n' +
68
' return n + 1;\n' +
69
' };'
70
],
71
sourceRoot: '/the/root',
72
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
73
};
74
exports.testMapRelativeSources = {
75
version: 3,
76
file: 'min.js',
77
names: ['bar', 'baz', 'n'],
78
sources: ['./one.js', './two.js'],
79
sourcesContent: [
80
' ONE.foo = function (bar) {\n' +
81
' return baz(bar);\n' +
82
' };',
83
' TWO.inc = function (n) {\n' +
84
' return n + 1;\n' +
85
' };'
86
],
87
sourceRoot: '/the/root',
88
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
89
};
90
exports.emptyMap = {
91
version: 3,
92
file: 'min.js',
93
names: [],
94
sources: [],
95
mappings: ''
96
};
97
98
99
function assertMapping(generatedLine, generatedColumn, originalSource,
100
originalLine, originalColumn, name, map, assert,
101
dontTestGenerated, dontTestOriginal) {
102
if (!dontTestOriginal) {
103
var origMapping = map.originalPositionFor({
104
line: generatedLine,
105
column: generatedColumn
106
});
107
assert.equal(origMapping.name, name,
108
'Incorrect name, expected ' + JSON.stringify(name)
109
+ ', got ' + JSON.stringify(origMapping.name));
110
assert.equal(origMapping.line, originalLine,
111
'Incorrect line, expected ' + JSON.stringify(originalLine)
112
+ ', got ' + JSON.stringify(origMapping.line));
113
assert.equal(origMapping.column, originalColumn,
114
'Incorrect column, expected ' + JSON.stringify(originalColumn)
115
+ ', got ' + JSON.stringify(origMapping.column));
116
117
var expectedSource;
118
119
if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
120
expectedSource = originalSource;
121
} else if (originalSource) {
122
expectedSource = map.sourceRoot
123
? util.join(map.sourceRoot, originalSource)
124
: originalSource;
125
} else {
126
expectedSource = null;
127
}
128
129
assert.equal(origMapping.source, expectedSource,
130
'Incorrect source, expected ' + JSON.stringify(expectedSource)
131
+ ', got ' + JSON.stringify(origMapping.source));
132
}
133
134
if (!dontTestGenerated) {
135
var genMapping = map.generatedPositionFor({
136
source: originalSource,
137
line: originalLine,
138
column: originalColumn
139
});
140
assert.equal(genMapping.line, generatedLine,
141
'Incorrect line, expected ' + JSON.stringify(generatedLine)
142
+ ', got ' + JSON.stringify(genMapping.line));
143
assert.equal(genMapping.column, generatedColumn,
144
'Incorrect column, expected ' + JSON.stringify(generatedColumn)
145
+ ', got ' + JSON.stringify(genMapping.column));
146
}
147
}
148
exports.assertMapping = assertMapping;
149
150
function assertEqualMaps(assert, actualMap, expectedMap) {
151
assert.equal(actualMap.version, expectedMap.version, "version mismatch");
152
assert.equal(actualMap.file, expectedMap.file, "file mismatch");
153
assert.equal(actualMap.names.length,
154
expectedMap.names.length,
155
"names length mismatch: " +
156
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
157
for (var i = 0; i < actualMap.names.length; i++) {
158
assert.equal(actualMap.names[i],
159
expectedMap.names[i],
160
"names[" + i + "] mismatch: " +
161
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
162
}
163
assert.equal(actualMap.sources.length,
164
expectedMap.sources.length,
165
"sources length mismatch: " +
166
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
167
for (var i = 0; i < actualMap.sources.length; i++) {
168
assert.equal(actualMap.sources[i],
169
expectedMap.sources[i],
170
"sources[" + i + "] length mismatch: " +
171
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
172
}
173
assert.equal(actualMap.sourceRoot,
174
expectedMap.sourceRoot,
175
"sourceRoot mismatch: " +
176
actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
177
assert.equal(actualMap.mappings, expectedMap.mappings,
178
"mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
179
if (actualMap.sourcesContent) {
180
assert.equal(actualMap.sourcesContent.length,
181
expectedMap.sourcesContent.length,
182
"sourcesContent length mismatch");
183
for (var i = 0; i < actualMap.sourcesContent.length; i++) {
184
assert.equal(actualMap.sourcesContent[i],
185
expectedMap.sourcesContent[i],
186
"sourcesContent[" + i + "] mismatch");
187
}
188
}
189
}
190
exports.assertEqualMaps = assertEqualMaps;
191
192
});
193
194