From 16ad3aabd6144e2bf15fb4065e697bf40d1caaf4 Mon Sep 17 00:00:00 2001 From: Joseph Mordetsky Date: Wed, 6 May 2020 19:17:44 -0400 Subject: [PATCH] fix: handle relative sourceRoots in source map files (#100) Co-authored-by: jmordetsky@bloomberg.net --- lib/v8-to-istanbul.js | 2 +- test/fixtures/one-up/relative-source-root.js | 0 test/fixtures/scripts/relative-source-root.compiled.js | 3 +++ .../scripts/relative-source-root.compiled.js.map | 1 + test/source.js | 6 ++++++ test/v8-to-istanbul.js | 9 +++++++++ 6 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/one-up/relative-source-root.js create mode 100644 test/fixtures/scripts/relative-source-root.compiled.js create mode 100644 test/fixtures/scripts/relative-source-root.compiled.js.map diff --git a/lib/v8-to-istanbul.js b/lib/v8-to-istanbul.js index 26f1e52c..d1cd6f46 100644 --- a/lib/v8-to-istanbul.js +++ b/lib/v8-to-istanbul.js @@ -79,7 +79,7 @@ module.exports = class V8ToIstanbul { if (isAbsolute(candidatePath)) { this.path = candidatePath } else { - this.path = resolve(dirname(this.path), sourcePath) + this.path = resolve(dirname(this.path), candidatePath) } } diff --git a/test/fixtures/one-up/relative-source-root.js b/test/fixtures/one-up/relative-source-root.js new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/scripts/relative-source-root.compiled.js b/test/fixtures/scripts/relative-source-root.compiled.js new file mode 100644 index 00000000..1e81b450 --- /dev/null +++ b/test/fixtures/scripts/relative-source-root.compiled.js @@ -0,0 +1,3 @@ + + +//# sourceMappingURL=relative-source-root.compiled.js.map \ No newline at end of file diff --git a/test/fixtures/scripts/relative-source-root.compiled.js.map b/test/fixtures/scripts/relative-source-root.compiled.js.map new file mode 100644 index 00000000..5df3c8f8 --- /dev/null +++ b/test/fixtures/scripts/relative-source-root.compiled.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["one-up/relative-source-root.js"],"names":[],"mappings":"","file":"relative-source-root.compiled.js", "sourceRoot":"../" } \ No newline at end of file diff --git a/test/source.js b/test/source.js index e99cc1bc..512ed98e 100644 --- a/test/source.js +++ b/test/source.js @@ -32,6 +32,12 @@ describe('Source', () => { source.relativeToOffset(2, 50).should.equal(22) source.relativeToOffset(1, Infinity).should.equal(1) }) + + it('returns empty object for out of range params', () => { + const sourceRaw = '' + const source = new CovSource(sourceRaw, 0) + source.offsetToOriginalRelative(undefined, Infinity, Infinity).should.deepEqual({}) + }) }) describe('ignore', () => { diff --git a/test/v8-to-istanbul.js b/test/v8-to-istanbul.js index bf17f13a..100f47f7 100644 --- a/test/v8-to-istanbul.js +++ b/test/v8-to-istanbul.js @@ -136,6 +136,15 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} ) await v8ToIstanbul.load() }) + + it('should handle relative sourceRoots correctly', async () => { + const v8ToIstanbul = new V8ToIstanbul( + `file://${require.resolve('./fixtures/scripts/relative-source-root.compiled.js')}`, + 0 + ) + await v8ToIstanbul.load() + assert(v8ToIstanbul.path.includes('v8-to-istanbul/test/fixtures/one-up/relative-source-root.js')) + }) }) // execute JavaScript files in fixtures directory; these