From 91e02c66c41151cf5375b620085dec03e24ec589 Mon Sep 17 00:00:00 2001 From: AndrewFinlay Date: Tue, 19 Mar 2019 10:35:01 +1100 Subject: [PATCH] chore: A few code cleanups (#1033) --- index.js | 39 +++++++++++------------------------ lib/commands/instrument.js | 2 +- lib/instrumenters/istanbul.js | 2 +- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 452677e3d..43e20884b 100755 --- a/index.js +++ b/index.js @@ -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) }) } @@ -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, @@ -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) { @@ -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 }) } @@ -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 }) } diff --git a/lib/commands/instrument.js b/lib/commands/instrument.js index 622c5325b..43267a111 100644 --- a/lib/commands/instrument.js +++ b/lib/commands/instrument.js @@ -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) } } diff --git a/lib/instrumenters/istanbul.js b/lib/instrumenters/istanbul.js index 7e8dd8f91..4340f6081 100644 --- a/lib/instrumenters/istanbul.js +++ b/lib/instrumenters/istanbul.js @@ -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 } : {}