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

Fix printing doc comments before attributes #642

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#### :bug: Bug Fix

- Fix pretty printer where it would print doc comments on the same line as other attributes.
- 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
- Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629
- Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654
Expand Down
9 changes: 9 additions & 0 deletions src/res_doc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ let join ~sep docs =
in
concat (loop [] sep docs)

let joinWithSep docsWithSep =
let rec loop acc docs =
match docs with
| [] -> List.rev acc
| [(x, _sep)] -> List.rev (x :: acc)
| (x, sep) :: xs -> loop (sep :: x :: acc) xs
in
concat (loop [] docsWithSep)

let fits w stack =
let width = ref w in
let result = ref None in
Expand Down
3 changes: 3 additions & 0 deletions src/res_doc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ val customLayout : t list -> t
val breakParent : t
val join : sep:t -> t list -> t

(* [(doc1, sep1); (doc2,sep2)] joins as doc1 sep1 doc2 *)
val joinWithSep : (t * t) list -> t

val space : t
val comma : t
val dot : t
Expand Down
34 changes: 18 additions & 16 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ and printStructureItem ~customLayout (si : Parsetree.structure_item) cmtTbl =
in
Doc.concat [printAttributes ~customLayout attrs cmtTbl; exprDoc]
| Pstr_attribute attr ->
printAttribute ~customLayout ~standalone:true attr cmtTbl
fst (printAttribute ~customLayout ~standalone:true attr cmtTbl)
| Pstr_extension (extension, attrs) ->
Doc.concat
[
Expand Down Expand Up @@ -940,7 +940,7 @@ and printSignatureItem ~customLayout (si : Parsetree.signature_item) cmtTbl =
| Psig_include includeDescription ->
printIncludeDescription ~customLayout includeDescription cmtTbl
| Psig_attribute attr ->
printAttribute ~customLayout ~standalone:true attr cmtTbl
fst (printAttribute ~customLayout ~standalone:true attr cmtTbl)
| Psig_extension (extension, attrs) ->
Doc.concat
[
Expand Down Expand Up @@ -5033,7 +5033,7 @@ and printAttributes ?loc ?(inline = false) ~customLayout
Doc.concat
[
Doc.group
(Doc.join ~sep:Doc.line
(Doc.joinWithSep
(List.map
(fun attr -> printAttribute ~customLayout attr cmtTbl)
attrs));
Expand Down Expand Up @@ -5134,20 +5134,22 @@ and printAttribute ?(standalone = false) ~customLayout
Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string (txt, _))}, _);
};
] ) ->
Doc.concat
[
Doc.text (if standalone then "/***" else "/**");
Doc.text txt;
Doc.text "*/";
]
( Doc.concat
[
Doc.text (if standalone then "/***" else "/**");
Doc.text txt;
Doc.text "*/";
],
Doc.hardLine )
| _ ->
Doc.group
(Doc.concat
[
Doc.text (if standalone then "@@" else "@");
Doc.text (convertBsExternalAttribute id.txt);
printPayload ~customLayout payload cmtTbl;
])
( Doc.group
(Doc.concat
[
Doc.text (if standalone then "@@" else "@");
Doc.text (convertBsExternalAttribute id.txt);
printPayload ~customLayout payload cmtTbl;
]),
Doc.line )

and printModExpr ~customLayout modExpr cmtTbl =
let doc =
Expand Down
14 changes: 13 additions & 1 deletion tests/printer/comments/docComments.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ let q = 11
* is a multi-line
multiline doc comment
*/
type h = int
type h = int

/* comment and attribute */
@foo let x = 10

/** doc comment and attribute */
@foo let x = 10

/** doc comment and 3 attributes */
@foo @bar @baz let x = 10

/** doc comment and 0 attributes */
let x = 10
16 changes: 16 additions & 0 deletions tests/printer/comments/expected/docComments.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ let q = 11
multiline doc comment
*/
type h = int

/* comment and attribute */
@foo let x = 10

/** doc comment and attribute */
@foo
let x = 10

/** doc comment and 3 attributes */
@foo
@bar
@baz
let x = 10

/** doc comment and 0 attributes */
let x = 10