From 628af48e2f7ab279c52f4355c0ccb92ac16b2c1a Mon Sep 17 00:00:00 2001 From: Joseph Mordetsky Date: Fri, 27 Mar 2020 14:41:21 -0400 Subject: [PATCH] fix: handle sourcemap `sources` emtpy edge case (#94) --- lib/v8-to-istanbul.js | 2 +- test/fixtures/scripts/empty.compiled.js | 3 +++ test/fixtures/scripts/empty.compiled.js.map | 1 + test/fixtures/scripts/empty.js | 0 test/v8-to-istanbul.js | 20 ++++++++++++++++++-- 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/scripts/empty.compiled.js create mode 100644 test/fixtures/scripts/empty.compiled.js.map create mode 100644 test/fixtures/scripts/empty.js diff --git a/lib/v8-to-istanbul.js b/lib/v8-to-istanbul.js index 283f3d36..26f1e52c 100644 --- a/lib/v8-to-istanbul.js +++ b/lib/v8-to-istanbul.js @@ -73,7 +73,7 @@ module.exports = class V8ToIstanbul { _rewritePath (rawSourceMap) { const sourceRoot = rawSourceMap.sourcemap.sourceRoot ? rawSourceMap.sourcemap.sourceRoot.replace('file://', '') : '' - const sourcePath = rawSourceMap.sourcemap.sources[0].replace('file://', '') + const sourcePath = rawSourceMap.sourcemap.sources.length >= 1 ? rawSourceMap.sourcemap.sources[0].replace('file://', '') : rawSourceMap.sourcemap.file const candidatePath = join(sourceRoot, sourcePath) if (isAbsolute(candidatePath)) { diff --git a/test/fixtures/scripts/empty.compiled.js b/test/fixtures/scripts/empty.compiled.js new file mode 100644 index 00000000..f33e6e13 --- /dev/null +++ b/test/fixtures/scripts/empty.compiled.js @@ -0,0 +1,3 @@ + + +//# sourceMappingURL=empty.compiled.js.map \ No newline at end of file diff --git a/test/fixtures/scripts/empty.compiled.js.map b/test/fixtures/scripts/empty.compiled.js.map new file mode 100644 index 00000000..196ae77a --- /dev/null +++ b/test/fixtures/scripts/empty.compiled.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"names":[],"mappings":"","file":"empty.compiled.js"} \ No newline at end of file diff --git a/test/fixtures/scripts/empty.js b/test/fixtures/scripts/empty.js new file mode 100644 index 00000000..e69de29b diff --git a/test/v8-to-istanbul.js b/test/v8-to-istanbul.js index 12eed5b9..10004967 100644 --- a/test/v8-to-istanbul.js +++ b/test/v8-to-istanbul.js @@ -1,5 +1,4 @@ -/* global describe, it */ - +/* global describe, it, beforeEach, afterEach */ const { readdirSync, lstatSync, writeFileSync, readFileSync } = require('fs') const path = require('path') const runFixture = require('./utils/run-fixture') @@ -90,6 +89,23 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} v8ToIstanbul.sourceTranspiled.should.not.be.undefined() }) }) + describe('source map format edge cases', () => { + let consoleWarn + beforeEach(() => { + consoleWarn = console.warn + console.warn = () => { throw new Error('Test should not invoke console.warn') } + }) + afterEach(() => { + console.warn = consoleWarn + }) + it('should handle empty sources in a sourcemap', async () => { + const v8ToIstanbul = new V8ToIstanbul( + `file://${require.resolve('./fixtures/scripts/empty.compiled.js')}`, + 0 + ) + await v8ToIstanbul.load() + }) + }) // execute JavaScript files in fixtures directory; these // files contain the raw v8 output along with a set of