Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make --all work for transpiled code #1047

Merged
merged 2 commits into from Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/source-maps.js
Expand Up @@ -4,12 +4,12 @@ const libSourceMaps = require('istanbul-lib-source-maps')
const fs = require('fs')
const path = require('path')

// TODO: write some unit tests for this class.
let sourceMapCache = libSourceMaps.createSourceMapStore()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about how this can have any effective change. The SourceMaps object is only created by the NYC object as nyc.sourceMaps. Since each process only gets one object, the singleton doesn't actually get reused by multiple SourceMaps objects. My vote is that we don't use singletons if it can be avoided.

Above aside if this is kept as a singleton variable I think it should be const, not let.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a const.

The issue is that we new NYC() during instrumentation, and then also new NYC() in report and check steps. I agree that singletons are often a bad pattern, but in the case of a cache I believe it's the correct behavior (it should be a cache of source-maps that exist for the processes lifecycle).

function SourceMaps (opts) {
this.cache = opts.cache
this.cacheDirectory = opts.cacheDirectory
this.sourceMapCache = libSourceMaps.createSourceMapStore()
this.loadedMaps = {}
this._sourceMapCache = sourceMapCache
}

SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
Expand All @@ -19,22 +19,22 @@ SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
var mapPath = path.join(this.cacheDirectory, hash + '.map')
fs.writeFileSync(mapPath, sourceMap.toJSON())
} else {
this.sourceMapCache.registerMap(filename, sourceMap.sourcemap)
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
}
}
return sourceMap
}

SourceMaps.prototype.remapCoverage = function (obj) {
var transformed = this.sourceMapCache.transformCoverage(
var transformed = this._sourceMapCache.transformCoverage(
libCoverage.createCoverageMap(obj)
)
return transformed.map.data
}

SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
var _this = this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are converting to an arrow function would you mind dropping _this and updating the whole function to use this directly?

Object.keys(report).forEach(function (absFile) {
Object.keys(report).forEach((absFile) => {
var fileReport = report[absFile]
if (fileReport && fileReport.contentHash) {
var hash = fileReport.contentHash
Expand All @@ -48,7 +48,7 @@ SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
}
}
if (_this.loadedMaps[hash]) {
_this.sourceMapCache.registerMap(absFile, _this.loadedMaps[hash])
this._sourceMapCache.registerMap(absFile, _this.loadedMaps[hash])
}
}
})
Expand Down
20 changes: 20 additions & 0 deletions test/source-maps.js
@@ -0,0 +1,20 @@
/* 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)
})
})