Skip to content

Print a line break after raw code that ends with a line comment - #9374

Open
mununki wants to merge 4 commits into
masterfrom
end-raw-code-line-comments
Open

mununki wants to merge 4 commits into
masterfrom
end-raw-code-line-comments

Conversation

@mununki

@mununki mununki commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Fixes: #9354

A %raw expression is printed on the same line as what follows it, so a line comment at its end commented that out. Around raw code, the closing parenthesis was lost, a syntax error: %raw(`x => x + 1 // add one`) compiled to (x => x + 1 // add one). After a raw literal, which is printed without parentheses, the rest of the expression was lost without an error: add(%raw(`1 // one`), 1) compiled to 1 // one + 1 | 0 and returned 1. A line break at the end of the code does not help, since E.raw_js_code trims it.

Js_dump now prints a line break after raw code that a line comment runs to the end of. Classify_function.classify_raw finds it in the parse that classifies the code, from the comments that the Flow lexer records: the last one is a line comment that starts after the last token. translcore keeps the result in the new field ends_in_line_comment of Js_raw_info.t, which Js_dump reads without parsing again.

  • A // in a string, a template, a regular expression or a block comment is not a comment, so the string literal "http://example.com" is printed as before. A line comment ends at any line terminator, a carriage return included. Code that the Flow parser rejects gets the line break.
  • %%raw statements are already printed on lines of their own.
  • No checked-in output changes.
  • Js_raw_info.t is part of the Lambda that a .cmj keeps for inlining, so the format of .cmj files changes, as with other changes to Lambda.

Tests: raw_line_comment_test.res checks a raw function, a raw literal as an argument, a called raw function, a line comment before the last line, a line comment after a block comment and after a carriage return, and a string, a regular expression and a block comment holding //. Compiled with master, the module fails to load with a SyntaxError, and without the cases that give one, the raw literals as arguments that end in a line comment return 1 instead of 2.

Checklist

  • Added a changelog fragment (see changes/README.md), or no release notes are needed and a maintainer should apply changelog:skip.
  • Added tests for the fix or feature.
  • Gave every commit a description below its subject, saying why it is made (see "Commit messages" in CONTRIBUTING.md).
  • Signed off every commit for the DCO (git commit -s).

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a line comment at the end of %raw code commenting out the generated code after it.

Semantic Diff

4 commits · Full page

Print a line break after raw code that ends with a line comment

b8484a6 · mununki · Nov 22, 2026, 04:48 UTC

Why

A %raw expression is printed on the same line as what follows it, so a line comment at its end commented that out: the parenthesis that closes raw code, which gave a syntax error, or the rest of an expression after a raw literal, which gave another value without an error. A line break at the end of the code does not help, since E.raw_js_code trims it. Js_dump now prints a line break after the code when Classify_function.ends_in_line_comment finds that a line comment runs to its end: its last line holds "//", and the code followed by ) does not parse.

JS modified (raw code ending with a line comment)

let add = (a, b) => a + b
let f: int => int = %raw(`x => x + 1 // add one`)
let two = add(%raw(`1 // one`), 1)

js

