Skip to content

fix(dedent): stop removing spaces within non-indented lines#82

Open
Yanhu007 wants to merge 1 commit intomuesli:masterfrom
Yanhu007:fix/dedent-non-indent-spaces
Open

fix(dedent): stop removing spaces within non-indented lines#82
Yanhu007 wants to merge 1 commit intomuesli:masterfrom
Yanhu007:fix/dedent-non-indent-spaces

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #72

Problem

dedent.String incorrectly removes spaces from within lines that have less indentation than the minimum:

input := "This is an example\n\n  with no shared indent."
dedent.String(input)
// Output: "Thisisan example\n\nwith no shared indent."
//          ^^^^^^^^ spaces between words removed!

Root Cause

The dedent function sets shouldOmit = false only in the space/tab case (after omitting enough), but not in the default case. So after encountering the first non-whitespace character, shouldOmit remains true, and subsequent spaces in the same line are counted toward the omit quota.

Fix

Set shouldOmit = false in the default case, so that only leading whitespace is eligible for removal.

// After fix:
"This is an example\n\n  with no shared indent.""This is an example\n\nwith no shared indent."
//  ✅ spaces within words preserved

All existing tests pass.

The dedent function failed to set shouldOmit=false in the default
(non-whitespace) case. This caused spaces within a line to be
incorrectly removed when the line had less indentation than the
computed minimum.

For example, "This is an example" would become "Thisisan example"
because the spaces between words were counted toward the omit quota.

Fixes muesli#72
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dedent incorrectly removes spaces that aren't at the start of a line

1 participant