@@ -17,8 +17,14 @@ const acorn = require('acorn');
17
17
const escodegen = require ( 'escodegen' ) ;
18
18
const convert = require ( 'convert-source-map' ) ;
19
19
const { transfer } = require ( 'multi-stage-sourcemap' ) ;
20
- const { unassertAst } = require ( 'unassert' ) ;
20
+ const { unassertAst, defaultOptions } = require ( 'unassert' ) ;
21
21
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
+ } ) ;
22
28
23
29
function mergeSourceMap ( incomingSourceMap , outgoingSourceMap ) {
24
30
if ( typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String ) {
@@ -53,15 +59,15 @@ function handleIncomingSourceMap (originalCode) {
53
59
return null ;
54
60
}
55
61
56
- function applyUnassertWithSourceMap ( code , filepath ) {
62
+ function applyUnassertWithSourceMap ( code , filepath , unassertOptions ) {
57
63
const ast = acorn . parse ( code , {
58
64
sourceType : 'module' ,
59
65
ecmaVersion : 'latest' ,
60
66
locations : true ,
61
67
allowHashBang : true
62
68
} ) ;
63
69
const inMap = handleIncomingSourceMap ( code ) ;
64
- const instrumented = escodegen . generate ( unassertAst ( ast ) , {
70
+ const instrumented = escodegen . generate ( unassertAst ( ast , unassertOptions ) , {
65
71
sourceMap : filepath ,
66
72
sourceContent : code ,
67
73
sourceMapWithCode : true
@@ -75,21 +81,20 @@ function applyUnassertWithSourceMap (code, filepath) {
75
81
}
76
82
}
77
83
78
- function applyUnassertWithoutSourceMap ( code ) {
84
+ function applyUnassertWithoutSourceMap ( code , unassertOptions ) {
79
85
const ast = acorn . parse ( code , {
80
86
sourceType : 'module' ,
81
87
ecmaVersion : 'latest' ,
82
88
allowHashBang : true
83
89
} ) ;
84
- return escodegen . generate ( unassertAst ( ast ) ) ;
90
+ return escodegen . generate ( unassertAst ( ast , unassertOptions ) ) ;
85
91
}
86
92
87
93
function shouldProduceSourceMap ( options ) {
88
94
return ( options && options . _flags && options . _flags . debug ) ;
89
95
}
90
96
91
97
function containsAssertions ( src ) {
92
- // Matches both `assert` and `power-assert`.
93
98
return src . indexOf ( 'assert' ) !== - 1 ;
94
99
}
95
100
@@ -109,9 +114,9 @@ module.exports = function unassertify (filepath, options) {
109
114
if ( ! containsAssertions ( data ) ) {
110
115
stream . queue ( data ) ;
111
116
} else if ( shouldProduceSourceMap ( options ) ) {
112
- stream . queue ( applyUnassertWithSourceMap ( data , filepath ) ) ;
117
+ stream . queue ( applyUnassertWithSourceMap ( data , filepath , unassertifyOptions ) ) ;
113
118
} else {
114
- stream . queue ( applyUnassertWithoutSourceMap ( data ) ) ;
119
+ stream . queue ( applyUnassertWithoutSourceMap ( data , unassertifyOptions ) ) ;
115
120
}
116
121
stream . queue ( null ) ;
117
122
}
0 commit comments