Skip to content

Commit 76eabe8

Browse files
authored
fix: consistently escape escape characters (#58)
Remove the "already escaped" logic as it was buggy, we should just always escape escape characters closes #56 fixes sveltejs/svelte#15476 fixes #57
1 parent 49d433b commit 76eabe8

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

.changeset/gold-toys-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'esrap': patch
3+
---
4+
5+
fix: consistently escape escape characters

src/handlers.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,10 @@ function prepend_comments(comments, state, newlines) {
116116
*/
117117
function quote(string, char) {
118118
let out = char;
119-
let escaped = false;
120119

121120
for (const c of string) {
122-
if (escaped) {
123-
out += c;
124-
escaped = false;
125-
} else if (c === '\\') {
121+
if (c === '\\') {
126122
out += '\\\\';
127-
escaped = true;
128123
} else if (c === char) {
129124
out += '\\' + c;
130125
} else if (c === '\n') {

test/quotes.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ test('escapes escape characters', () => {
8282
expect(code).toMatchInlineSnapshot(`"const str = 'a\\\\nb';"`);
8383
});
8484

85+
test('escapes escape characters#2', () => {
86+
const ast = load('const str = "a\\\\\\nb"');
87+
clean(ast);
88+
const code = print(ast).code;
89+
90+
expect(code).toMatchInlineSnapshot(`"const str = 'a\\\\\\nb';"`);
91+
});
92+
93+
test('escapes double escaped backslashes', () => {
94+
const ast = load("var text = $.text('\\\\\\\\');");
95+
clean(ast);
96+
const code = print(ast).code;
97+
98+
expect(code).toMatchInlineSnapshot(`"var text = $.text('\\\\\\\\');"`);
99+
});
100+
85101
test('does not escape already-escaped single quotes', () => {
86102
const ast = load(`const str = 'a\\'b'`);
87103
clean(ast);

0 commit comments

Comments
 (0)