Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/js/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
// escapeForTemplateLiteral escapes characters that have special meaning in
// JavaScript template literals
func escapeForTemplateLiteral(s string) string {
// Escape backticks so they don't terminate the template literal.
// Also escape backslashes that precede backticks to avoid double-escaping issues.
s = strings.ReplaceAll(s, "\\`", "\\\\`") // First escape already-escaped backticks
s = strings.ReplaceAll(s, "`", "\\`") // Then escape remaining backticks
// Escape backslashes first (must be done before backticks to avoid double-escaping)
s = strings.ReplaceAll(s, "\\", "\\\\")
// Then escape backticks so they don't terminate the template literal
s = strings.ReplaceAll(s, "`", "\\`")
return s
}

Expand Down
42 changes: 42 additions & 0 deletions pkg/js/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,48 @@ func TestExpand(t *testing.T) {
envVars: map[string]string{},
expected: "Use `inline` and ```block``` code",
},
{
name: "single backslash",
commands: "test\\value",
envVars: map[string]string{},
expected: "test\\value",
},
{
name: "backslash n (not newline)",
commands: "test\\nvalue",
envVars: map[string]string{},
expected: "test\\nvalue",
},
{
name: "backslash t (not tab)",
commands: "test\\tvalue",
envVars: map[string]string{},
expected: "test\\tvalue",
},
{
name: "windows path",
commands: "C:\\Users\\Alice\\Documents",
envVars: map[string]string{},
expected: "C:\\Users\\Alice\\Documents",
},
{
name: "network path",
commands: "\\\\server\\share\\file",
envVars: map[string]string{},
expected: "\\\\server\\share\\file",
},
{
name: "multiple backslashes",
commands: "test\\\\value",
envVars: map[string]string{},
expected: "test\\\\value",
},
{
name: "regex pattern with backslashes",
commands: "\\d+\\.\\d+",
envVars: map[string]string{},
expected: "\\d+\\.\\d+",
},
}

for _, tt := range tests {
Expand Down