Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 19c7071

Browse files
authored
Unify comment formatting inside empty blocks (#647)
* keep comments inside the empty record * refactor printcommentsinside * tweak * fix jsx case * test changes * fix modtype format * dont closing tag if comments inside * clean * clean and comments * clean * update CHANGELOG * fix review comments * add more tests * clean * clean * remove duplicate test * remove isNil * use find_opt instead
1 parent 3d25038 commit 19c7071

File tree

11 files changed

+252
-157
lines changed

11 files changed

+252
-157
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Fix location issue in error messages with JSX V4 where the body of the component is an application https://github.com/rescript-lang/syntax/pull/633
2828
- Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629
2929
- Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654
30+
- Fix printing of comments inside empty blocks https://github.com/rescript-lang/syntax/pull/647
3031
- Fix location issue in error messages with JSX V4 where the multiple props types are defined https://github.com/rescript-lang/syntax/pull/655
3132

3233
## ReScript 10.0

src/res_comments_table.ml

+25-19
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,11 @@ and walkTypeDeclaration (td : Parsetree.type_declaration) t comments =
692692
| Ptype_abstract | Ptype_open -> rest
693693
| Ptype_record labelDeclarations ->
694694
let () =
695-
walkList
696-
(labelDeclarations |> List.map (fun ld -> LabelDeclaration ld))
697-
t rest
695+
if labelDeclarations = [] then attach t.inside td.ptype_loc rest
696+
else
697+
walkList
698+
(labelDeclarations |> List.map (fun ld -> LabelDeclaration ld))
699+
t rest
698700
in
699701
[]
700702
| Ptype_variant constructorDeclarations ->
@@ -1023,22 +1025,26 @@ and walkExpression expr t comments =
10231025
| Pexp_array exprs | Pexp_tuple exprs ->
10241026
walkList (exprs |> List.map (fun e -> Expression e)) t comments
10251027
| Pexp_record (rows, spreadExpr) ->
1026-
let comments =
1027-
match spreadExpr with
1028-
| None -> comments
1029-
| Some expr ->
1030-
let leading, inside, trailing = partitionByLoc comments expr.pexp_loc in
1031-
attach t.leading expr.pexp_loc leading;
1032-
walkExpression expr t inside;
1033-
let afterExpr, rest =
1034-
partitionAdjacentTrailing expr.pexp_loc trailing
1035-
in
1036-
attach t.trailing expr.pexp_loc afterExpr;
1037-
rest
1038-
in
1039-
walkList
1040-
(rows |> List.map (fun (li, e) -> ExprRecordRow (li, e)))
1041-
t comments
1028+
if rows = [] then attach t.inside expr.pexp_loc comments
1029+
else
1030+
let comments =
1031+
match spreadExpr with
1032+
| None -> comments
1033+
| Some expr ->
1034+
let leading, inside, trailing =
1035+
partitionByLoc comments expr.pexp_loc
1036+
in
1037+
attach t.leading expr.pexp_loc leading;
1038+
walkExpression expr t inside;
1039+
let afterExpr, rest =
1040+
partitionAdjacentTrailing expr.pexp_loc trailing
1041+
in
1042+
attach t.trailing expr.pexp_loc afterExpr;
1043+
rest
1044+
in
1045+
walkList
1046+
(rows |> List.map (fun (li, e) -> ExprRecordRow (li, e)))
1047+
t comments
10421048
| Pexp_field (expr, longident) ->
10431049
let leading, inside, trailing = partitionByLoc comments expr.pexp_loc in
10441050
let trailing =

0 commit comments

Comments
 (0)