(root: string, file: string | undefined)
| 52 | * is the exact fragility this function exists to remove. |
| 53 | */ |
| 54 | export function toWorkspaceRelative(root: string, file: string | undefined): string { |
| 55 | if (file === undefined || file.length === 0) { |
| 56 | return ''; |
| 57 | } |
| 58 | const path = file.startsWith('file://') |
| 59 | ? file.slice('file://'.length).split('/').map(decodeSegment).join('/') |
| 60 | : file; |
| 61 | const normalizedRoot = root.replace(/\/+$/, ''); |
| 62 | if (normalizedRoot.length > 0 && path.startsWith(`${normalizedRoot}/`)) { |
| 63 | return path.slice(normalizedRoot.length + 1); |
| 64 | } |
| 65 | return path; |
| 66 | } |
| 67 | |
| 68 | /** A segment that is not valid percent-encoding is taken literally, never dropped. */ |
| 69 | function decodeSegment(segment: string): string { |
no outgoing calls
no test coverage detected