Skip to content

Preserve blank documents when formatting with keepLines - #157

Open
Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/keep-lines-blank-documents
Open

Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/keep-lines-blank-documents

Conversation

@pradeep-ramola

Copy link
Copy Markdown

Fixes #156.

With keepLines: true, formatting a whitespace-only document inserts another copy of its line breaks at offset zero. Starting with one blank line, three format operations produce 2, 4, then 8 breaks.

Only insert the leading-line-break edit when the first scanned token is not EOF. Whitespace-only documents then retain the existing no-edit behavior of empty/single-line blank documents, while leading blank lines before JSON or comments continue to be preserved.

Added 11 regression/control tests covering LF, CRLF, CR, multiple blank lines, surrounding spaces/tabs, final-newline options, repeated formatting, empty/single-line inputs, and leading lines before actual content.

Validation on Windows:

  • Before the fix: six new tests fail and five controls pass.
  • npm test with Node 22.23.5: compilation, ESLint, and all 99 tests pass.
  • Full compiled test suite with Node 24.16.0: all 99 tests pass.
  • 32,760 additional formatting/idempotence checks pass across 1,365 whitespace-only inputs, both indentation/final-newline options, whole-document/range formatting, and three successive passes.
  • git diff --check: passes.

This branch is based directly on main and does not include #155. No dependency or public API changes. Linux and macOS were not tested locally.

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.

keepLines doubles line breaks when formatting blank documents

1 participant