6
6
// Dependencies
7
7
// ------------
8
8
9
- var { createInstrumenter } = require ( 'istanbul-lib-instrument' )
10
- var minimatch = require ( 'minimatch' )
11
- var path = require ( 'path' )
12
- var _ = require ( 'lodash' )
13
- var SourceMapConsumer = require ( 'source-map' ) . SourceMapConsumer
14
- var SourceMapGenerator = require ( 'source-map' ) . SourceMapGenerator
15
- var globalSourceMapStore = require ( './source-map-store' )
16
- var globalCoverageMap = require ( './coverage-map' )
9
+ const { createInstrumenter } = require ( 'istanbul-lib-instrument' )
10
+ const minimatch = require ( 'minimatch' )
11
+ const path = require ( 'path' )
12
+ const { SourceMapConsumer, SourceMapGenerator } = require ( 'source-map' )
13
+ const globalSourceMapStore = require ( './source-map-store' )
14
+ const globalCoverageMap = require ( './coverage-map' )
17
15
18
16
// Regexes
19
17
// -------
20
18
21
- var coverageObjRegex = / \{ .* " p a t h " .* " f n M a p " .* " s t a t e m e n t M a p " .* " b r a n c h M a p " .* \} / g
19
+ const coverageObjRegex = / \{ .* " p a t h " .* " f n M a p " .* " s t a t e m e n t M a p " .* " b r a n c h M a p " .* \} / g
22
20
23
21
// Preprocessor creator function
24
- function createCoveragePreprocessor ( logger , helper , basePath , reporters , coverageReporter ) {
25
- var log = logger . create ( 'preprocessor.coverage' )
22
+ function createCoveragePreprocessor ( logger , basePath , reporters = [ ] , coverageReporter = { } ) {
23
+ const log = logger . create ( 'preprocessor.coverage' )
26
24
27
25
// Options
28
26
// -------
@@ -48,7 +46,7 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
48
46
return new Obj . Instrumenter ( opts )
49
47
}
50
48
}
51
- if ( ! _ . isFunction ( Obj ) ) {
49
+ if ( typeof Obj !== 'function' ) {
52
50
// Object doesn't have old instrumenter variable and isn't a
53
51
// constructor, so we can't use it to create an instrumenter
54
52
return null
@@ -61,54 +59,49 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
61
59
return Obj
62
60
}
63
61
64
- var instrumenterOverrides = { }
65
- var instrumenters = { istanbul : createInstrumenter }
66
- var includeAllSources = false
67
- var useJSExtensionForCoffeeScript = false
68
-
69
- if ( coverageReporter ) {
70
- instrumenterOverrides = coverageReporter . instrumenter
71
- _ . forEach ( coverageReporter . instrumenters , function ( instrumenter , literal ) {
72
- var creatorFunction = getCreatorFunction ( instrumenter )
73
- if ( creatorFunction ) {
74
- instrumenters [ literal ] = creatorFunction
75
- }
76
- } )
77
- includeAllSources = coverageReporter . includeAllSources === true
78
- useJSExtensionForCoffeeScript = coverageReporter . useJSExtensionForCoffeeScript === true
79
- }
62
+ const instrumenters = { istanbul : createInstrumenter }
63
+ const instrumenterOverrides = coverageReporter . instrumenter || { }
64
+ const { includeAllSources = false , useJSExtensionForCoffeeScript = false } = coverageReporter
65
+
66
+ Object . entries ( coverageReporter . instrumenters || { } ) . forEach ( ( [ literal , instrumenter ] ) => {
67
+ const creatorFunction = getCreatorFunction ( instrumenter )
68
+ if ( creatorFunction ) {
69
+ instrumenters [ literal ] = creatorFunction
70
+ }
71
+ } )
80
72
81
- var sourceMapStore = globalSourceMapStore . get ( basePath )
73
+ const sourceMapStore = globalSourceMapStore . get ( basePath )
82
74
83
- var instrumentersOptions = _ . reduce ( instrumenters , function getInstrumenterOptions ( memo , instrument , name ) {
84
- memo [ name ] = { }
75
+ const instrumentersOptions = Object . keys ( instrumenters ) . reduce ( ( memo , key ) => {
76
+ memo [ key ] = { }
85
77
86
- if ( coverageReporter && coverageReporter . instrumenterOptions ) {
87
- memo [ name ] = coverageReporter . instrumenterOptions [ name ]
78
+ if ( coverageReporter . instrumenterOptions ) {
79
+ memo [ key ] = coverageReporter . instrumenterOptions [ key ]
88
80
}
89
81
90
82
return memo
91
83
} , { } )
92
84
93
85
// if coverage reporter is not used, do not preprocess the files
94
- if ( ! _ . includes ( reporters , 'coverage' ) ) {
86
+ if ( ! reporters . includes ( 'coverage' ) ) {
95
87
return function ( content , _ , done ) {
96
88
done ( content )
97
89
}
98
90
}
99
91
100
92
// check instrumenter override requests
101
93
function checkInstrumenters ( ) {
102
- return _ . reduce ( instrumenterOverrides , function ( acc , literal , pattern ) {
103
- if ( ! _ . includes ( _ . keys ( instrumenters ) , String ( literal ) ) ) {
94
+ const keys = Object . keys ( instrumenters )
95
+ return Object . values ( instrumenterOverrides ) . some ( literal => {
96
+ const notIncluded = ! keys . includes ( String ( literal ) )
97
+ if ( notIncluded ) {
104
98
log . error ( 'Unknown instrumenter: %s' , literal )
105
- return false
106
99
}
107
- return acc
108
- } , true )
100
+ return notIncluded
101
+ } )
109
102
}
110
103
111
- if ( ! checkInstrumenters ( ) ) {
104
+ if ( checkInstrumenters ( ) ) {
112
105
return function ( content , _ , done ) {
113
106
return done ( 1 )
114
107
}
@@ -117,20 +110,19 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
117
110
return function ( content , file , done ) {
118
111
log . debug ( 'Processing "%s".' , file . originalPath )
119
112
120
- var jsPath = path . resolve ( file . originalPath )
121
- // default instrumenters
122
- var instrumenterLiteral = 'istanbul'
123
-
124
- _ . forEach ( instrumenterOverrides , function ( literal , pattern ) {
113
+ const jsPath = path . resolve ( file . originalPath )
114
+ // 'istanbul' is default instrumenters
115
+ const instrumenterLiteral = Object . keys ( instrumenterOverrides ) . reduce ( ( res , pattern ) => {
125
116
if ( minimatch ( file . originalPath , pattern , { dot : true } ) ) {
126
- instrumenterLiteral = String ( literal )
117
+ return instrumenterOverrides [ pattern ]
127
118
}
128
- } )
119
+ return res
120
+ } , 'istanbul' )
129
121
130
- var instrumenterCreator = instrumenters [ instrumenterLiteral ]
131
- var constructOptions = instrumentersOptions [ instrumenterLiteral ] || { }
132
- var options = Object . assign ( { } , constructOptions )
133
- var codeGenerationOptions = null
122
+ const instrumenterCreator = instrumenters [ instrumenterLiteral ]
123
+ const constructOptions = instrumentersOptions [ instrumenterLiteral ] || { }
124
+ let options = Object . assign ( { } , constructOptions )
125
+ let codeGenerationOptions = null
134
126
options . autoWrap = options . autoWrap || ! options . noAutoWrap
135
127
136
128
if ( file . sourceMap ) {
@@ -148,15 +140,15 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
148
140
149
141
options = Object . assign ( { } , options , { codeGenerationOptions : codeGenerationOptions } )
150
142
151
- var instrumenter = instrumenterCreator ( options )
143
+ const instrumenter = instrumenterCreator ( options )
152
144
instrumenter . instrument ( content , jsPath , function ( err , instrumentedCode ) {
153
145
if ( err ) {
154
146
log . error ( '%s\n at %s' , err . message , file . originalPath )
155
147
done ( err . message )
156
148
} else {
157
149
if ( file . sourceMap && instrumenter . lastSourceMap ( ) ) {
158
150
log . debug ( 'Adding source map to instrumented file for "%s".' , file . originalPath )
159
- var generator = SourceMapGenerator . fromSourceMap ( new SourceMapConsumer ( instrumenter . lastSourceMap ( ) . toString ( ) ) )
151
+ const generator = SourceMapGenerator . fromSourceMap ( new SourceMapConsumer ( instrumenter . lastSourceMap ( ) . toString ( ) ) )
160
152
generator . applySourceMap ( new SourceMapConsumer ( file . sourceMap ) )
161
153
file . sourceMap = JSON . parse ( generator . toString ( ) )
162
154
instrumentedCode += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
@@ -167,7 +159,7 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
167
159
sourceMapStore . registerMap ( jsPath , file . sourceMap )
168
160
169
161
if ( includeAllSources ) {
170
- var coverageObj
162
+ let coverageObj
171
163
// Check if the file coverage object is exposed from the instrumenter directly
172
164
if ( instrumenter . lastFileCoverage ) {
173
165
coverageObj = instrumenter . lastFileCoverage ( )
@@ -177,7 +169,7 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
177
169
178
170
// reset stateful regex
179
171
coverageObjRegex . lastIndex = 0
180
- var coverageObjMatch = coverageObjRegex . exec ( instrumentedCode )
172
+ const coverageObjMatch = coverageObjRegex . exec ( instrumentedCode )
181
173
if ( coverageObjMatch !== null ) {
182
174
coverageObj = JSON . parse ( coverageObjMatch [ 0 ] )
183
175
globalCoverageMap . add ( coverageObj )
@@ -198,7 +190,6 @@ function createCoveragePreprocessor (logger, helper, basePath, reporters, covera
198
190
199
191
createCoveragePreprocessor . $inject = [
200
192
'logger' ,
201
- 'helper' ,
202
193
'config.basePath' ,
203
194
'config.reporters' ,
204
195
'config.coverageReporter'
0 commit comments