From 716e5e3414bf2ff15e4243780f3203b9bdbcf5c7 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 16 Jul 2025 18:38:45 +0800 Subject: [PATCH 1/5] Remove trailing comma in empty dict and print inside comment --- compiler/syntax/src/res_printer.ml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 3941c57691..50f991af7b 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -1506,7 +1506,7 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = List.filter_map tuple_to_row expressions | _ -> [] in - Doc.breakable_group ~force_break + let doc = Doc.breakable_group ~force_break (Doc.concat [ Doc.indent @@ -1523,9 +1523,9 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = print_comments doc cmt_tbl e.pexp_loc) rows); ]); - Doc.trailing_comma; - Doc.soft_line; - ]) + if List.is_empty rows then Doc.nil else Doc.concat [Doc.trailing_comma; Doc.soft_line]; + ]) + in doc and print_constructor_declarations ~state ~private_flag (cds : Parsetree.constructor_declaration list) cmt_tbl = @@ -4222,12 +4222,13 @@ and print_pexp_apply ~state expr cmt_tbl = args = [(Nolabel, key_values)]; } when Res_parsetree_viewer.is_tuple_array key_values -> - Doc.concat + let doc = Doc.concat [ Doc.text "dict{"; + print_comments_inside cmt_tbl expr.pexp_loc; print_literal_dict_expr ~state key_values cmt_tbl; Doc.rbrace; - ] + ] in doc | Pexp_apply { funct = From 494b3cd6ce80817f3f80e2e62a4e6cb169a5e0a8 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 16 Jul 2025 18:43:03 +0800 Subject: [PATCH 2/5] Remove extra line --- compiler/syntax/src/res_printer.ml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 50f991af7b..7ae5ca6687 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -1506,13 +1506,13 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = List.filter_map tuple_to_row expressions | _ -> [] in - let doc = Doc.breakable_group ~force_break + Doc.breakable_group ~force_break (Doc.concat [ Doc.indent (Doc.concat [ - Doc.soft_line; + (if List.is_empty rows then Doc.nil else Doc.soft_line); Doc.join ~sep:(Doc.concat [Doc.text ","; Doc.line]) (List.map @@ -1523,9 +1523,9 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = print_comments doc cmt_tbl e.pexp_loc) rows); ]); - if List.is_empty rows then Doc.nil else Doc.concat [Doc.trailing_comma; Doc.soft_line]; - ]) - in doc + (if List.is_empty rows then Doc.nil + else Doc.concat [Doc.trailing_comma; Doc.soft_line]); + ]) and print_constructor_declarations ~state ~private_flag (cds : Parsetree.constructor_declaration list) cmt_tbl = @@ -4222,13 +4222,13 @@ and print_pexp_apply ~state expr cmt_tbl = args = [(Nolabel, key_values)]; } when Res_parsetree_viewer.is_tuple_array key_values -> - let doc = Doc.concat + Doc.concat [ Doc.text "dict{"; print_comments_inside cmt_tbl expr.pexp_loc; print_literal_dict_expr ~state key_values cmt_tbl; Doc.rbrace; - ] in doc + ] | Pexp_apply { funct = From 56c2eeab9c87806ffacfd5e7787384144f810ac1 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 16 Jul 2025 19:24:30 +0800 Subject: [PATCH 3/5] Add test --- tests/syntax_tests/data/printer/expr/dict.res | 6 ++++++ tests/syntax_tests/data/printer/expr/expected/dict.res.txt | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/tests/syntax_tests/data/printer/expr/dict.res b/tests/syntax_tests/data/printer/expr/dict.res index 12984008db..0a05246e98 100644 --- a/tests/syntax_tests/data/printer/expr/dict.res +++ b/tests/syntax_tests/data/printer/expr/dict.res @@ -1,6 +1,12 @@ // empty dict let x = dict{} +// empty dict with inside comment +let x = dict{ + // inside comment + +} + // one value let x = dict{"foo": "bar"} diff --git a/tests/syntax_tests/data/printer/expr/expected/dict.res.txt b/tests/syntax_tests/data/printer/expr/expected/dict.res.txt index 25cfcb6ca5..c93760b4d2 100644 --- a/tests/syntax_tests/data/printer/expr/expected/dict.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/dict.res.txt @@ -1,6 +1,11 @@ // empty dict let x = dict{} +// empty dict with inside comment +let x = dict{ + // inside comment +} + // one value let x = dict{"foo": "bar"} From 3f37fffd94b2c1d87b7ccd94f242629a70e1955b Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 16 Jul 2025 19:30:03 +0800 Subject: [PATCH 4/5] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc49f2fe0..37bbaf41b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - Fix `typeof` parens on functions. https://github.com/rescript-lang/rescript/pull/7643 - Rewatch: Add --dev flag to clean command. https://github.com/rescript-lang/rescript/pull/7622 - Rewatch: Use root package suffix in clean log messages. https://github.com/rescript-lang/rescript/pull/7648 +- Fix inside comment printing for empty dict. https://github.com/rescript-lang/rescript/pull/7654 # 12.0.0-beta.1 From 9880aed3f662eb880dd8bf2a58e9ea081ca8b37c Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 16 Jul 2025 19:48:31 +0800 Subject: [PATCH 5/5] Don't use List.is_empty --- compiler/syntax/src/res_printer.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 7ae5ca6687..17dd46f102 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -1512,7 +1512,7 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = Doc.indent (Doc.concat [ - (if List.is_empty rows then Doc.nil else Doc.soft_line); + (if rows = [] then Doc.nil else Doc.soft_line); Doc.join ~sep:(Doc.concat [Doc.text ","; Doc.line]) (List.map @@ -1523,7 +1523,7 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = print_comments doc cmt_tbl e.pexp_loc) rows); ]); - (if List.is_empty rows then Doc.nil + (if rows = [] then Doc.nil else Doc.concat [Doc.trailing_comma; Doc.soft_line]); ])