Skip to content

Commit af1eaa8

Browse files
authored
Standardize doc comments printing (#7529)
* Split res.doc attribute with other attributes * WIP * Better fix * Handle edge case * Handle variant printing with comment * Refactor * Update formatted runtimes and tests files * Update CHANGELOG * Fix some edge case * Remove accidentally committed file * Fix tools_tests * Fix List.is_empty not available in older ocaml
1 parent 1db1fc4 commit af1eaa8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+697
-719
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- Improve error message for pipe (`->`) syntax. https://github.com/rescript-lang/rescript/pull/7520
3939
- Improve a few error messages around various subtyping issues. https://github.com/rescript-lang/rescript/pull/7404
4040
- In module declarations, accept the invalid syntax `M = {...}` and format it to `M : {...}`. https://github.com/rescript-lang/rescript/pull/7527
41+
- Improve doc comment formatting to match the style of multiline comments. https://github.com/rescript-lang/rescript/pull/7529
4142

4243
#### :house: Internal
4344

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,23 @@ let filter_printable_attributes attrs = List.filter is_printable_attribute attrs
567567
let partition_printable_attributes attrs =
568568
List.partition is_printable_attribute attrs
569569

570+
let partition_doc_comment_attributes attrs =
571+
List.partition
572+
(fun ((id, payload) : Parsetree.attribute) ->
573+
match (id, payload) with
574+
| ( {txt = "res.doc"},
575+
PStr
576+
[
577+
{
578+
pstr_desc =
579+
Pstr_eval
580+
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
581+
};
582+
] ) ->
583+
true
584+
| _ -> false)
585+
attrs
586+
570587
let is_fun_newtype expr =
571588
match expr.pexp_desc with
572589
| Pexp_fun _ | Pexp_newtype _ -> true

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ val has_printable_attributes : Parsetree.attributes -> bool
9898
val filter_printable_attributes : Parsetree.attributes -> Parsetree.attributes
9999
val partition_printable_attributes :
100100
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
101+
val partition_doc_comment_attributes :
102+
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
101103

102104
val requires_special_callback_printing_last_arg :
103105
(Asttypes.arg_label * Parsetree.expression) list -> bool

compiler/syntax/src/res_printer.ml

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,15 @@ and print_constructor_declarations ~state ~private_flag
15491549

15501550
and print_constructor_declaration2 ~state i
15511551
(cd : Parsetree.constructor_declaration) cmt_tbl =
1552-
let attrs = print_attributes ~state cd.pcd_attributes cmt_tbl in
1552+
let comment_attrs, attrs =
1553+
ParsetreeViewer.partition_doc_comment_attributes cd.pcd_attributes
1554+
in
1555+
let comment_doc =
1556+
match comment_attrs with
1557+
| [] -> Doc.nil
1558+
| comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
1559+
in
1560+
let attrs = print_attributes ~state attrs cmt_tbl in
15531561
let is_dot_dot_dot = cd.pcd_name.txt = "..." in
15541562
let bar =
15551563
if i > 0 || cd.pcd_attributes <> [] || is_dot_dot_dot then Doc.text "| "
@@ -1572,6 +1580,7 @@ and print_constructor_declaration2 ~state i
15721580
Doc.concat
15731581
[
15741582
bar;
1583+
comment_doc;
15751584
Doc.group
15761585
(Doc.concat
15771586
[
@@ -1934,8 +1943,20 @@ and print_typ_expr ?inline_record_definitions ~(state : State.t)
19341943
let doc =
19351944
match typ_expr.ptyp_attributes with
19361945
| _ :: _ as attrs when not should_print_its_own_attributes ->
1937-
Doc.group
1938-
(Doc.concat [print_attributes ~state attrs cmt_tbl; rendered_type])
1946+
let doc_comment_attr, attrs =
1947+
ParsetreeViewer.partition_doc_comment_attributes attrs
1948+
in
1949+
let comment_doc =
1950+
match doc_comment_attr with
1951+
| [] -> Doc.nil
1952+
| _ -> print_doc_comments ~state ~sep:Doc.space cmt_tbl doc_comment_attr
1953+
in
1954+
let attrs_doc =
1955+
match attrs with
1956+
| [] -> Doc.nil
1957+
| _ -> print_attributes ~state attrs cmt_tbl
1958+
in
1959+
Doc.group (Doc.concat [comment_doc; attrs_doc; rendered_type])
19391960
| _ -> rendered_type
19401961
in
19411962
print_comments doc cmt_tbl typ_expr.ptyp_loc
@@ -5568,6 +5589,15 @@ and print_bs_object_row ~state (lbl, expr) cmt_tbl =
55685589
in
55695590
print_comments doc cmt_tbl cmt_loc
55705591

5592+
and print_doc_comments ~state ?(sep = Doc.hard_line) cmt_tbl attrs =
5593+
Doc.concat
5594+
[
5595+
Doc.group
5596+
(Doc.join_with_sep
5597+
(List.map (fun attr -> print_attribute ~state attr cmt_tbl) attrs));
5598+
sep;
5599+
]
5600+
55715601
(* The optional loc indicates whether we need to print the attributes in
55725602
* relation to some location. In practise this means the following:
55735603
* `@attr type t = string` -> on the same line, print on the same line
@@ -5579,6 +5609,9 @@ and print_attributes ?loc ?(inline = false) ~state
55795609
match ParsetreeViewer.filter_parsing_attrs attrs with
55805610
| [] -> Doc.nil
55815611
| attrs ->
5612+
let comment_attrs, attrs =
5613+
ParsetreeViewer.partition_doc_comment_attributes attrs
5614+
in
55825615
let line_break =
55835616
match loc with
55845617
| None -> Doc.line
@@ -5587,15 +5620,30 @@ and print_attributes ?loc ?(inline = false) ~state
55875620
| ({loc = first_loc}, _) :: _
55885621
when loc.loc_start.pos_lnum > first_loc.loc_end.pos_lnum ->
55895622
Doc.hard_line
5590-
| _ -> Doc.line)
5623+
| _ ->
5624+
let has_comment_attrs = not (comment_attrs = []) in
5625+
if has_comment_attrs then Doc.space else Doc.line)
55915626
in
5592-
Doc.concat
5593-
[
5594-
Doc.group
5595-
(Doc.join_with_sep
5596-
(List.map (fun attr -> print_attribute ~state attr cmt_tbl) attrs));
5597-
(if inline then Doc.space else line_break);
5598-
]
5627+
let comment_doc =
5628+
match comment_attrs with
5629+
| [] -> Doc.nil
5630+
| comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
5631+
in
5632+
let attrs_doc =
5633+
match attrs with
5634+
| [] -> Doc.nil
5635+
| _ ->
5636+
Doc.concat
5637+
[
5638+
Doc.group
5639+
(Doc.join_with_sep
5640+
(List.map
5641+
(fun attr -> print_attribute ~state attr cmt_tbl)
5642+
attrs));
5643+
(if inline then Doc.space else line_break);
5644+
]
5645+
in
5646+
Doc.concat [comment_doc; attrs_doc]
55995647

56005648
and print_payload ~state (payload : Parsetree.payload) cmt_tbl =
56015649
match payload with

runtime/Belt_Array.resi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ assertEqual(Belt.Array.reverse([10, 11, 12, 13, 14]), [14, 13, 12, 11, 10])
126126
*/
127127
let reverse: t<'a> => t<'a>
128128

129-
@new
130129
/**
131130
`makeUninitialized(n)` creates an array of length `n` filled with the undefined
132131
value. You must specify the type of data that will eventually fill the array.
@@ -139,9 +138,9 @@ let arr: array<Js.undefined<string>> = Belt.Array.makeUninitialized(5)
139138
assertEqual(Belt.Array.getExn(arr, 0), Js.undefined)
140139
```
141140
*/
141+
@new
142142
external makeUninitialized: int => array<Js.undefined<'a>> = "Array"
143143

144-
@new
145144
/**
146145
**Unsafe**
147146

@@ -157,6 +156,7 @@ Belt.Array.setExn(arr, 0, "example")
157156
assertEqual(Belt.Array.getExn(arr, 0), "example")
158157
```
159158
*/
159+
@new
160160
external makeUninitializedUnsafe: int => t<'a> = "Array"
161161

162162
/**
@@ -335,11 +335,11 @@ assertEqual(Belt.Array.sliceToEnd([10, 11, 12, 13, 14, 15, 16], -4), [13, 14, 15
335335
*/
336336
let sliceToEnd: (t<'a>, int) => t<'a>
337337

338-
@send
339338
/**
340339
`copy(a)` returns a copy of `a`; that is; a fresh array containing the same
341340
elements as `a`.
342341
*/
342+
@send
343343
external copy: (t<'a>, @as(0) _) => t<'a> = "slice"
344344

345345
/**
@@ -770,7 +770,6 @@ assertEqual(Belt.Array.eq([1, 2, 3], [(-1), (-2), (-3)], (a, b) => abs(a) == abs
770770
*/
771771
let eq: (t<'a>, t<'a>, ('a, 'a) => bool) => bool
772772

773-
@set
774773
/**
775774
Unsafe `truncateToLengthUnsafe(xs, n)` sets length of array `xs` to `n`. If `n`
776775
is greater than the length of `xs`; the extra elements are set to `Js.Null_undefined.null`.
@@ -786,6 +785,7 @@ Belt.Array.truncateToLengthUnsafe(arr, 3)
786785
assertEqual(arr, ["ant", "bee", "cat"])
787786
```
788787
*/
788+
@set
789789
external truncateToLengthUnsafe: (t<'a>, int) => unit = "length"
790790

791791
@deprecated("Use `init` instead")

runtime/Belt_Float.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ assertEqual(Belt.Float.fromString("1.0"), Some(1.0))
5757
*/
5858
let fromString: string => option<float>
5959

60-
@val
6160
/**
6261
Converts a given `float` to a `string`. Uses the JavaScript `String` constructor under the hood.
6362

@@ -67,6 +66,7 @@ Converts a given `float` to a `string`. Uses the JavaScript `String` constructor
6766
assertEqual(Belt.Float.toString(1.0), "1")
6867
```
6968
*/
69+
@val
7070
external toString: float => string = "String"
7171

7272
/**

runtime/Belt_List.resi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,6 @@ assertEqual(Belt.List.keep(list{None, Some(2), Some(3), None}, Belt.Option.isSom
790790
*/
791791
let keep: (t<'a>, 'a => bool) => t<'a>
792792

793-
@deprecated("This function will soon be deprecated. Please, use `List.keep` instead.")
794793
/**
795794
Returns a list of all elements in `someList` which satisfy the predicate function `pred`.
796795

@@ -804,6 +803,7 @@ assertEqual(Belt.List.filter(list{1, 2, 3, 4}, isEven), list{2, 4})
804803
assertEqual(Belt.List.filter(list{None, Some(2), Some(3), None}, Belt.Option.isSome), list{Some(2), Some(3)})
805804
```
806805
*/
806+
@deprecated("This function will soon be deprecated. Please, use `List.keep` instead.")
807807
let filter: (t<'a>, 'a => bool) => t<'a>
808808

809809
/** Uncurried version of [keepWithIndex](#keepWithIndex). */
@@ -823,10 +823,6 @@ assertEqual(Belt.List.keepWithIndex(list{1, 2, 3, 4}, (_x, index) => isEven(inde
823823
*/
824824
let keepWithIndex: (t<'a>, ('a, int) => bool) => t<'a>
825825

826-
@deprecated(
827-
"This function will soon be deprecated. Please, use `List.keepWithIndex` \
828-
instead."
829-
)
830826
/**
831827
Returns a list of all elements in `someList` which satisfy the predicate function `pred`.
832828

@@ -838,6 +834,10 @@ let isEven = x => mod(x, 2) == 0
838834
assertEqual(Belt.List.filterWithIndex(list{1, 2, 3, 4}, (_x, index) => isEven(index)), list{1, 3})
839835
```
840836
*/
837+
@deprecated(
838+
"This function will soon be deprecated. Please, use `List.keepWithIndex` \
839+
instead."
840+
)
841841
let filterWithIndex: (t<'a>, ('a, int) => bool) => t<'a>
842842

843843
/** Uncurried version of [keepMap](#keepMap). */

runtime/Js.res

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,17 @@ external undefined: undefined<'a> = "%undefined"
219219
*/
220220
external typeof: 'a => string = "%typeof"
221221

222-
@val @scope("console") /** Equivalent to console.log any value. */
222+
/** Equivalent to console.log any value. */
223+
@val @scope("console")
223224
external log: 'a => unit = "log"
224225

225226
@val @scope("console") external log2: ('a, 'b) => unit = "log"
226227
@val @scope("console") external log3: ('a, 'b, 'c) => unit = "log"
227228

228229
@val @scope("console") external log4: ('a, 'b, 'c, 'd) => unit = "log"
229230

230-
@val @scope("console") @variadic /** A convenience function to console.log more than 4 arguments */
231+
/** A convenience function to console.log more than 4 arguments */
232+
@val @scope("console") @variadic
231233
external logMany: array<'a> => unit = "log"
232234

233235
external eqNull: ('a, null<'a>) => bool = "%equal_null"

0 commit comments

Comments
 (0)