|
8 | 8 |
|
9 | 9 | import { Uri, WorkspaceFolder } from 'vscode'; |
10 | 10 | import { getWorkspaceFolder } from '../../../common/vscodeapi'; |
| 11 | +import { sendTelemetryEvent } from '../../../telemetry'; |
| 12 | +import { EventName } from '../../../telemetry/constants'; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * @returns whether the provided parameter is a JavaScript String or not. |
14 | 16 | */ |
15 | 17 | function isString(str: any): str is string { |
16 | | - if (typeof str === 'string' || str instanceof String) { |
17 | | - return true; |
18 | | - } |
19 | | - |
20 | | - return false; |
| 18 | + return typeof str === 'string' || str instanceof String; |
21 | 19 | } |
22 | 20 |
|
23 | | -export function resolveVariables( |
| 21 | +/** |
| 22 | + * Resolves VS Code variable placeholders in a string value. |
| 23 | + * |
| 24 | + * Specifically handles: |
| 25 | + * - `${workspaceFolder}` - replaced with the workspace folder path |
| 26 | + * - `${env.VAR}` or `${env:VAR}` - replaced with empty string |
| 27 | + * - Unknown variables - left as-is in the original `${variable}` format |
| 28 | + * |
| 29 | + * @param value The string containing variable placeholders to resolve |
| 30 | + * @param rootFolder Fallback folder path to use if no workspace folder is available |
| 31 | + * @param folder The workspace folder context for variable resolution |
| 32 | + * @returns The string with variables resolved, or undefined if input was undefined |
| 33 | + */ |
| 34 | +export function resolveWorkspaceVariables( |
24 | 35 | value: string | undefined, |
25 | 36 | rootFolder: string | Uri | undefined, |
26 | 37 | folder: WorkspaceFolder | undefined, |
27 | 38 | ): string | undefined { |
28 | | - if (value) { |
29 | | - const workspaceFolder = folder ? getWorkspaceFolder(folder.uri) : undefined; |
30 | | - const variablesObject: { [key: string]: any } = {}; |
31 | | - variablesObject.workspaceFolder = workspaceFolder ? workspaceFolder.uri.fsPath : rootFolder; |
32 | | - |
33 | | - const regexp = /\$\{(.*?)\}/g; |
34 | | - return value.replace(regexp, (match: string, name: string) => { |
35 | | - const newValue = variablesObject[name]; |
36 | | - if (isString(newValue)) { |
37 | | - return newValue; |
38 | | - } |
39 | | - return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match; |
40 | | - }); |
| 39 | + if (!value) { |
| 40 | + return value; |
41 | 41 | } |
42 | | - return value; |
| 42 | + |
| 43 | + // opt for folder with fallback to rootFolder |
| 44 | + const workspaceFolder = folder ? getWorkspaceFolder(folder.uri) : undefined; |
| 45 | + const workspaceFolderPath = workspaceFolder ? workspaceFolder.uri.fsPath : rootFolder; |
| 46 | + |
| 47 | + // Replace all ${variable} patterns |
| 48 | + return value.replace(/\$\{([^}]+)\}/g, (match: string, variableName: string) => { |
| 49 | + // Handle workspaceFolder variable |
| 50 | + if (variableName === 'workspaceFolder' && isString(workspaceFolderPath)) { |
| 51 | + // Track usage of this potentially deprecated code path |
| 52 | + sendTelemetryEvent(EventName.DEPRECATED_CODE_PATH_USAGE, undefined, { |
| 53 | + codePath: 'workspaceFolder_substitution', |
| 54 | + }); |
| 55 | + return workspaceFolderPath; |
| 56 | + } |
| 57 | + |
| 58 | + // Replace environment variables with empty string |
| 59 | + if (variableName.startsWith('env.') || variableName.startsWith('env:')) { |
| 60 | + // Track usage of this potentially deprecated code path |
| 61 | + sendTelemetryEvent(EventName.DEPRECATED_CODE_PATH_USAGE, undefined, { |
| 62 | + codePath: 'env_variable_substitution', |
| 63 | + }); |
| 64 | + return ''; |
| 65 | + } |
| 66 | + |
| 67 | + // Unknown variables are left unchanged |
| 68 | + return match; |
| 69 | + }); |
43 | 70 | } |
0 commit comments