/
index.js
243 lines (227 loc) · 8.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* espower-source - Power Assert instrumentor from source to source.
*
* https://github.com/power-assert-js/espower-source
*
* Copyright (c) 2014-2018 Takuto Wada
* Licensed under the MIT license.
* https://github.com/power-assert-js/espower-source/blob/master/MIT-LICENSE.txt
*/
'use strict';
var espower = require('espower');
var acorn = require('acorn');
require('acorn-es7-plugin')(acorn);
var estraverse = require('estraverse');
var mergeVisitors = require('merge-estraverse-visitors');
var empowerAssert = require('empower-assert');
var escodegen = require('escodegen');
var extend = require('xtend');
var convert = require('convert-source-map');
var transfer = require('multi-stage-sourcemap').transfer;
var _path = require('path');
var isAbsolute = require('path-is-absolute');
function mergeSourceMap (incomingSourceMap, outgoingSourceMap) {
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
outgoingSourceMap = JSON.parse(outgoingSourceMap);
}
if (!incomingSourceMap) {
return outgoingSourceMap;
}
return JSON.parse(transfer({fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap}));
}
function copyPropertyIfExists (name, from, to) {
if (from[name]) {
to.setProperty(name, from[name]);
}
}
function reconnectSourceMap (inMap, outMap) {
var mergedRawMap = mergeSourceMap(inMap, outMap.toObject());
var reMap = convert.fromObject(mergedRawMap);
copyPropertyIfExists('sources', inMap, reMap);
copyPropertyIfExists('sourceRoot', inMap, reMap);
copyPropertyIfExists('sourcesContent', inMap, reMap);
return reMap;
}
function handleIncomingSourceMap (originalCode, options) {
var inMap;
if (options.sourceMap) {
if (typeof options.sourceMap === 'string' || options.sourceMap instanceof String) {
options.sourceMap = JSON.parse(options.sourceMap);
}
inMap = options.sourceMap;
} else {
var sourceMappingURL = retrieveSourceMapURL(originalCode);
var commented;
// relative file sourceMap
// //# sourceMappingURL=foo.js.map or /*# sourceMappingURL=foo.js.map */
if (sourceMappingURL && !/^data:application\/json[^,]+base64,/.test(sourceMappingURL)) {
commented = convert.fromMapFileSource(originalCode, _path.dirname(options.path));
} else {
// inline sourceMap or none sourceMap
commented = convert.fromSource(originalCode);
}
if (commented) {
inMap = commented.toObject();
options.sourceMap = inMap;
}
}
return inMap;
}
// copy from https://github.com/evanw/node-source-map-support/blob/master/source-map-support.js#L99
function retrieveSourceMapURL(source) {
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;
// Keep executing the search to find the *last* sourceMappingURL to avoid
// picking up sourceMappingURLs from comments, strings, etc.
var lastMatch, match;
while (match = re.exec(source)) {
lastMatch = match;
}
if (!lastMatch) {
return null;
}
return lastMatch[1];
};
function adjustFilepath (filepath, sourceRoot) {
if (!sourceRoot || !isAbsolute(filepath)) {
return filepath;
}
return _path.relative(sourceRoot, filepath);
}
function syntaxErrorLine(lineNumber, maxLineNumberLength, line) {
var content = [' '];
var lineNumberString = String(lineNumber);
for (var i = lineNumberString.length; i < maxLineNumberLength; i++) {
content.push(' ');
}
content.push(lineNumberString, ': ', line);
return content.join('');
}
function showSyntaxErrorDetail(code, error, filepath) {
var i;
var begin = code.lastIndexOf('\n', error.pos);
var end = code.indexOf('\n', error.pos);
if (end === -1) {
end = undefined;
}
var line = code.slice(begin + 1, end);
var beforeLines = [];
for (i = 0; i < 5; i++) {
if (begin === -1) {
break;
}
var lastBegin = begin;
begin = code.lastIndexOf('\n', begin - 1);
beforeLines.unshift(code.slice(begin + 1, lastBegin));
if (begin === 0) {
break;
}
}
var afterLines = [];
for (i = 0; i < 5; i++) {
if (end === undefined) {
break;
}
var lastEnd = end;
end = code.indexOf('\n', end + 1);
if (end === -1) {
end = undefined;
}
afterLines.push(code.slice(lastEnd + 1, end));
}
var lines = [''];
var numberLength = String(error.loc.line + afterLines.length).length;
for (i = 0; i < beforeLines.length; i++) {
lines.push(
syntaxErrorLine(error.loc.line - beforeLines.length + i, numberLength, beforeLines[i])
);
}
lines.push(syntaxErrorLine(error.loc.line, numberLength, line));
var lineContent = [];
for (i = 0; i < 6 + numberLength + error.loc.column - 1; i++) {
lineContent.push(' ');
}
lineContent.push('^');
lines.push(lineContent.join(''));
for (i = 0; i < afterLines.length; i++) {
lines.push(
syntaxErrorLine(error.loc.line + i + 1, numberLength, afterLines[i])
);
}
lines.push('', 'Parse Error: ' + error.message + (filepath ? ' in ' + filepath : ''));
var detail = lines.join('\n');
var err = new SyntaxError(detail);
err.loc = error.loc;
err.pos = error.pos;
err.raisedAt = error.pos;
throw err;
}
function instrument (originalCode, filepath, options) {
var jsAst;
try {
jsAst = acorn.parse(originalCode, {locations: true, ecmaVersion: options.ecmaVersion, sourceType: options.sourceType, plugins: {asyncawait: true}});
} catch (e) {
if (e instanceof SyntaxError && e.pos && e.loc) {
showSyntaxErrorDetail(originalCode, e, filepath);
}
throw e;
}
var modifiedAst = estraverse.replace(jsAst, mergeVisitors([
{
enter: empowerAssert.enter
},
espower.createVisitor(jsAst, options)
]));
var escodegenOptions = extend({
sourceMap: adjustFilepath(filepath || options.path, options.sourceRoot),
sourceContent: originalCode,
sourceMapWithCode: true
});
if (options.sourceRoot) {
escodegenOptions.sourceMapRoot = options.sourceRoot;
}
return escodegen.generate(modifiedAst, escodegenOptions);
}
function instrumentWithoutSourceMapOutput (originalCode, options) {
var jsAst;
try {
jsAst = acorn.parse(originalCode, {locations: true, ecmaVersion: options.ecmaVersion, sourceType: options.sourceType, plugins: {asyncawait: true}});
} catch (e) {
if (e instanceof SyntaxError && e.pos && e.loc) {
showSyntaxErrorDetail(originalCode, e);
}
throw e;
}
var modifiedAst = estraverse.replace(jsAst, mergeVisitors([
{
enter: empowerAssert.enter
},
espower.createVisitor(jsAst, options)
]));
return escodegen.generate(modifiedAst);
}
function mergeEspowerOptions (options, filepath) {
return extend(espower.defaultOptions(), {
ecmaVersion: 2018,
sourceType: 'module',
path: filepath
}, options);
}
module.exports = function espowerSource (originalCode, filepath, options) {
if (typeof originalCode === 'undefined' || originalCode === null) {
throw new espower.EspowerError('`originalCode` is not specified', espowerSource);
}
var espowerOptions = mergeEspowerOptions(options, filepath);
var inMap = handleIncomingSourceMap(originalCode, espowerOptions);
if (!(filepath || espowerOptions.path)) {
return instrumentWithoutSourceMapOutput(originalCode, espowerOptions);
}
var instrumented = instrument(originalCode, filepath, espowerOptions);
var outMap = convert.fromJSON(instrumented.map.toString());
if (inMap) {
var reMap = reconnectSourceMap(inMap, outMap);
return instrumented.code + '\n' + reMap.toComment() + '\n';
} else {
return instrumented.code + '\n' + outMap.toComment() + '\n';
}
};