Skip to content

Commit b82fbce

Browse files
authored
Fix comments formatted away in function with no args (#7095)
* Print comments inside fn with no arg * Add tests * Update CHANGELOG
1 parent 4bfe542 commit b82fbce

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080
3535
- Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094
3636
- Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058
37+
- Fix comments formatted away in function without arguments. https://github.com/rescript-lang/rescript-compiler/pull/7095
3738

3839
#### :nail_care: Polish
3940

compiler/syntax/src/res_printer.ml

+16-3
Original file line numberDiff line numberDiff line change
@@ -4842,9 +4842,22 @@ and print_arguments_with_callback_in_last_position ~state args cmt_tbl =
48424842
and print_arguments ~state ?(partial = false)
48434843
(args : (Asttypes.arg_label * Parsetree.expression) list) cmt_tbl =
48444844
match args with
4845-
| [(Nolabel, {pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)})]
4846-
->
4847-
Doc.text "()"
4845+
| [
4846+
( Nolabel,
4847+
{
4848+
pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _);
4849+
pexp_loc = loc;
4850+
} );
4851+
] ->
4852+
if has_leading_line_comment cmt_tbl loc then
4853+
let cmt = print_comments Doc.nil cmt_tbl loc in
4854+
Doc.concat
4855+
[
4856+
Doc.lparen;
4857+
Doc.indent (Doc.group (Doc.concat [Doc.soft_line; cmt]));
4858+
Doc.rparen;
4859+
]
4860+
else Doc.text "()"
48484861
| [(Nolabel, arg)] when ParsetreeViewer.is_huggable_expression arg ->
48494862
let arg_doc =
48504863
let doc = print_expression_with_comments ~state arg cmt_tbl in

tests/syntax_tests/printer/comments/expected/expr.res.txt

+26
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ let multiply = (
234234
/* c2 */ m2 /* c3 */,
235235
) => ()
236236

237+
f(
238+
// a
239+
// b
240+
// c
241+
)
242+
243+
f(
244+
// a
245+
// b
246+
// c
247+
)
248+
249+
let x = {
250+
f(
251+
// a
252+
// b
253+
// c
254+
)
255+
256+
f(
257+
// a
258+
// b
259+
// c
260+
)
261+
}
262+
237263
f(() => 1)
238264
// c1
239265
f(_ => 1)

tests/syntax_tests/printer/comments/expr.res

+29
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,35 @@ let f = (/* c0 */ ~greeting /* c1 */, /* c2 */ ~from /* c3 */ as /* c4 */ hometo
223223
let multiply = (/* c-2 */ type t /* c-1 */,/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => ()
224224
let multiply = (/* c-4 */ type t /* c-3 */,/* c0 */ m1 /* c1 */, /* c-2 */ type s /* c-1 */, /* c2 */ m2 /* c3 */) => ()
225225

226+
f(
227+
// a
228+
// b
229+
// c
230+
)
231+
232+
f(
233+
// a
234+
// b
235+
// c
236+
()
237+
)
238+
239+
let x = {
240+
f(
241+
// a
242+
// b
243+
// c
244+
)
245+
246+
247+
f(
248+
// a
249+
// b
250+
// c
251+
()
252+
)
253+
}
254+
226255
f(()
227256
// c1
228257
=> 1);

0 commit comments

Comments
 (0)