From bb04c561bffe9802b7d2e7e91216aa1d9230490a Mon Sep 17 00:00:00 2001 From: Clement Yan Date: Fri, 4 Jun 2021 05:22:19 +0800 Subject: [PATCH] fix: address file URL path regression on Windows (#146) BREAKING CHANGE: minimum Node version now 10.12. --- lib/v8-to-istanbul.js | 8 ++++++-- package.json | 2 +- test/v8-to-istanbul.js | 21 +++++++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/v8-to-istanbul.js b/lib/v8-to-istanbul.js index f4bd25d0..46fefb89 100644 --- a/lib/v8-to-istanbul.js +++ b/lib/v8-to-istanbul.js @@ -1,6 +1,7 @@ const assert = require('assert') const convertSourceMap = require('convert-source-map') const { dirname, isAbsolute, join, resolve } = require('path') +const { fileURLToPath } = require('url') const CovBranch = require('./branch') const CovFunction = require('./function') const CovSource = require('./source') @@ -83,7 +84,10 @@ module.exports = class V8ToIstanbul { } _resolveSource (rawSourceMap, sourcePath) { - sourcePath = sourcePath.replace(/(^file:\/\/)|(^webpack:\/\/)/, '') + if (sourcePath.startsWith('file://')) { + return fileURLToPath(sourcePath) + } + sourcePath = sourcePath.replace(/^webpack:\/\//, '') const sourceRoot = rawSourceMap.sourcemap.sourceRoot ? rawSourceMap.sourcemap.sourceRoot.replace('file://', '') : '' const candidatePath = join(sourceRoot, sourcePath) @@ -282,5 +286,5 @@ module.exports = class V8ToIstanbul { } function parsePath (scriptPath) { - return scriptPath.replace('file://', '') + return scriptPath.startsWith('file://') ? fileURLToPath(scriptPath) : scriptPath } diff --git a/package.json b/package.json index ae5caf0d..728697c1 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "tap": "^14.10.8" }, "engines": { - "node": ">=10.10.0" + "node": ">=10.12.0" }, "files": [ "lib/*.js", diff --git a/test/v8-to-istanbul.js b/test/v8-to-istanbul.js index 6765966d..78f95a26 100644 --- a/test/v8-to-istanbul.js +++ b/test/v8-to-istanbul.js @@ -1,6 +1,7 @@ /* global describe, it, beforeEach, afterEach */ const { readdirSync, lstatSync, writeFileSync, readFileSync } = require('fs') const path = require('path') +const { pathToFileURL } = require('url') const runFixture = require('./utils/run-fixture') const V8ToIstanbul = require('../lib/v8-to-istanbul') const crypto = require('crypto') @@ -25,7 +26,7 @@ describe('V8ToIstanbul', async () => { it('handles ESM style paths', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/functions.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/functions.js')).href, 0 ) await v8ToIstanbul.load() @@ -94,7 +95,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} it('should clamp line source column >= 0', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/needs-compile.compiled.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/needs-compile.compiled.js')).href, 0 ) @@ -124,7 +125,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} it('should exclude files when passing excludePath', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/sourcemap-multisource.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/sourcemap-multisource.js')).href, 0, undefined, path => path.indexOf('bootstrap') > -1 @@ -137,7 +138,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} endOffset: 1 }] }]) - Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/src/index.ts', '/src/utils.ts']) + Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/src/index.ts', '/src/utils.ts'].map(path.normalize)) }) }) @@ -152,7 +153,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} }) it('should handle empty sources in a sourcemap', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/empty.compiled.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/empty.compiled.js')).href, 0 ) await v8ToIstanbul.load() @@ -160,22 +161,22 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} it('should handle relative sourceRoots correctly', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/relative-source-root.compiled.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/relative-source-root.compiled.js')).href, 0 ) await v8ToIstanbul.load() - assert(v8ToIstanbul.path.includes('v8-to-istanbul/test/fixtures/one-up/relative-source-root.js')) + assert(v8ToIstanbul.path.includes(path.normalize('v8-to-istanbul/test/fixtures/one-up/relative-source-root.js'))) }) - it('should handles source maps with moultiple sources', async () => { + it('should handles source maps with multiple sources', async () => { const v8ToIstanbul = new V8ToIstanbul( - `file://${require.resolve('./fixtures/scripts/sourcemap-multisource.js')}`, + pathToFileURL(require.resolve('./fixtures/scripts/sourcemap-multisource.js')).href, 0 ) await v8ToIstanbul.load() v8ToIstanbul.covSources.length.should.equal(3) - Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/webpack/bootstrap', '/src/index.ts', '/src/utils.ts']) + Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/webpack/bootstrap', '/src/index.ts', '/src/utils.ts'].map(path.normalize)) }) })