-let f = (x => x + 1 // add one);
+let f = (x => x + 1 // add one
+);
 
-let two = 1 // one + 1 | 0;
+let two = 1 // one
+ + 1 | 0;

Review

  • Why parse the code? A "//" on the last line may be in a string, a template or a regular expression, as in the string literal "http://example.com", which is printed as before. Only code whose last line holds "//" is parsed, and code that the Flow parser rejects gets the line break.
  • What about %%raw? A raw statement is already printed on lines of its own.
  • Which checked-in outputs change? None.
Look for a line comment after the last line feed only

8cdb399 · mununki · Nov 22, 2026, 05:00 UTC

The scan only spares the parse for code that cannot end in a line comment, and one that runs to the end starts after the last line feed. Codecov found the carriage return case untested, and the parse already ends a line comment there, so code whose last line break is a lone carriage return is now parsed, and the output does not change.

Find a line comment at the end of raw code in the parse that classifies it

c0f01c5 · mununki · Nov 28, 2026, 13:02 UTC

Classify_function.ends_in_line_comment looked for "//" on the last line of a raw expression and then parsed the code once more, in parentheses, as Js_dump prints it, taking a parse error for a comment that runs to the end. That was a third parse of the code, after Ast_raw_validate and classify. The lexer of the Flow parser already records each comment with its kind and location, so classify_raw now reads them in the parse that classifies the code: it ends in a line comment when the last comment is a line comment that starts after the last token. translcore keeps the result in the new field ends_in_line_comment of Js_raw_info.t, and Js_dump reads it instead of parsing, as the review of #9374 suggested.

Tests: raw_line_comment_test.res adds a line comment after a block comment, one after a carriage return, which the code of afterReturn holds, and a "//" in a block comment. All checked-in outputs, these cases included, are the same with the previous commit.

Review

  • Does the format of .cmj files change? Yes: Js_raw_info.t is part of the Lambda that a .cmj keeps for inlining, as with any other change to Lambda. A .cmj has no magic number, and a build makes it again when the compiler changes.
✨ Drop the case of raw code without a token from the line comment check

53524f8 · mununki · Nov 28, 2026, 13:15 UTC · new

Codecov found the case for code without a token untested. Such code, a raw expression that is only a comment, does not parse as an expression, so the check already counts it as ending in a line comment through its parse errors, before it looks at the comments. The case never ran. The output does not change: %raw(// only a comment) still gets a line break, after the warning that the code does not parse.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,712,969 → 655,721,036 (±0.00%).

Generated projects: 629,286,110 → 629,286,479 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,224,546,906 → 9,224,108,275 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 26,662,166 → 26,662,491 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 53524f8 compared with base 83cf1de in this run.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.34%. Comparing base (643e9c0) to head (53524f8).
⚠️ Report is 23 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9374      +/-   ##
==========================================
+ Coverage   82.21%   82.34%   +0.13%     
==========================================
  Files         460      462       +2     
  Lines       65551    65817     +266     
==========================================
+ Hits        53891    54198     +307     
+ Misses      11660    11619      -41     
Files with missing lines Coverage Δ
compiler/backend/js_dump.ml 92.60% <ø> (-1.48%) ⬇️
compiler/backend/js_exp_make.ml 67.71% <100.00%> (+1.24%) ⬆️
compiler/backend/lam_compile_primitive.ml 81.49% <100.00%> (ø)
compiler/typing/classify_function.ml 86.20% <100.00%> (+4.38%) ⬆️
compiler/typing/translcore.ml 88.28% <100.00%> (ø)

... and 35 files with indirect coverage changes

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 53524f8

@github-actions

Copy link
Copy Markdown

@mununki
mununki requested review from cknitt and cristianoc November 22, 2026 14:17
Why:
    A `%raw` expression is printed on the same line as what follows it, so
    a line comment at its end commented that out: the parenthesis that
    closes raw code, which gave a syntax error, or the rest of an
    expression after a raw literal, which gave another value without an
    error. A line break at the end of the code does not help, since
    `E.raw_js_code` trims it. `Js_dump` now prints a line break after the
    code when `Classify_function.ends_in_line_comment` finds that a line
    comment runs to its end: its last line holds "//", and the code
    followed by `)` does not parse.

JS modified (raw code ending with a line comment):
Example:
    let add = (a, b) => a + b
    let f: int => int = %raw(`x => x + 1 // add one`)
    let two = add(%raw(`1 // one`), 1)
Before (js):
    let f = (x => x + 1 // add one);

    let two = 1 // one + 1 | 0;
After (js):
    let f = (x => x + 1 // add one
    );

    let two = 1 // one
     + 1 | 0;

Review: Why parse the code? A "//" on the last line may be in a string,
    a template or a regular expression, as in the string literal
    `"http://example.com"`, which is printed as before. Only code whose
    last line holds "//" is parsed, and code that the Flow parser rejects
    gets the line break.
Review: What about `%%raw`? A raw statement is already printed on lines of
    its own.
Review: Which checked-in outputs change? None.

Signed-off-by: mununki <woonki.moon@gmail.com>
The scan only spares the parse for code that cannot end in a line
comment, and one that runs to the end starts after the last line feed.
Codecov found the carriage return case untested, and the parse already
ends a line comment there, so code whose last line break is a lone
carriage return is now parsed, and the output does not change.

Signed-off-by: mununki <woonki.moon@gmail.com>
@mununki
mununki force-pushed the end-raw-code-line-comments branch from 49656f2 to 8cdb399 Compare November 23, 2026 08:31
@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member

This review was written by Claude (Claude Code).

The fix looks right to me, and adding the line break only when it's needed is better than always adding one (as %%raw does), which would change every raw function's output.

One suggestion on how the comment is found. ends_in_line_comment scans the last line for //, then reparses (code) and takes a parse error to mean a comment. That's a third parse of the raw code; ast_raw_validate and classify already parse it. Flow's lexer already records every comment with its kind and location, and classify_exp already uses kind = Line. Reading that list answers the question directly:

let ends_in_line_comment code =
  let open Parser_flow in
  let env = Parser_env.init_env None code in
  let _ = do_parse env Parse.expression false in
  let last_token = Parser_env.last_loc env in
  (* Eat EOF so that the comments before it reach [comments]. *)
  Parser_env.Eat.token env;
  match (Parser_env.comments env, last_token) with
  | (loc, {Flow_ast.Comment.kind = Line}) :: _, Some last ->
    Loc.pos_cmp loc.start last._end >= 0
  | (_, {Flow_ast.Comment.kind = Line}) :: _, None -> true
  | _ -> false

Better still, do this inside classify, which already has the parse, and keep the result in Js_raw_info. Then Js_dump reads a flag and doesn't parse at all.

I checked this against the vendored parser. Besides your test cases, it also gives the right answer for 1\r// c, `a\n//b`, 1 /* a // b */, 1 /* a */ // b, a raw function whose body ends in a comment, and a snippet that is only a comment.

Optional: a test with \r before the comment would show that other line terminators work.

…es it

`Classify_function.ends_in_line_comment` looked for "//" on the last line
of a raw expression and then parsed the code once more, in parentheses, as
`Js_dump` prints it, taking a parse error for a comment that runs to the end.
That was a third parse of the code, after `Ast_raw_validate` and `classify`.
The lexer of the Flow parser already records each comment with its kind and
location, so `classify_raw` now reads them in the parse that classifies the
code: it ends in a line comment when the last comment is a line comment that
starts after the last token. `translcore` keeps the result in the new field
`ends_in_line_comment` of `Js_raw_info.t`, and `Js_dump` reads it instead of
parsing, as the review of #9374 suggested.

Tests: `raw_line_comment_test.res` adds a line comment after a block comment,
one after a carriage return, which the code of `afterReturn` holds, and a
"//" in a block comment. All checked-in outputs, these cases included, are
the same with the previous commit.

Review: Does the format of `.cmj` files change? Yes: `Js_raw_info.t` is part
    of the Lambda that a `.cmj` keeps for inlining, as with any other change
    to Lambda. A `.cmj` has no magic number, and a build makes it again when
    the compiler changes.

Signed-off-by: mununki <woonki.moon@gmail.com>
@mununki

mununki commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review and the snippet. Done in c0f01c5, as you suggest: classify_raw reads the comments that the Flow lexer records, in the parse that classifies the code, and translcore keeps the result in a new ends_in_line_comment field of Js_raw_info.t. Js_dump reads the flag and no longer parses. All checked-in outputs are the same as with the previous commit.

The test now also has 1 /* a */ // b, a carriage return before the comment, and 1 /* a // b */. The carriage return is a literal character in the raw template, since %raw takes its code as it is, escapes included.

Codecov found the case for code without a token untested. Such code, a raw
expression that is only a comment, does not parse as an expression, so the
check already counts it as ending in a line comment through its parse
errors, before it looks at the comments. The case never ran. The output does
not change: `%raw(`// only a comment`)` still gets a line break, after the
warning that the code does not parse.

Signed-off-by: mununki <woonki.moon@gmail.com>
@cknitt

cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T13:43:07.202896Z 53524f8 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. 🎉

Reviewed commit: 53524f8baf

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

This branch has not been deployed

No deployments
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.

%raw code that ends with a line comment comments out the closing parenthesis

2 participants