forked from unassert-js/unassertify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (105 loc) · 3.42 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
/**
* unassertify
* Browserify transform for unassert
* Encourages programming with assertions by providing tools to compile them away.
*
* https://github.com/unassert-js/unassertify
*
* Copyright (c) 2015-2018 Takuto Wada
* Licensed under the MIT license.
* https://github.com/unassert-js/unassertify/blob/master/LICENSE
*/
'use strict';
var path = require('path');
var through = require('through');
var acorn = require('acorn');
var escodegen = require('escodegen');
var convert = require('convert-source-map');
var transfer = require('multi-stage-sourcemap').transfer;
var unassert = require('unassert');
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 overwritePropertyIfExists (name, from, to) {
if (from.hasOwnProperty(name)) {
to.setProperty(name, from[name]);
}
}
function reconnectSourceMap (inMap, outMap) {
var mergedRawMap = mergeSourceMap(inMap, outMap.toObject());
var reMap = convert.fromObject(mergedRawMap);
overwritePropertyIfExists('sources', inMap, reMap);
overwritePropertyIfExists('sourceRoot', inMap, reMap);
overwritePropertyIfExists('sourcesContent', inMap, reMap);
return reMap;
}
function handleIncomingSourceMap (originalCode) {
var commented = convert.fromSource(originalCode);
if (commented) {
return commented.toObject();
}
return null;
}
function applyUnassertWithSourceMap (code, filepath) {
var ast = acorn.parse(code, {
sourceType: 'module',
ecmaVersion: 2018,
locations: true,
allowHashBang: true
});
var inMap = handleIncomingSourceMap(code);
var instrumented = escodegen.generate(unassert(ast), {
sourceMap: filepath,
sourceContent: code,
sourceMapWithCode: true
});
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';
}
}
function applyUnassertWithoutSourceMap (code) {
var ast = acorn.parse(code, {
sourceType: 'module',
ecmaVersion: 2018,
allowHashBang: true
});
return escodegen.generate(unassert(ast));
}
function shouldProduceSourceMap (options) {
return (options && options._flags && options._flags.debug);
}
function containsAssertions (src) {
// Matches both `assert` and `power-assert`.
return src.indexOf('assert') !== -1;
}
module.exports = function unassertify (filepath, options) {
if (path.extname(filepath) === '.json') {
return through();
}
var data = '',
stream = through(write, end);
function write(buf) {
data += buf;
}
function end() {
if (!containsAssertions(data)) {
stream.queue(data);
} else if (shouldProduceSourceMap(options)) {
stream.queue(applyUnassertWithSourceMap(data, filepath));
} else {
stream.queue(applyUnassertWithoutSourceMap(data));
}
stream.queue(null);
}
return stream;
};