Skip to content
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 @@ -21,6 +21,7 @@
#### :bug: Bug fix

- Make rewatch compile independent modules after an unrelated failure and recompile blocked dependents when a changed interface survives a failed implementation, including across full watcher rebuilds. https://github.com/rescript-lang/rescript/pull/8667
- Keep comments on labelled parameters with default values, and before `=?` in optional arrow-type arguments, when formatting. https://github.com/rescript-lang/rescript/pull/8690
- Fix invalid JavaScript that exported names left unbound when a module's toplevel always throws. https://github.com/rescript-lang/rescript/pull/8692
- Make module inclusion error messages independent of the length of the source file path. https://github.com/rescript-lang/rescript/pull/8691

Expand Down
14 changes: 13 additions & 1 deletion compiler/syntax/src/res_comment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type t = {
style: style;
loc: Location.t;
mutable prev_tok_end_pos: Lexing.position;
mutable prev_tok_is_question: bool;
}

let loc t = t.loc
Expand All @@ -20,6 +21,10 @@ let prev_tok_end_pos t = t.prev_tok_end_pos

let set_prev_tok_end_pos t pos = t.prev_tok_end_pos <- pos

let prev_tok_is_question t = t.prev_tok_is_question

let set_prev_tok_is_question t b = t.prev_tok_is_question <- b

let is_single_line_comment t = t.style = SingleLine

let is_doc_comment t = t.style = DocComment
Expand All @@ -35,7 +40,13 @@ let to_string t =
(loc_end.pos_cnum - loc_end.pos_bol)

let make_single_line_comment ~loc txt =
{txt; loc; style = SingleLine; prev_tok_end_pos = Lexing.dummy_pos}
{
txt;
loc;
style = SingleLine;
prev_tok_end_pos = Lexing.dummy_pos;
prev_tok_is_question = false;
}

let make_multi_line_comment ~loc ~doc_comment ~standalone txt =
{
Expand All @@ -45,6 +56,7 @@ let make_multi_line_comment ~loc ~doc_comment ~standalone txt =
(if doc_comment then if standalone then ModuleComment else DocComment
else MultiLine);
prev_tok_end_pos = Lexing.dummy_pos;
prev_tok_is_question = false;
}

let trim_spaces s =
Expand Down
6 changes: 6 additions & 0 deletions compiler/syntax/src/res_comment.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ val prev_tok_end_pos : t -> Lexing.position

val set_prev_tok_end_pos : t -> Lexing.position -> unit

(* Whether the token before the comment is [?], e.g. the end of the [=?] of an
optional parameter. *)
val prev_tok_is_question : t -> bool

val set_prev_tok_is_question : t -> bool -> unit

val is_doc_comment : t -> bool

val is_module_comment : t -> bool
Expand Down
28 changes: 24 additions & 4 deletions compiler/syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ let partition_adjacent_trailing loc1 comments =
in
loop ~prev_end_pos:loc1.loc_end [] comments

(* Like [partition_adjacent_trailing], and also keeps the comments that follow
* a [?] token placed right after [loc1], as in the [=?] of an optional
* parameter without a default:
* (~x /* before */ =? /* after */, ~y)
* Both comments trail the parameter [~x]. *)
let partition_adjacent_trailing_through_question loc1 comments =
let after_loc1, rest = partition_adjacent_trailing loc1 comments in
match rest with
| first :: _
when Comment.prev_tok_is_question first
&& (Comment.prev_tok_end_pos first).pos_cnum
> loc1.Location.loc_end.pos_cnum ->
let after_question, rest =
partition_adjacent_trailing
{loc1 with loc_end = Comment.prev_tok_end_pos first}
rest
in
(after_loc1 @ after_question, rest)
| _ -> (after_loc1, rest)

