From 3d9eaa4263e3db341092986e76fc5b972f7d87e8 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Wed, 24 Apr 2019 14:12:31 -0400 Subject: [PATCH] fix: Purge source-map cache before reporting if cache is disabled. (#1080) --- bin/nyc.js | 1 + index.js | 6 ++++++ lib/source-maps.js | 8 ++++++-- test/source-maps.js | 20 -------------------- 4 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 test/source-maps.js diff --git a/bin/nyc.js b/bin/nyc.js index 7c188e40a..97ef50b67 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -67,6 +67,7 @@ if ([ nyc.writeProcessIndex() + nyc.maybePurgeSourceMapCache() if (argv.checkCoverage) { nyc.checkCoverage({ lines: argv.lines, diff --git a/index.js b/index.js index 8c9d8e9a8..acf66ff19 100755 --- a/index.js +++ b/index.js @@ -236,6 +236,12 @@ class NYC { return this._transform(code, filename) } + maybePurgeSourceMapCache () { + if (!this.cache) { + this.sourceMaps.purgeCache() + } + } + _transformFactory (cacheDir) { const instrumenter = this.instrumenter() let instrumented diff --git a/lib/source-maps.js b/lib/source-maps.js index c4840fc01..b36285d75 100644 --- a/lib/source-maps.js +++ b/lib/source-maps.js @@ -4,12 +4,16 @@ const libSourceMaps = require('istanbul-lib-source-maps') const fs = require('fs') const path = require('path') -const sourceMapCache = libSourceMaps.createSourceMapStore() function SourceMaps (opts) { this.cache = opts.cache this.cacheDirectory = opts.cacheDirectory this.loadedMaps = {} - this._sourceMapCache = sourceMapCache + this._sourceMapCache = libSourceMaps.createSourceMapStore() +} + +SourceMaps.prototype.purgeCache = function () { + this._sourceMapCache = libSourceMaps.createSourceMapStore() + this.loadedMaps = {} } SourceMaps.prototype.extractAndRegister = function (code, filename, hash) { diff --git a/test/source-maps.js b/test/source-maps.js deleted file mode 100644 index c95334832..000000000 --- a/test/source-maps.js +++ /dev/null @@ -1,20 +0,0 @@ -/* global describe, it */ - -require('chai').should() -require('tap').mochaGlobals() - -const { readFileSync } = require('fs') -const SourceMaps = require('../self-coverage/lib/source-maps') - -describe('source-maps', function () { - it('caches source maps globally', function () { - const sm = new SourceMaps({}) - // load a source map into cache. - const sourceFile = require.resolve('./fixtures/source-maps/instrumented/s1.min.js') - sm.extractAndRegister(readFileSync(sourceFile, 'utf8'), sourceFile, 'abc123') - // create a new SourceMaps instance. - const sm2 = new SourceMaps({}) - // the two instances of SourceMaps should share a cache. - sm._sourceMapCache.should.deep.equal(sm2._sourceMapCache) - }) -})