| 1 |
import path from 'path' |
| 2 |
import { realpathSync } from 'fs' |
| 3 |
import { execFileSync } from 'child_process' |
| 4 |
import { declare } from '@babel/helper-plugin-utils' |
| 5 |
import { programVisitor } from 'istanbul-lib-instrument' |
| 6 |
import TestExclude from 'test-exclude' |
| 7 |
import schema from '@istanbuljs/schema' |
| 8 |
|
| 9 |
function getRealpath (n) { |
| 10 |
try { |
| 11 |
return realpathSync(n) || n |
| 12 |
} catch (e) { |
| 13 |
|
| 14 |
return n |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
const memoize = new Map() |
| 19 |
|
| 20 |
const memosep = path.sep === '/' ? ':' : ';' |
| 21 |
|
| 22 |
function loadNycConfig (cwd, opts) { |
| 23 |
let memokey = cwd |
| 24 |
const args = [ |
| 25 |
path.resolve(__dirname, 'load-nyc-config-sync.js'), |
| 26 |
cwd |
| 27 |
] |
| 28 |
|
| 29 |
if ('nycrcPath' in opts) { |
| 30 |
args.push(opts.nycrcPath) |
| 31 |
|
| 32 |
memokey += memosep + opts.nycrcPath |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if (memoize.has(memokey)) { |
| 37 |
return memoize.get(memokey) |
| 38 |
} |
| 39 |
|
| 40 |
const result = JSON.parse(execFileSync(process.execPath, args)) |
| 41 |
const error = result['load-nyc-config-sync-error'] |
| 42 |
if (error) { |
| 43 |
throw new Error(error) |
| 44 |
} |
| 45 |
|
| 46 |
const config = { |
| 47 |
...schema.defaults.babelPluginIstanbul, |
| 48 |
cwd, |
| 49 |
...result |
| 50 |
} |
| 51 |
memoize.set(memokey, config) |
| 52 |
return config |
| 53 |
} |
| 54 |
|
| 55 |
function findConfig (opts) { |
| 56 |
const cwd = getRealpath(opts.cwd || process.env.NYC_CWD || process.cwd()) |
| 57 |
const keys = Object.keys(opts) |
| 58 |
const ignored = Object.keys(opts).filter(s => s === 'nycrcPath' || s === 'cwd') |
| 59 |
if (keys.length > ignored.length) { |
| 60 |
|
| 61 |
|
| 62 |
return { |
| 63 |
...schema.defaults.babelPluginIstanbul, |
| 64 |
cwd, |
| 65 |
...opts |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (ignored.length === 0 && process.env.NYC_CONFIG) { |
| 70 |
|
| 71 |
return JSON.parse(process.env.NYC_CONFIG) |
| 72 |
} |
| 73 |
|
| 74 |
return loadNycConfig(cwd, opts) |
| 75 |
} |
| 76 |
|
| 77 |
function makeShouldSkip () { |
| 78 |
let exclude |
| 79 |
|
| 80 |
return function shouldSkip (file, nycConfig) { |
| 81 |
if (!exclude || (exclude.cwd !== nycConfig.cwd)) { |
| 82 |
exclude = new TestExclude({ |
| 83 |
cwd: nycConfig.cwd, |
| 84 |
include: nycConfig.include, |
| 85 |
exclude: nycConfig.exclude, |
| 86 |
extension: nycConfig.extension, |
| 87 |
|
| 88 |
excludeNodeModules: nycConfig.excludeNodeModules !== false |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
return !exclude.shouldInstrument(file) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
export default declare(api => { |
| 97 |
api.assertVersion(7) |
| 98 |
|
| 99 |
const shouldSkip = makeShouldSkip() |
| 100 |
|
| 101 |
const t = api.types |
| 102 |
return { |
| 103 |
visitor: { |
| 104 |
Program: { |
| 105 |
enter (path) { |
| 106 |
this.__dv__ = null |
| 107 |
this.nycConfig = findConfig(this.opts) |
| 108 |
const realPath = getRealpath(this.file.opts.filename) |
| 109 |
if (shouldSkip(realPath, this.nycConfig)) { |
| 110 |
return |
| 111 |
} |
| 112 |
let { inputSourceMap } = this.opts |
| 113 |
if (this.opts.useInlineSourceMaps !== false) { |
| 114 |
if (!inputSourceMap && this.file.inputMap) { |
| 115 |
inputSourceMap = this.file.inputMap.sourcemap |
| 116 |
} |
| 117 |
} |
| 118 |
const visitorOptions = {} |
| 119 |
Object.entries(schema.defaults.instrumentVisitor).forEach(([name, defaultValue]) => { |
| 120 |
if (name in this.nycConfig) { |
| 121 |
visitorOptions[name] = this.nycConfig[name] |
| 122 |
} else { |
| 123 |
visitorOptions[name] = schema.defaults.instrumentVisitor[name] |
| 124 |
} |
| 125 |
}) |
| 126 |
this.__dv__ = programVisitor(t, realPath, { |
| 127 |
...visitorOptions, |
| 128 |
inputSourceMap |
| 129 |
}) |
| 130 |
this.__dv__.enter(path) |
| 131 |
}, |
| 132 |
exit (path) { |
| 133 |
if (!this.__dv__) { |
| 134 |
return |
| 135 |
} |
| 136 |
const result = this.__dv__.exit(path) |
| 137 |
if (this.opts.onCover) { |
| 138 |
this.opts.onCover(getRealpath(this.file.opts.filename), result.fileCoverage) |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
}) |