Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/launch-editor/parse-file.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const path = require('path')
const { fileURLToPath } = require('url')

const positionRE = /:(\d+)(:(\d+))?$/
const windowsDriveRE = /^\/[a-z](?::|%3a)/i

module.exports = function parseFile(file) {
// support `file://` protocol
if (file.startsWith('file://')) {
file = require('url').fileURLToPath(file)
const url = new URL(file)
// On Windows, `fileURLToPath` rejects file URLs without a drive letter
// (e.g. `file:///src/main.js`) because they are not valid Windows paths.
// Parse them as POSIX file URLs and let the path resolution below map
// them onto the current drive.
file =
process.platform === 'win32' &&
url.hostname === '' &&
!windowsDriveRE.test(url.pathname)
? fileURLToPath(url, { windows: false })
: fileURLToPath(url)
}

const unresolvedFileName = file.replace(positionRE, '')
Expand Down
8 changes: 8 additions & 0 deletions packages/launch-editor/parse-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('launchEditor path resolution', () => {
})
})

test('parses file URLs without a drive letter', () => {
assert.deepEqual(parseFile('file:///foo/bar.js:10:5'), {
fileName: path.resolve('/foo/bar.js'),
lineNumber: '10',
columnNumber: '5',
})
})

test('resolves a parent-directory component created by a position suffix', () => {
const project = path.resolve('project')
const file = `${project}${path.sep}attacker-controlled${path.sep}..:1:1`
Expand Down