| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {ReactFunctionLocation, ReactCallSite} from 'shared/ReactTypes'; |
| 11 | |
| 12 | export function checkConditions( |
| 13 | editorURL: string, |
| 14 | source: ReactFunctionLocation | ReactCallSite, |
| 15 | ): {url: URL | null, shouldDisableButton: boolean} { |
| 16 | try { |
| 17 | const url = new URL(editorURL); |
| 18 | |
| 19 | const [, sourceURL, line, column] = source; |
| 20 | let filePath; |
| 21 | |
| 22 | // Check if sourceURL is a correct URL, which has a protocol specified |
| 23 | if (sourceURL.startsWith('file:///')) { |
| 24 | filePath = new URL(sourceURL).pathname; |
| 25 | } else if (sourceURL.includes('://')) { |
| 26 | // $FlowFixMe[cannot-resolve-name] |
| 27 | if (!__IS_INTERNAL_VERSION__) { |
| 28 | // In this case, we can't really determine the path to a file, disable a button |
| 29 | return {url: null, shouldDisableButton: true}; |
| 30 | } else { |
| 31 | const endOfSourceMapURLPattern = '.js/'; |
| 32 | const endOfSourceMapURLIndex = sourceURL.lastIndexOf( |
| 33 | endOfSourceMapURLPattern, |
| 34 | ); |
| 35 | |
| 36 | if (endOfSourceMapURLIndex === -1) { |
| 37 | return {url: null, shouldDisableButton: true}; |
| 38 | } else { |
| 39 | filePath = sourceURL.slice( |
| 40 | endOfSourceMapURLIndex + endOfSourceMapURLPattern.length, |
| 41 | sourceURL.length, |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | } else { |
| 46 | filePath = sourceURL; |
| 47 | } |
| 48 | |
| 49 | const lineNumberAsString = String(line); |
| 50 | const columnNumberAsString = String(column); |
| 51 | |
| 52 | url.href = url.href |
| 53 | .replace('{path}', filePath) |
| 54 | .replace('{line}', lineNumberAsString) |
| 55 | .replace('{column}', columnNumberAsString) |
| 56 | .replace('%7Bpath%7D', filePath) |
| 57 | .replace('%7Bline%7D', lineNumberAsString) |
| 58 | .replace('%7Bcolumn%7D', columnNumberAsString); |
| 59 | |
| 60 | return {url, shouldDisableButton: false}; |
| 61 | } catch (e) { |
| 62 | // User has provided incorrect editor url |
| 63 | return {url: null, shouldDisableButton: true}; |
| 64 | } |
| 65 | } |