Skip to content

Commit db66883

Browse files
authored
Merge pull request #1094 from dgageot/fix-1085
Fix #1085
2 parents a2b4487 + 22daa4f commit db66883

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

pkg/js/expand.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
// escapeForTemplateLiteral escapes characters that have special meaning in
1515
// JavaScript template literals
1616
func escapeForTemplateLiteral(s string) string {
17-
// Escape backticks so they don't terminate the template literal.
18-
// Also escape backslashes that precede backticks to avoid double-escaping issues.
19-
s = strings.ReplaceAll(s, "\\`", "\\\\`") // First escape already-escaped backticks
20-
s = strings.ReplaceAll(s, "`", "\\`") // Then escape remaining backticks
17+
// Escape backslashes first (must be done before backticks to avoid double-escaping)
18+
s = strings.ReplaceAll(s, "\\", "\\\\")
19+
// Then escape backticks so they don't terminate the template literal
20+
s = strings.ReplaceAll(s, "`", "\\`")
2121
return s
2222
}
2323

pkg/js/expand_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ func TestExpand(t *testing.T) {
7171
envVars: map[string]string{},
7272
expected: "Use `inline` and ```block``` code",
7373
},
74+
{
75+
name: "single backslash",
76+
commands: "test\\value",
77+
envVars: map[string]string{},
78+
expected: "test\\value",
79+
},
80+
{
81+
name: "backslash n (not newline)",
82+
commands: "test\\nvalue",
83+
envVars: map[string]string{},
84+
expected: "test\\nvalue",
85+
},
86+
{
87+
name: "backslash t (not tab)",
88+
commands: "test\\tvalue",
89+
envVars: map[string]string{},
90+
expected: "test\\tvalue",
91+
},
92+
{
93+
name: "windows path",
94+
commands: "C:\\Users\\Alice\\Documents",
95+
envVars: map[string]string{},
96+
expected: "C:\\Users\\Alice\\Documents",
97+
},
98+
{
99+
name: "network path",
100+
commands: "\\\\server\\share\\file",
101+
envVars: map[string]string{},
102+
expected: "\\\\server\\share\\file",
103+
},
104+
{
105+
name: "multiple backslashes",
106+
commands: "test\\\\value",
107+
envVars: map[string]string{},
108+
expected: "test\\\\value",
109+
},
110+
{
111+
name: "regex pattern with backslashes",
112+
commands: "\\d+\\.\\d+",
113+
envVars: map[string]string{},
114+
expected: "\\d+\\.\\d+",
115+
},
74116
}
75117

76118
for _, tt := range tests {

0 commit comments

Comments
 (0)