(* Splits comments that follow a location but come before another token.
* This is particularly useful for handling comments between two tokens
* where traditional leading/trailing partitioning isn't precise enough.
Expand Down Expand Up @@ -712,7 +732,7 @@ and visit_list_but_continue_with_remaining_comments :
| Some loc ->
let after_prev, rest =
if newline_delimited then partition_by_on_same_line loc comments
else partition_adjacent_trailing loc comments
else partition_adjacent_trailing_through_question loc comments
in
attach t.trailing loc after_prev;
rest
Expand All @@ -730,7 +750,7 @@ and visit_list_but_continue_with_remaining_comments :
(* Same line *)
if prev_loc.loc_end.pos_lnum == curr_loc.loc_start.pos_lnum then
let after_prev, before_curr =
partition_adjacent_trailing prev_loc leading
partition_adjacent_trailing_through_question prev_loc leading
in
let () = attach t.trailing prev_loc after_prev in
let () = attach t.leading curr_loc before_curr in
Expand Down Expand Up @@ -1806,10 +1826,10 @@ and walk_expr_parameter (_attrs, _argLbl, expr_opt, pattern) t comments =
walk_pattern pattern t inside;
match expr_opt with
| Some expr ->
let _afterPat, rest =
let after_pat, rest =
partition_adjacent_trailing pattern.ppat_loc trailing
in
attach t.trailing pattern.ppat_loc trailing;
attach t.trailing pattern.ppat_loc after_pat;
if is_block_expr expr then walk_expression expr t rest
else
let leading, inside, trailing = partition_by_loc rest expr.pexp_loc in
Expand Down
6 changes: 5 additions & 1 deletion compiler/syntax/src/res_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ let rec read cache =
match token with
| Comment c when not (Comment.is_doc_comment c || Comment.is_module_comment c)
->
let _, preceding_end, _ = cache.scanned in
let _, preceding_end, preceding_token = cache.scanned in
Comment.set_prev_tok_end_pos c preceding_end;
Comment.set_prev_tok_is_question c
(match preceding_token with
| Token.Question -> true
| _ -> false);
cache.comments <- c :: cache.comments;
cache.scanned <- scanned;
read cache
Expand Down
70 changes: 49 additions & 21 deletions compiler/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,28 @@ let print_comments doc (tbl : Comment_table.t) loc =
let doc_with_leading_comments = print_leading_comments doc tbl.leading loc in
print_trailing_comments doc_with_leading_comments tbl.trailing loc

(* Prints [doc] with the comments of [loc], followed by [marker], a token that
the source writes right after [loc] (such as [=?]). The trailing comments
adjacent to [loc] precede the marker in the source and print before it; the
remaining trailing comments print after it. *)
let print_comments_before_marker doc (tbl : Comment_table.t) loc marker =
let after_marker =
match Hashtbl.find_opt tbl.trailing loc with
| None -> []
| Some comments ->
let before_marker, after_marker =
Comment_table.partition_adjacent_trailing loc comments
in
Hashtbl.replace tbl.trailing loc before_marker;
after_marker
in
let doc = Doc.concat [print_comments doc tbl loc; marker] in
match after_marker with
| [] -> doc
| _ ->
Hashtbl.replace tbl.trailing loc after_marker;
print_trailing_comments doc tbl.trailing loc

let is_empty_doc doc = doc = Doc.nil

let print_list ~get_loc ~nodes ~print ?(force_break = false) t =
Expand Down Expand Up @@ -2355,10 +2377,10 @@ and print_type_parameter ?inline_record_definitions ~state {attrs; lbl; typ}
attrs;
label;
print_typ_expr ?inline_record_definitions ~state typ cmt_tbl;
optional_indicator;
])
in
print_comments doc cmt_tbl loc
(* [loc] ends before [=?]. *)
print_comments_before_marker doc cmt_tbl loc optional_indicator

and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
i =
Expand Down Expand Up @@ -5465,27 +5487,34 @@ and print_exp_fun_parameter ~state parameter cmt_tbl =
{ppat_desc = Ppat_var string_loc; ppat_attributes} )
when lbl = string_loc.txt ->
(* ~d *)
Doc.concat
[
print_attributes ~state ppat_attributes cmt_tbl;
Doc.text "~";
print_ident_like lbl;
]
print_comments
(Doc.concat
[
print_attributes ~state ppat_attributes cmt_tbl;
Doc.text "~";
print_ident_like lbl;
])
cmt_tbl pattern.ppat_loc
| ( (Labelled {txt = lbl} | Optional {txt = lbl}),
{
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
ppat_desc =
Ppat_constraint
(({ppat_desc = Ppat_var {txt}} as var_pattern), typ);
ppat_attributes;
} )
when lbl = txt ->
(* ~d: e *)
Doc.concat
[
print_attributes ~state ppat_attributes cmt_tbl;
Doc.text "~";
print_ident_like lbl;
Doc.text ": ";
print_typ_expr ~state typ cmt_tbl;
]
print_comments
(Doc.concat
[
print_attributes ~state ppat_attributes cmt_tbl;
print_comments
(Doc.concat [Doc.text "~"; print_ident_like lbl])
cmt_tbl var_pattern.ppat_loc;
Doc.text ": ";
print_typ_expr ~state typ cmt_tbl;
])
cmt_tbl pattern.ppat_loc
| (Labelled {txt = lbl} | Optional {txt = lbl}), pattern ->
(* ~b as c *)
Doc.concat
Expand All @@ -5502,17 +5531,16 @@ and print_exp_fun_parameter ~state parameter cmt_tbl =
| _ -> Doc.nil
in
let doc =
Doc.group
(Doc.concat
[attrs; label_with_pattern; default_expr_doc; optional_label_suffix])
Doc.group (Doc.concat [attrs; label_with_pattern; default_expr_doc])
in
let lbl_loc = Asttypes.get_lbl_loc lbl in
let cmt_loc =
match default_expr with
| None -> {lbl_loc with loc_end = pattern.ppat_loc.loc_end}
| Some expr -> {lbl_loc with loc_end = expr.pexp_loc.loc_end}
in
print_comments doc cmt_tbl cmt_loc
(* Without a default, [cmt_loc] ends before [=?]. *)
print_comments_before_marker doc cmt_tbl cmt_loc optional_label_suffix

