Skip to content

Commit

Permalink
chore: A few code cleanups (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFinlay authored and coreyfarrell committed Mar 18, 2019
1 parent 2867538 commit 91e02c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
39 changes: 12 additions & 27 deletions index.js
Expand Up @@ -111,17 +111,10 @@ NYC.prototype._disableCachingTransform = function () {
}

NYC.prototype._loadAdditionalModules = function () {
var _this = this
this.require.forEach(function (r) {
// first attempt to require the module relative to
// the directory being instrumented.
var p = resolveFrom.silent(_this.cwd, r)
if (p) {
require(p)
return
}
// now try other locations, .e.g, the nyc node_modules folder.
require(r)
this.require.forEach(requireModule => {
// Attempt to require the module relative to the directory being instrumented.
// Then try other locations, e.g. the nyc node_modules folder.
require(resolveFrom.silent(this.cwd, requireModule) || requireModule)
})
}

Expand All @@ -130,7 +123,7 @@ NYC.prototype.instrumenter = function () {
}

NYC.prototype._createInstrumenter = function () {
return this._instrumenterLib(this.cwd, {
return this._instrumenterLib({
ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a),
produceSourceMap: this.config.produceSourceMap,
compact: this.config.compact,
Expand All @@ -141,15 +134,9 @@ NYC.prototype._createInstrumenter = function () {
}

NYC.prototype.addFile = function (filename) {
var relFile = path.relative(this.cwd, filename)
var source = this._readTranspiledSource(path.resolve(this.cwd, filename))
var instrumentedSource = this._maybeInstrumentSource(source, filename, relFile)

return {
instrument: !!instrumentedSource,
relFile: relFile,
content: instrumentedSource || source
}
const relFile = path.relative(this.cwd, filename)
const source = this._readTranspiledSource(path.resolve(this.cwd, filename))
this._maybeInstrumentSource(source, filename, relFile)
}

NYC.prototype._readTranspiledSource = function (filePath) {
Expand Down Expand Up @@ -286,8 +273,8 @@ NYC.prototype._handleJs = function (code, options) {
}

NYC.prototype._addHook = function (type) {
var handleJs = this._handleJs.bind(this)
var dummyMatcher = function () { return true } // we do all processing in transformer
const handleJs = this._handleJs.bind(this)
const dummyMatcher = () => true // we do all processing in transformer
libHook['hook' + type](dummyMatcher, handleJs, { extensions: this.extensions })
}

Expand Down Expand Up @@ -328,12 +315,10 @@ NYC.prototype.reset = function () {
}

NYC.prototype._wrapExit = function () {
var _this = this

// we always want to write coverage
// regardless of how the process exits.
onExit(function () {
_this.writeCoverageFile()
onExit(() => {
this.writeCoverageFile()
}, { alwaysLast: true })
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/instrument.js
Expand Up @@ -84,7 +84,7 @@ exports.handler = function (argv) {
if (relPath !== '' && !relPath.startsWith('..')) {
rimraf.sync(argv.output)
} else {
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}'`)
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}' or containing directory.`)
process.exit(1)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/instrumenters/istanbul.js
Expand Up @@ -4,7 +4,7 @@ const { createInstrumenter } = require('istanbul-lib-instrument')
const convertSourceMap = require('convert-source-map')
const mergeSourceMap = require('merge-source-map')

function InstrumenterIstanbul (cwd, options) {
function InstrumenterIstanbul (options) {
const plugins = options.parserPlugins
const configPlugins = plugins ? { plugins } : {}

Expand Down

0 comments on commit 91e02c6

Please sign in to comment.