all files / src/ index.js

94.44% Statements 17/18
87.5% Branches 7/8
100% Functions 3/3
94.44% Lines 17/18
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                                                                     
import {realpathSync} from 'fs'
import {dirname} from 'path'
import {programVisitor} from 'istanbul-lib-instrument'
const testExclude = require('test-exclude')
const findUp = require('find-up')
 
function getRealpath (n) {
  try {
    return realpathSync(n) || n
  } catch (e) {
    return n
  }
}
 
let exclude
function shouldSkip (file) {
  if (!exclude) {
    exclude = testExclude({
      configKey: 'nyc',
      configPath: dirname(findUp.sync('package.json'))
    })
  }
 
  return !exclude.shouldInstrument(file)
}
 
function makeVisitor ({types: t}) {
  return {
    visitor: {
      Program: {
        enter (path) {
          this.__dv__ = null
          const realPath = getRealpath(this.file.opts.filename)
          if (shouldSkip(realPath)) {
            return
          }
          this.__dv__ = programVisitor(t, realPath)
          this.__dv__.enter(path)
        },
        exit (path) {
          if (!this.__dv__) {
            return
          }
          this.__dv__.exit(path)
        }
      }
    }
  }
}
 
export default makeVisitor