Skip to content

Take the formatter's line endings from a line break outside literals and comments - #9201

Merged
cknitt merged 1 commit into
masterfrom
drift-9178-line-endings
Nov 4, 2026
Merged

cknitt merged 1 commit into
masterfrom
drift-9178-line-endings

Conversation

@cknitt

@cknitt cknitt commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #9178

bsc -format and the editor's format command picked LF or CRLF output from the first \n in the raw source. When that \n sat 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 \n came 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 newline field: CRLF when a \r precedes that LF, LF otherwise, including when there is no such line break (only lone CRs, or a single line).

Res_line_endings is 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.ml gets 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

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Take the formatter's line endings from a line break outside string literals and comments.

Semantic Diff

1 commit · Full page

✏️ Take the formatter's line endings from a line break between tokens

85e903d · cknitt + Claude · Nov 03, 2026, 18:27 UTC · edited

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)

let a = "x\r\ny"
let b = 1

text · bytes of bsc -format

-let a = "x\r\ny"\r\n\r\nlet b = 1\r\n
+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.

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.75862% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.73%. Comparing base (ac73578) to head (85e903d).

Files with missing lines Patch % Lines
compiler/syntax/cli/res_cli.ml 54.54% 5 Missing ⚠️
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     
Files with missing lines Coverage Δ
analysis/src/commands.ml 62.50% <ø> (+0.43%) ⬆️
compiler/syntax/src/res_driver.ml 89.83% <100.00%> (+1.15%) ⬆️
compiler/syntax/src/res_multi_printer.ml 78.57% <100.00%> (-1.43%) ⬇️
compiler/syntax/src/res_parser.ml 96.12% <100.00%> (+0.25%) ⬆️
compiler/syntax/src/res_scanner.ml 91.69% <100.00%> (+0.04%) ⬆️
compiler/syntax/cli/res_cli.ml 76.00% <54.54%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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>
@cknitt
cknitt force-pushed the drift-9178-line-endings branch from 92416a0 to 85e903d Compare November 3, 2026 18:35
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T18:52:28.760073Z 85e903d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 85e903d297

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9201

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9201

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9201

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9201

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9201

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9201

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9201

commit: 85e903d

@cknitt
cknitt marked this pull request as ready for review November 3, 2026 19:00
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@cknitt
cknitt requested a review from cristianoc November 4, 2026 05:51
@cknitt
cknitt merged commit 1457839 into master Nov 4, 2026
26 checks passed
@cknitt
cknitt deleted the drift-9178-line-endings branch November 4, 2026 08:25
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.

Formatter takes the file's line ending from a line break inside a string literal or comment

2 participants