Skip to content

Commit 3586a7f

Browse files
authored
[DevTools] Allow file:/// urls to be opened in editor (facebook#33965)
If a `file:///` path is specified as the url of a file, like after source mapping into an ESM file, then we should be able to open it in a code editor.
1 parent f6fb1a0 commit 3586a7f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

packages/react-devtools-shared/src/devtools/views/Components/OpenInEditorButton.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow
78
*/
89

910
import * as React from 'react';
@@ -26,10 +27,14 @@ function checkConditions(
2627
try {
2728
const url = new URL(editorURL);
2829

29-
let [, sourceURL, ,] = source;
30+
const [, sourceURL, line] = source;
31+
let filePath;
3032

3133
// Check if sourceURL is a correct URL, which has a protocol specified
32-
if (sourceURL.includes('://')) {
34+
if (sourceURL.startsWith('file:///')) {
35+
filePath = new URL(sourceURL).pathname;
36+
} else if (sourceURL.includes('://')) {
37+
// $FlowFixMe[cannot-resolve-name]
3338
if (!__IS_INTERNAL_VERSION__) {
3439
// In this case, we can't really determine the path to a file, disable a button
3540
return {url: null, shouldDisableButton: true};
@@ -42,20 +47,22 @@ function checkConditions(
4247
if (endOfSourceMapURLIndex === -1) {
4348
return {url: null, shouldDisableButton: true};
4449
} else {
45-
sourceURL = sourceURL.slice(
50+
filePath = sourceURL.slice(
4651
endOfSourceMapURLIndex + endOfSourceMapURLPattern.length,
4752
sourceURL.length,
4853
);
4954
}
5055
}
56+
} else {
57+
filePath = sourceURL;
5158
}
5259

53-
const lineNumberAsString = String(source.line);
60+
const lineNumberAsString = String(line);
5461

5562
url.href = url.href
56-
.replace('{path}', sourceURL)
63+
.replace('{path}', filePath)
5764
.replace('{line}', lineNumberAsString)
58-
.replace('%7Bpath%7D', sourceURL)
65+
.replace('%7Bpath%7D', filePath)
5966
.replace('%7Bline%7D', lineNumberAsString);
6067

6168
return {url, shouldDisableButton: false};

0 commit comments

Comments
 (0)