Semantic Diff

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

4 commits

643e9c03dbf69629e7d42ce5652a50b828982210..pr-9374

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

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

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.

modifiedJS raw code ending with a line comment

let add = (a, b) => a + blet f: int => int = %raw(`x => x + 1 // add one`)let two = add(%raw(`1 // one`), 1)
js
Before
After
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.
8cdb39903 · mununki · Nov 22, 2026, 05:00 UTC

Look for a line comment after the last line feed only

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.

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

Find a line comment at the end of raw code in the parse that classifies 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.
53524f8ba · mununki · Nov 28, 2026, 13:15 UTC

Drop the case of raw code without a token from the line comment check

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.