Skip to content

Commit

Permalink
fix: quote() should include newline in literal
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 1, 2024
1 parent 408d2a0 commit 6c2cfa1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down Expand Up @@ -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`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion utils/string_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions utils/string_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "\\";
}
Expand Down

0 comments on commit 6c2cfa1

Please sign in to comment.