Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80540 views
1
'use strict';
2
/*jshint asi: true */
3
4
var test = require('tap').test;
5
var convert = require('convert-source-map');
6
var commentRegex = require('convert-source-map').commentRegex;
7
var combine = require('..');
8
var mappingsFromMap = require('../lib/mappings-from-map');
9
10
function checkMappings(foo, sm, lineOffset) {
11
function inspect(obj, depth) {
12
return require('util').inspect(obj, false, depth || 5, true);
13
}
14
15
var fooMappings = mappingsFromMap(foo);
16
var mappings = mappingsFromMap(sm);
17
18
var genLinesOffset = true;
19
var origLinesSame = true;
20
for (var i = 0; i < mappings.length; i++) {
21
var fooGen = fooMappings[i].generated;
22
var fooOrig = fooMappings[i].original;
23
var gen = mappings[i].generated
24
var orig = mappings[i].original;
25
26
if (gen.column !== fooGen.column || gen.line !== (fooGen.line + lineOffset)) {
27
console.error(
28
'generated mapping at %s not offset properly:\ninput: [%s]\noutput:[%s]\n\n',
29
i ,
30
inspect(fooGen),
31
inspect(gen)
32
);
33
genLinesOffset = false;
34
}
35
36
if (orig.column !== fooOrig.column || orig.line !== fooOrig.line) {
37
console.error(
38
'original mapping at %s is not the same as the genrated mapping:\ninput: [%s]\noutput:[%s]\n\n',
39
i ,
40
inspect(fooOrig),
41
inspect(orig)
42
);
43
origLinesSame = false;
44
}
45
}
46
return { genLinesOffset: genLinesOffset, origLinesSame: origLinesSame };
47
}
48
49
var foo = {
50
version : 3,
51
file : 'foo.js',
52
sourceRoot : '',
53
sources : [ 'foo.coffee' ],
54
names : [],
55
mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',
56
sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };
57
58
test('add one file with inlined source', function (t) {
59
60
var mapComment = convert.fromObject(foo).toComment();
61
var file = {
62
id: 'xyz'
63
, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
64
, sourceFile: 'foo.js'
65
};
66
67
var lineOffset = 3
68
var base64 = combine.create()
69
.addFile(file, { line: lineOffset })
70
.base64()
71
72
var sm = convert.fromBase64(base64).toObject();
73
var res = checkMappings(foo, sm, lineOffset);
74
75
t.ok(res.genLinesOffset, 'all generated lines are offset properly and columns unchanged')
76
t.ok(res.origLinesSame, 'all original lines and columns are unchanged')
77
t.equal(sm.sourcesContent[0], foo.sourcesContent[0], 'includes the original source')
78
t.equal(sm.sources[0], 'foo.coffee', 'includes original filename')
79
t.end()
80
});
81
82
83
test('add one file without inlined source', function (t) {
84
85
var mapComment = convert
86
.fromObject(foo)
87
.setProperty('sourcesContent', [])
88
.toComment();
89
90
var file = {
91
id: 'xyz'
92
, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
93
, sourceFile: 'foo.js'
94
};
95
96
var lineOffset = 3
97
var base64 = combine.create()
98
.addFile(file, { line: lineOffset })
99
.base64()
100
101
var sm = convert.fromBase64(base64).toObject();
102
var mappings = mappingsFromMap(sm);
103
104
t.equal(sm.sourcesContent[0], file.source, 'includes the generated source')
105
t.equal(sm.sources[0], 'foo.js', 'includes generated filename')
106
107
t.deepEqual(
108
mappings
109
, [ { generated: { line: 4, column: 0 },
110
original: { line: 1, column: 0 },
111
source: 'foo.js', name: undefined },
112
{ generated: { line: 5, column: 0 },
113
original: { line: 2, column: 0 },
114
source: 'foo.js', name: undefined },
115
{ generated: { line: 6, column: 0 },
116
original: { line: 3, column: 0 },
117
source: 'foo.js', name: undefined },
118
{ generated: { line: 7, column: 0 },
119
original: { line: 4, column: 0 },
120
source: 'foo.js', name: undefined },
121
{ generated: { line: 8, column: 0 },
122
original: { line: 5, column: 0 },
123
source: 'foo.js', name: undefined },
124
{ generated: { line: 9, column: 0 },
125
original: { line: 6, column: 0 },
126
source: 'foo.js', name: undefined },
127
{ generated: { line: 10, column: 0 },
128
original: { line: 7, column: 0 },
129
source: 'foo.js', name: undefined } ]
130
, 'generates mappings offset by the given line'
131
)
132
t.end()
133
})
134
135
test('remove comments', function (t) {
136
var mapComment = convert.fromObject(foo).toComment();
137
138
function sourcemapComments(src) {
139
var matches = src.match(commentRegex);
140
return matches ? matches.length : 0;
141
}
142
143
t.equal(sourcemapComments('var a = 1;\n' + mapComment), 1);
144
145
[ ''
146
, 'var a = 1;\n' + mapComment
147
, 'var a = 1;\n' + mapComment + '\nvar b = 5;\n' + mapComment
148
] .forEach(function (x) {
149
var removed = combine.removeComments(x)
150
t.equal(sourcemapComments(removed), 0)
151
})
152
t.end()
153
})
154
155