Skip to content

Commit 47578f7

Browse files
committed
refactor: define assert explicitly as target variable to work well with containsAssertions optimization
1 parent e596118 commit 47578f7

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ const acorn = require('acorn');
1717
const escodegen = require('escodegen');
1818
const convert = require('convert-source-map');
1919
const { transfer } = require('multi-stage-sourcemap');
20-
const { unassertAst } = require('unassert');
20+
const { unassertAst, defaultOptions } = require('unassert');
2121
const hasOwn = Object.prototype.hasOwnProperty;
22+
// define `assert` explicitly as target variable to work well with `containsAssertions` optimization
23+
const unassertifyOptions = Object.assign(defaultOptions(), {
24+
variables: [
25+
'assert'
26+
]
27+
});
2228

2329
function mergeSourceMap (incomingSourceMap, outgoingSourceMap) {
2430
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
@@ -53,15 +59,15 @@ function handleIncomingSourceMap (originalCode) {
5359
return null;
5460
}
5561

56-
function applyUnassertWithSourceMap (code, filepath) {
62+
function applyUnassertWithSourceMap (code, filepath, unassertOptions) {
5763
const ast = acorn.parse(code, {
5864
sourceType: 'module',
5965
ecmaVersion: 'latest',
6066
locations: true,
6167
allowHashBang: true
6268
});
6369
const inMap = handleIncomingSourceMap(code);
64-
const instrumented = escodegen.generate(unassertAst(ast), {
70+
const instrumented = escodegen.generate(unassertAst(ast, unassertOptions), {
6571
sourceMap: filepath,
6672
sourceContent: code,
6773
sourceMapWithCode: true
@@ -75,21 +81,20 @@ function applyUnassertWithSourceMap (code, filepath) {
7581
}
7682
}
7783

78-
function applyUnassertWithoutSourceMap (code) {
84+
function applyUnassertWithoutSourceMap (code, unassertOptions) {
7985
const ast = acorn.parse(code, {
8086
sourceType: 'module',
8187
ecmaVersion: 'latest',
8288
allowHashBang: true
8389
});
84-
return escodegen.generate(unassertAst(ast));
90+
return escodegen.generate(unassertAst(ast, unassertOptions));
8591
}
8692

8793
function shouldProduceSourceMap (options) {
8894
return (options && options._flags && options._flags.debug);
8995
}
9096

9197
function containsAssertions (src) {
92-
// Matches both `assert` and `power-assert`.
9398
return src.indexOf('assert') !== -1;
9499
}
95100

@@ -109,9 +114,9 @@ module.exports = function unassertify (filepath, options) {
109114
if (!containsAssertions(data)) {
110115
stream.queue(data);
111116
} else if (shouldProduceSourceMap(options)) {
112-
stream.queue(applyUnassertWithSourceMap(data, filepath));
117+
stream.queue(applyUnassertWithSourceMap(data, filepath, unassertifyOptions));
113118
} else {
114-
stream.queue(applyUnassertWithoutSourceMap(data));
119+
stream.queue(applyUnassertWithoutSourceMap(data, unassertifyOptions));
115120
}
116121
stream.queue(null);
117122
}

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