-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathpreprocessor.js
201 lines (171 loc) · 6.4 KB
/
preprocessor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Coverage Preprocessor
// =====================
//
// Depends on the the reporter to generate an actual report
// Dependencies
// ------------
const { createInstrumenter } = require('istanbul-lib-instrument')
const minimatch = require('minimatch')
const path = require('path')
const globalSourceMapStore = require('./source-map-store')
const globalCoverageMap = require('./coverage-map')
// Regexes
// -------
const coverageObjRegex = /\{.*"path".*"fnMap".*"statementMap".*"branchMap".*\}/g
// Preprocessor creator function
function createCoveragePreprocessor (logger, basePath, reporters = [], coverageReporter = {}) {
const log = logger.create('preprocessor.coverage')
// Options
// -------
function isConstructor (Func) {
try {
// eslint-disable-next-line
new Func()
} catch (err) {
// error message should be of the form: "TypeError: func is not a constructor"
// test for this type of message to ensure we failed due to the function not being
// constructable
if (/TypeError.*constructor/.test(err.message)) {
return false
}
}
return true
}
function getCreatorFunction (Obj) {
if (Obj.Instrumenter) {
return function (opts) {
return new Obj.Instrumenter(opts)
}
}
if (typeof Obj !== 'function') {
// Object doesn't have old instrumenter variable and isn't a
// constructor, so we can't use it to create an instrumenter
return null
}
if (isConstructor(Obj)) {
return function (opts) {
return new Obj(opts)
}
}
return Obj
}
const instrumenters = { istanbul: createInstrumenter }
const instrumenterOverrides = coverageReporter.instrumenter || {}
const { includeAllSources = false, useJSExtensionForCoffeeScript = false } = coverageReporter
Object.entries(coverageReporter.instrumenters || {}).forEach(([literal, instrumenter]) => {
const creatorFunction = getCreatorFunction(instrumenter)
if (creatorFunction) {
instrumenters[literal] = creatorFunction
}
})
const sourceMapStore = globalSourceMapStore.get(basePath)
const instrumentersOptions = Object.keys(instrumenters).reduce((memo, key) => {
memo[key] = {}
if (coverageReporter.instrumenterOptions) {
memo[key] = coverageReporter.instrumenterOptions[key]
}
return memo
}, {})
// if coverage reporter is not used, do not preprocess the files
if (!reporters.includes('coverage')) {
log.info('coverage not included in reporters %s', reporters)
return function (content, _, done) {
done(content)
}
}
log.debug('coverage included in reporters %s', reporters)
// check instrumenter override requests
function checkInstrumenters () {
const keys = Object.keys(instrumenters)
return Object.values(instrumenterOverrides).some(literal => {
const notIncluded = !keys.includes(String(literal))
if (notIncluded) {
log.error('Unknown instrumenter: %s', literal)
}
return notIncluded
})
}
if (checkInstrumenters()) {
return function (content, _, done) {
return done(1)
}
}
return function (content, file, done) {
log.debug('Processing "%s".', file.originalPath)
const jsPath = path.resolve(file.originalPath)
// 'istanbul' is default instrumenters
const instrumenterLiteral = Object.keys(instrumenterOverrides).reduce((res, pattern) => {
if (minimatch(file.originalPath, pattern, { dot: true })) {
return instrumenterOverrides[pattern]
}
return res
}, 'istanbul')
const instrumenterCreator = instrumenters[instrumenterLiteral]
const constructOptions = instrumentersOptions[instrumenterLiteral] || {}
let options = Object.assign({}, constructOptions)
let codeGenerationOptions = null
options.autoWrap = options.autoWrap || !options.noAutoWrap
if (file.sourceMap) {
log.debug('Enabling source map generation for "%s".', file.originalPath)
codeGenerationOptions = Object.assign({}, {
format: {
compact: !constructOptions.noCompact
},
sourceMap: file.sourceMap.file,
sourceMapWithCode: true,
file: file.path
}, constructOptions.codeGenerationOptions || {})
options.produceSourceMap = true
}
options = Object.assign({}, options, { codeGenerationOptions: codeGenerationOptions })
const instrumenter = instrumenterCreator(options)
instrumenter.instrument(content, jsPath, function (err, instrumentedCode) {
if (err) {
log.error('%s\n at %s', err.message, file.originalPath)
done(err.message)
} else {
// Register the incoming sourceMap for transformation during reporting (if it exists)
if (file.sourceMap) {
sourceMapStore.registerMap(jsPath, file.sourceMap)
}
// Add merged source map (if it merged correctly)
const lastSourceMap = instrumenter.lastSourceMap()
if (lastSourceMap) {
log.debug('Adding source map to instrumented file for "%s".', file.originalPath)
file.sourceMap = lastSourceMap
instrumentedCode += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
instrumentedCode += Buffer.from(JSON.stringify(lastSourceMap)).toString('base64') + '\n'
}
if (includeAllSources) {
let coverageObj
// Check if the file coverage object is exposed from the instrumenter directly
if (instrumenter.lastFileCoverage) {
coverageObj = instrumenter.lastFileCoverage()
globalCoverageMap.add(coverageObj)
} else {
// Attempt to match and parse coverage object from instrumented code
// reset stateful regex
coverageObjRegex.lastIndex = 0
const coverageObjMatch = coverageObjRegex.exec(instrumentedCode)
if (coverageObjMatch !== null) {
coverageObj = JSON.parse(coverageObjMatch[0])
globalCoverageMap.add(coverageObj)
}
}
}
// RequireJS expects JavaScript files to end with `.js`
if (useJSExtensionForCoffeeScript && instrumenterLiteral === 'ibrik') {
file.path = file.path.replace(/\.coffee$/, '.js')
}
done(instrumentedCode)
}
}, file.sourceMap)
}
}
createCoveragePreprocessor.$inject = [
'logger',
'config.basePath',
'config.reporters',
'config.coverageReporter'
]
module.exports = createCoveragePreprocessor