Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions text/unstable_dedent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const WHITE_SPACE_ONLY_LINE_REGEXP = new RegExp(
* Removes indentation from multiline strings.
*
* - Removes leading newline
* - Removes trailing whitespace, including newlines
* - Removes a single trailing newline (with any preceding whitespace on that line)
* - Replaces whitespace-only lines with empty lines
* - Finds the minimum indentation among remaining lines and removes that much indentation from all of them
*
Expand Down Expand Up @@ -44,7 +44,7 @@ export function dedent(input: string): string;
* Removes indentation from multiline strings.
*
* - Removes leading newline
* - Removes trailing whitespace, including newlines
* - Removes a single trailing newline (with any preceding whitespace on that line)
* - Replaces whitespace-only lines with empty lines
* - Finds the minimum indentation among remaining lines and removes that much indentation from all of them
*
Expand Down Expand Up @@ -76,7 +76,10 @@ export function dedent(
// Substitute nonempty placeholder so multiline substitutions do not affect indent width.
const joinedTemplate = typeof input === "string" ? input : input.join("x");
const ignoreFirstUnindented = !joinedTemplate.startsWith("\n");
const trimmedTemplate = joinedTemplate.replace(/^\n/, "").trimEnd();
const trimmedTemplate = joinedTemplate.replace(/^\n/, "").replace(
/\n[\t ]*$/,
"",
);
const lines = trimmedTemplate.split("\n");

const linesToCheck = lines.slice(
Expand All @@ -90,7 +93,7 @@ export function dedent(
const inputString = typeof input === "string"
? input
: String.raw({ raw: input }, ...values);
const trimmedInput = inputString.replace(/^\n/, "").trimEnd();
const trimmedInput = inputString.replace(/^\n/, "").replace(/\n[\t ]*$/, "");

// No lines to indent
if (!indent) return trimmedInput;
Expand Down
9 changes: 9 additions & 0 deletions text/unstable_dedent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ Deno.test("dedent() handles example 2", () => {
);
});

// Test case for issue #6831
Deno.test("dedent() only strips single trailing newline", () => {
const result = dedent`
a

`;
assertEquals(result, "a\n");
});

Deno.test("dedent() handles empty lines", () => {
assertEquals(
dedent(`
Expand Down
Loading