diff --git a/CHANGELOG.md b/CHANGELOG.md index 99c18803c37..0fa6d957e54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/compiler/syntax/src/res_comment.ml b/compiler/syntax/src/res_comment.ml index f03209f8295..ed72613745c 100644 --- a/compiler/syntax/src/res_comment.ml +++ b/compiler/syntax/src/res_comment.ml @@ -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 @@ -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 @@ -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 = { @@ -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 = diff --git a/compiler/syntax/src/res_comment.mli b/compiler/syntax/src/res_comment.mli index ce788ddb6c9..5cc7a446a93 100644 --- a/compiler/syntax/src/res_comment.mli +++ b/compiler/syntax/src/res_comment.mli @@ -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 diff --git a/compiler/syntax/src/res_comments_table.ml b/compiler/syntax/src/res_comments_table.ml index 4e575bf94ff..6591522507e 100644 --- a/compiler/syntax/src/res_comments_table.ml +++ b/compiler/syntax/src/res_comments_table.ml @@ -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. @@ -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 @@ -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 @@ -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 diff --git a/compiler/syntax/src/res_parser.ml b/compiler/syntax/src/res_parser.ml index cd399d8acbd..ddf50f1ccae 100644 --- a/compiler/syntax/src/res_parser.ml +++ b/compiler/syntax/src/res_parser.ml @@ -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 diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 6e9ff38af30..c52fd4a655a 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -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 = @@ -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 = @@ -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 @@ -5502,9 +5531,7 @@ 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 = @@ -5512,7 +5539,8 @@ and print_exp_fun_parameter ~state parameter cmt_tbl = | 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 diff --git a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt index c3f20d18a4d..9f821840637 100644 --- a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt @@ -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 = ( diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index f244d48072c..c74b892f8a5 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -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 diff --git a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt index a0e5607359b..c02f7ebe0b4 100644 --- a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt @@ -95,3 +95,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 diff --git a/tests/syntax_tests/data/printer/comments/namedArgs.res b/tests/syntax_tests/data/printer/comments/namedArgs.res index d1205c08797..e211c6cde77 100644 --- a/tests/syntax_tests/data/printer/comments/namedArgs.res +++ b/tests/syntax_tests/data/printer/comments/namedArgs.res @@ -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 diff --git a/tests/syntax_tests/data/printer/comments/typexpr.res b/tests/syntax_tests/data/printer/comments/typexpr.res index fd27fb3462b..8979259b633 100644 --- a/tests/syntax_tests/data/printer/comments/typexpr.res +++ b/tests/syntax_tests/data/printer/comments/typexpr.res @@ -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