Repository navigation
Take the formatter's line endings from a line break outside literals and comments - #9201
Conversation
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page ✏️ Take the formatter's line endings from a line break between tokens85e903d · cknitt + Claude · Nov 03, 2026, 18:27 UTC · edited
Format modified (line endings of an LF file) let a = "x\r\ny"
let b = 1
-let a = "x\r\ny"\r\n\r\nlet b = 1\r\n
+let a = "x\r\ny"\n\nlet b = 1\nReview
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9201 +/- ##
=======================================
Coverage 81.73% 81.73%
=======================================
Files 458 457 -1
Lines 65292 65304 +12
=======================================
+ Hits 53364 53377 +13
+ Misses 11928 11927 -1
🚀 New features to boost your workflow:
|
Why:
The formatter chose CRLF or LF from the first LF in the raw source, even
when it sat inside a string literal or a comment, so an LF file whose
first line holds "x\r\ny" was rewritten all-CRLF, and a CRLF file with a
bare LF in such a literal all-LF. The scanner now records the first LF it
skips as whitespace, the parser keeps it for the first consumed token
that follows one, and the parse result carries the newline; literals and
comments consume their own line breaks. With none, the output is LF.
Format modified (line endings of an LF file):
Example:
let a = "x\r\ny"
let b = 1
Before (text): bytes of bsc -format
let a = "x\r\ny"\r\n\r\nlet b = 1\r\n
After (text): bytes of bsc -format
let a = "x\r\ny"\n\nlet b = 1\n
Review: Why record the break on consumption rather than in the scanner alone?
Lookahead can read text after a backtick as code before the parser rescans
it as template text, so only consumed tokens count, and checkpoints restore
the recorded break when they roll back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb
Signed-off-by: Christoph Knittel <ck@cca.io>
92416a0 to
85e903d
Compare
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9201 |
Fixes #9178
bsc -formatand the editor's format command picked LF or CRLF output from the first\nin the raw source. When that\nsat inside a string literal or a comment, the code lines got the wrong line endings: an LF file whose first line holds"x\r\ny"came out all-CRLF, and a CRLF file whose first line holds a literal with a bare\ncame out all-LF.The newline now comes from the parse itself. The scanner records the first LF it skips as whitespace between tokens; strings, templates, regexes and comments consume their own line breaks, so they never set it, and the parser already drives the template and regex modes. The parser keeps the break of the first consumed token that follows one (including the end of file), so lookahead that reads template text as code before the parser rescans it does not count, and checkpoints restore it on rollback. The parse result gets a
newlinefield: CRLF when a\rprecedes that LF, LF otherwise, including when there is no such line break (only lone CRs, or a single line).Res_line_endingsis removed. The three callers take the newline from the parse result:Res_multi_printer.print_res(bsc -format),res_parser -print res, and the analysis format command.Comments the printer reindents still get their line breaks rewritten with the chosen newline, as before (#9116).
Tests
tests/syntax_tests/res_test.mlgets raw-bytes cases next to the existing #9042/#9116 ones: both directions for string literals, block and doc comments, template literals with interpolations and nested braces, a char literal'"', regex literals holding"(at the top level, in a JSX child and in an interpolation), divisions followed by a string holding/, a JSX closing tag followed by a string holding/, sources without any line break, and sources without a final newline.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb