Skip to content

Commit 030c3ec

Browse files
committed
fix: correctly escape \r
1 parent de464e6 commit 030c3ec

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

.changeset/rude-eagles-tease.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: correctly escape `\r`

src/handlers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function quote(string, char) {
129129
out += '\\' + c;
130130
} else if (c === '\n') {
131131
out += '\\n';
132+
} else if (c === '\r') {
133+
out += '\\r';
132134
} else {
133135
out += c;
134136
}

test/quotes.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ test('does not escape already-escaped double quotes', () => {
9393

9494
expect(code).toMatchInlineSnapshot(`"const str = 'a"b';"`);
9595
});
96+
97+
test.only('correctly handle \\n\\r', () => {
98+
const ast = load('const str = "a\\n\\rb"');
99+
clean(ast);
100+
const code = print(ast).code;
101+
102+
expect(code).toMatchInlineSnapshot(`"const str = 'a\\n\\rb';"`);
103+
});

0 commit comments

Comments
 (0)