Skip to content

Commit ffeb8ad

Browse files
committed
Fix edge case where // inside a sequence wrapped in braces didn't break.
```reason let x = { // }; ``` If the { }-sequence didn't break we would get parse error: ```reason let x = { // }; ```
1 parent 0199ea6 commit ffeb8ad

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

formatTest/unit_tests/expected_output/lineComments.re

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ let tuple_equal = ((csu, mgd)) =>
353353
*/
354354
let fun_def_comment_inline = () => {/* */};
355355

356+
let fun_def_comment_newline = () => {
357+
//
358+
};
359+
356360
let fun_def_comment_long = () => {
357361
/* longer comment inside empty function body */
358362
};

formatTest/unit_tests/input/lineComments.re

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ let tuple_equal((csu, mgd)) =
361361
*/
362362
let fun_def_comment_inline = () => { /* */ };
363363

364+
let fun_def_comment_newline = () => {
365+
//
366+
};
367+
364368
let fun_def_comment_long = () => { /* longer comment inside empty function body */};
365369

366370
let trueThing = true;

src/reason-parser/reason_comment.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ let is_doc t =
4848

4949
let make ~location category text =
5050
{ text; category; location }
51+
52+
let isLineComment {category; text} = match category with
53+
| SingleLine -> Reason_syntax_util.isLineComment text
54+
| EndOfLine | Regular -> false

src/reason-parser/reason_pprint_ast.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,22 @@ let rec insertSingleLineComment layout comment =
14871487
| Easy _ ->
14881488
prependSingleLineComment comment layout
14891489
| Sequence (listConfig, subLayouts) when subLayouts == [] ->
1490-
(* If there are no subLayouts (empty body), create a Sequence of just the comment *)
1490+
(* If there are no subLayouts (empty body), create a Sequence of just the
1491+
* comment. We need to be careful when the empty body contains a //-style
1492+
* comment. Example:
1493+
* let make = () => {
1494+
* //
1495+
* };
1496+
* It is clear that the sequence needs to always break here, otherwise
1497+
* we get a parse error: let make = () => { // };
1498+
* The closing brace and semicolon `};` would become part of the comment…
1499+
*)
1500+
let listConfig =
1501+
if Reason_comment.isLineComment comment then
1502+
{listConfig with break = Always_rec}
1503+
else
1504+
listConfig
1505+
in
14911506
Sequence (listConfig, [formatComment comment])
14921507
| Sequence (listConfig, subLayouts) ->
14931508
let (beforeComment, afterComment) =

0 commit comments

Comments
 (0)