Skip to content

Commit bab59a1

Browse files
committed
Fix backslash escaping for CodeQL warnings and update .gitignore
1 parent 042c2d0 commit bab59a1

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ extension.js
4343
extension.js.map
4444
*.wasm
4545
i18n/
46+
47+
# CodeQL local analysis artifacts
48+
/webview-ui/codeql-db/
49+
/codeql-results.sarif

webview-ui/src/utils/context-mentions.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ export function insertMention(
6565
*/
6666
// Escape spaces, handling already escaped spaces
6767
const formattedValue = value
68-
.replace(/\\\\/g, "\\DOUBLE_BACKSLASH") // First preserve actual double backslashes
69-
.replace(/\\ /g, "\\ESCAPED_SPACE") // Temporarily replace already escaped spaces
70-
.replace(/ /g, "\\ ") // Escape all normal spaces
71-
.replace(/\\ESCAPED_SPACE/g, "\\\\ ") // Restore escaped spaces with proper escaping
72-
.replace(/\\DOUBLE_BACKSLASH/g, "\\\\") // Restore actual double backslashes
68+
.replace(/\\\\\\\\/g, "\\DOUBLE_BACKSLASH") // First preserve actual double backslashes
69+
.replace(/\\\\ /g, "\\ESCAPED_SPACE") // Temporarily replace already escaped spaces
70+
// ADDED: Escape standalone backslashes not part of the above patterns
71+
.replace(/(?<!\\\\)\\\\(?! |\\\\)/g, '\\\\\\\\') // Makes \ -> \\
72+
.replace(/ /g, "\\ ") // Escape normal spaces (makes ' ' -> '\\ ')
73+
.replace(/\\ESCAPED_SPACE/g, "\\ ") // Restore escaped spaces correctly (makes '\\ESCAPED_SPACE' -> '\\ ')
74+
.replace(/\\DOUBLE_BACKSLASH/g, "\\\\\\\\") // Restore actual double backslashes (makes \\)
7375

7476
if (lastAtIndex !== -1) {
7577
// If there's an '@' symbol, replace text after it up to the next space/end

webview-ui/src/utils/path-mentions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function convertToMentionPath(path: string, cwd?: string): string {
1818

1919
if (!normalizedCwd) {
2020
// If no CWD, just escape spaces in the original path
21-
return processedPath.replace(/ /g, "\\ ");
21+
return processedPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ ');
2222
}
2323

2424
// Remove trailing slash from cwd if it exists
@@ -53,10 +53,11 @@ export function convertToMentionPath(path: string, cwd?: string): string {
5353
* will undergo a second round of escaping, resulting in double backslashes.
5454
* This is necessary to preserve the escapes through the entire text processing pipeline.
5555
*/
56-
// Escape spaces
57-
return mentionPath.replace(/ /g, "\\ ");
56+
// Escape backslashes first, then spaces (single backslash for space)
57+
return mentionPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ ');
5858
}
5959

6060
// If path doesn't start with CWD, escape spaces in the processed path
61-
return processedPath.replace(/ /g, "\\ ");
61+
// Escape backslashes first, then spaces (single backslash for space)
62+
return processedPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ ');
6263
}

0 commit comments

Comments
 (0)