Debian Codegraph @

nyc/lib/instrumenters/istanbul.js

raw
1 'use strict'
2
3 function InstrumenterIstanbul (options) {
4 const { createInstrumenter } = require('istanbul-lib-instrument')
5 const convertSourceMap = require('convert-source-map')
6
7 const instrumenter = createInstrumenter({
8 autoWrap: true,
9 coverageVariable: '__coverage__',
10 embedSource: true,
11 compact: options.compact,
12 preserveComments: options.preserveComments,
13 produceSourceMap: options.produceSourceMap,
14 ignoreClassMethods: options.ignoreClassMethods,
15 esModules: options.esModules,
16 parserPlugins: options.parserPlugins
17 })
18
19 return {
20 instrumentSync (code, filename, { sourceMap, registerMap }) {
21 var instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
22 if (instrumented !== code) {
23 registerMap()
24 }
25
26 // the instrumenter can optionally produce source maps,
27 // this is useful for features like remapping stack-traces.
28 if (options.produceSourceMap) {
29 var lastSourceMap = instrumenter.lastSourceMap()
30 /* istanbul ignore else */
31 if (lastSourceMap) {
32 instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
33 }
34 }
35
36 return instrumented
37 },
38 lastFileCoverage () {
39 return instrumenter.lastFileCoverage()
40 }
41 }
42 }
43
44 module.exports = InstrumenterIstanbul