and print_expression_block ~state ~braces expr cmt_tbl =
let expr = Parsetree_viewer.unwrap_braces expr in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ let multiply = (~x=/* c0 */ m1 /* c1 */, ~y=/* c2 */ m2 /* c3 */) => ()
let f = (
/* c0 */ ~greeting /* c1 */,
/* c2 */ ~from as /* c3 */ /* c4 */ hometown /* c5 */,
/* c6 */ ~x=?,
) => /* c7 */ ()
/* c6 */ ~x=? /* c7 */,
) => ()

let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => ()
let multiply = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,26 @@ let make = (
) => {
()
}

let f = (~x as y /* a */=/* b */ 1, ()) => y
let f = (~x /* a */=/* b */ 1, ()) => x
let f = (~x: int /* a */=1, ()) => x
let f = (~x /* a */: int /* b */=1, ()) => x
let f = (~x /* a */: int, ()) => x
let f = (~x /* a */=?, ()) => x
let f = (~x: int /* a */=?, ()) => x
let f = (~x as y /* a */=?, ()) => y
let f = (~x=? /* a */, ~y) => x
let f = (~x: int=? /* a */, ~y) => x
let f = (~x=? /* a */) => x
let f = (~x /* a */=? /* b */, ~y) => x
let f = (~x /* a */ /* a2 */=? /* b */ /* b2 */, ~y) => x
let f = (~x as y=? /* a */, ~z) => y
let f = (~x=?, /* a */ ~y) => x
let f = (~x=? /* a */, ~y) => x
let f = (
~x: int /* a */=? /* b */,
~someLongParameterName,
~anotherLongParameterName,
~yetAnotherLongParameterName,
) => x
14 changes: 14 additions & 0 deletions tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ external another_test: (
// comment 2
int,
) => unit = "test"
type optionalArg = (~x: int /* a */=?, unit) => int

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add this formatter fix to the changelog

This is a user-facing formatter bug fix, but the commit does not update CHANGELOG.md; add an entry under the current unreleased bug-fix section and end it with the PR link as required.

AGENTS.md reference: AGENTS.md:L93-L95

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in bfcc089, under the Unreleased section with the PR link.

type lastOptionalArg = (~x: int /* a */=?) => int
external optionalArgExternal: (~x: int /* a */=?, unit) => int = "f"
type optionalArgAfter = (~x: int=? /* a */, ~y: int) => int
type lastOptionalArgAfter = (~x: int=? /* a */) => int
type optionalArgAround = (~x: int /* a */ /* a2 */=? /* b */, ~y: int) => int
type optionalArgThenComma = (~x: int=?, /* a */ ~y: int) => int
external optionalArgAfterExternal: (~x: int=? /* a */, unit) => int = "f"
type optionalArgAfterMultiline = (
~x: int=? /* a */,
~someLongParameterName: int,
~anotherLongParameterName: int,
~yetAnotherLongParameterName: int,
) => int
26 changes: 26 additions & 0 deletions tests/syntax_tests/data/printer/comments/namedArgs.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,29 @@ let make = (
) => {
()
}

let f = (~x as y /* a */ = /* b */ 1, ()) => y
let f = (~x /* a */ = /* b */ 1, ()) => x
let f = (~x: int /* a */ = 1, ()) => x
let f = (~x /* a */ : int /* b */ = 1, ()) => x
let f = (~x /* a */ : int, ()) => x
let f = (~x /* a */ = ?, ()) => x
let f = (~x: int /* a */ = ?, ()) => x
let f = (~x as y /* a */ = ?, ()) => y
let f = (~x=? /* a */, ~y) => x
let f = (~x: int=? /* a */, ~y) => x
let f = (~x=? /* a */) => x
let f = (~x /* a */ =? /* b */, ~y) => x
let f = (~x /* a */ /* a2 */ =? /* b */ /* b2 */, ~y) => x
let f = (~x as y=? /* a */, ~z) => y
let f = (~x=?, /* a */ ~y) => x
let f = (
~x=? /* a */,
~y,
) => x
let f = (
~x: int /* a */ =? /* b */,
~someLongParameterName,
~anotherLongParameterName,
~yetAnotherLongParameterName,
) => x
14 changes: 14 additions & 0 deletions tests/syntax_tests/data/printer/comments/typexpr.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ external another_test: (
// comment 2
int,
) => unit = "test"
type optionalArg = (~x: int /* a */ =?, unit) => int
type lastOptionalArg = (~x: int /* a */ =?) => int
external optionalArgExternal: (~x: int /* a */ =?, unit) => int = "f"
type optionalArgAfter = (~x: int=? /* a */, ~y: int) => int
type lastOptionalArgAfter = (~x: int=? /* a */) => int
type optionalArgAround = (~x: int /* a */ /* a2 */ =? /* b */, ~y: int) => int
type optionalArgThenComma = (~x: int=?, /* a */ ~y: int) => int
external optionalArgAfterExternal: (~x: int=? /* a */, unit) => int = "f"
type optionalArgAfterMultiline = (
~x: int=? /* a */,
~someLongParameterName: int,
~anotherLongParameterName: int,
~yetAnotherLongParameterName: int,
) => int
Loading