From 6c2cfa1ca886573fee92099a8d884ef56c055f14 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 1 Mar 2024 13:51:48 -0500 Subject: [PATCH] fix: quote() should include newline in literal --- mod.test.ts | 4 ++-- utils/string_utils.test.ts | 2 +- utils/string_utils.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mod.test.ts b/mod.test.ts index d2b61ee..199223c 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -551,7 +551,7 @@ test`; }); it("should write out text surrounded by quotes and escape quotes and new lines", () => { - const expected = `"te\\"\\\r\nst"`; + const expected = `"te\\"\\r\\n\\\r\nst"`; doTest(expected, writer => { writer.quote("te\"\r\nst"); }); @@ -1155,7 +1155,7 @@ describe("#hangingIndent", () => { writer.hangingIndent(() => { writer.quote("t\nu").newLine().write("t"); }); - expect(writer.toString()).to.equal(`"t\\\nu"\n t`); + expect(writer.toString()).to.equal(`"t\\n\\\nu"\n t`); }); }); diff --git a/utils/string_utils.test.ts b/utils/string_utils.test.ts index b3a9033..cc15c0e 100644 --- a/utils/string_utils.test.ts +++ b/utils/string_utils.test.ts @@ -7,7 +7,7 @@ describe("escapeForWithinString", () => { } it("should escape the quotes and newline", () => { - doTest(`"testing\n this out"`, `\\"testing\\\n this out\\"`); + doTest(`"testing\n this out"`, `\\"testing\\n\\\n this out\\"`); }); function doQuoteTest(input: string, quote: string, expected: string) { diff --git a/utils/string_utils.ts b/utils/string_utils.ts index ba561e8..c21cc34 100644 --- a/utils/string_utils.ts +++ b/utils/string_utils.ts @@ -6,10 +6,10 @@ export function escapeForWithinString(str: string, quoteKind: string) { if (str[i] === quoteKind) { result += "\\"; } else if (str[i] === "\r" && str[i + 1] === "\n") { - result += "\\"; + result += "\\r\\n\\"; i++; // skip the \r } else if (str[i] === "\n") { - result += "\\"; + result += "\\n\\"; } else if (str[i] === "\\") { result += "\\"; }