Skip to content

Commit da63931

Browse files
committed
feat(unassertify): remove assertions when debug:true as well
1 parent a325632 commit da63931

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

index.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,65 @@
1313
var through = require('through');
1414
var esprima = require('esprima');
1515
var escodegen = require('escodegen');
16+
var convert = require('convert-source-map');
17+
var transfer = require('multi-stage-sourcemap').transfer;
1618
var unassert = require('unassert');
1719

18-
function isDebugMode (options) {
19-
return (options && options._flags && options._flags.debug);
20+
function mergeSourceMap (incomingSourceMap, outgoingSourceMap) {
21+
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
22+
outgoingSourceMap = JSON.parse(outgoingSourceMap);
23+
}
24+
if (!incomingSourceMap) {
25+
return outgoingSourceMap;
26+
}
27+
return JSON.parse(transfer({fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap}));
2028
}
2129

22-
function applyUnassert (code, options) {
30+
function handleIncomingSourceMap (originalCode) {
31+
var commented = convert.fromSource(originalCode);
32+
if (commented) {
33+
return commented.toObject();
34+
}
35+
return null;
36+
}
37+
38+
function applyUnassertWithSourceMap (code, filepath, options) {
39+
var ast = esprima.parse(code, { sourceType: 'module' });
40+
var inMap = handleIncomingSourceMap(code);
41+
var instrumented = escodegen.generate(unassert(ast), {
42+
sourceMap: filepath,
43+
sourceContent: code,
44+
sourceMapWithCode: true
45+
});
46+
var outMap = convert.fromJSON(instrumented.map.toString());
47+
if (inMap) {
48+
var mergedRawMap = mergeSourceMap(inMap, outMap.toObject());
49+
var reMap = convert.fromObject(mergedRawMap);
50+
if (inMap.sources) {
51+
reMap.setProperty('sources', inMap.sources);
52+
}
53+
if (inMap.sourceRoot) {
54+
reMap.setProperty('sourceRoot', inMap.sourceRoot);
55+
}
56+
if (inMap.sourcesContent) {
57+
reMap.setProperty('sourcesContent', inMap.sourcesContent);
58+
}
59+
return instrumented.code + '\n' + reMap.toComment() + '\n';
60+
} else {
61+
return instrumented.code + '\n' + outMap.toComment() + '\n';
62+
}
63+
}
64+
65+
function applyUnassertWithoutSourceMap (code, filepath, options) {
2366
var ast = esprima.parse(code, { sourceType: 'module' });
2467
return escodegen.generate(unassert(ast));
2568
}
2669

27-
module.exports = function unassertify (filepath, options) {
28-
if (isDebugMode(options)) {
29-
return through();
30-
}
70+
function isDebugMode (options) {
71+
return (options && options._flags && options._flags.debug);
72+
}
3173

74+
module.exports = function unassertify (filepath, options) {
3275
var data = '',
3376
stream = through(write, end);
3477

@@ -37,7 +80,11 @@ module.exports = function unassertify (filepath, options) {
3780
}
3881

3982
function end() {
40-
stream.queue(applyUnassert(data, options));
83+
if (isDebugMode(options)) {
84+
stream.queue(applyUnassertWithSourceMap(data, filepath, options));
85+
} else {
86+
stream.queue(applyUnassertWithoutSourceMap(data, filepath, options));
87+
}
4188
stream.queue(null);
4289
}
4390

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
},
1010
"bugs": "https://github.com/twada/unassertify/issues",
1111
"dependencies": {
12+
"convert-source-map": "^1.1.1",
1213
"escodegen": "^1.6.1",
1314
"esprima": "^2.2.0",
15+
"multi-stage-sourcemap": "^0.2.1",
1416
"through": "^2.3.7",
1517
"unassert": "^1.1.0"
1618
},

test/fixtures/func/expected-with-sourcemap.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('unassertify', function () {
2525
});
2626

2727

28-
describe('do nothing if debug: true', function() {
28+
describe('adjust sourcemap if debug: true', function() {
2929
var stream = unassertify(
3030
'/absolute/path/to/test/fixtures/func/fixture.js',
3131
{
@@ -47,7 +47,7 @@ describe('do nothing if debug: true', function() {
4747
output += buf;
4848
});
4949
stream.on('end', function() {
50-
var expected = fs.readFileSync('test/fixtures/func/fixture.js', 'utf8');
50+
var expected = fs.readFileSync('test/fixtures/func/expected-with-sourcemap.js', 'utf8');
5151
assert.equal(output, expected);
5252
done();
5353
});
@@ -57,7 +57,7 @@ describe('do nothing if debug: true', function() {
5757
});
5858

5959

60-
describe('remove assertions if debug: false', function() {
60+
describe('just remove assertions if debug: false', function() {
6161
var stream = unassertify(
6262
'/absolute/path/to/test/fixtures/func/fixture.js',
6363
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy