Skip to content
Open
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 @@ -38,6 +38,7 @@
- Rewatch: plain output when not running in tty. https://github.com/rescript-lang/rescript/pull/7970
- Streamline rewatch help texts. https://github.com/rescript-lang/rescript/pull/7973
- Rewatch: Reduced build progress output from 7 steps to 3 for cleaner, less verbose logging. https://github.com/rescript-lang/rescript/pull/7971
- Formatter: Improve multiline printing of record types and values. https://github.com/rescript-lang/rescript/pull/7993

#### :house: Internal

Expand Down
33 changes: 25 additions & 8 deletions compiler/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ and print_type_declaration ~state ~name ~equal_sign ~rec_flag i
manifest;
Doc.concat [Doc.space; Doc.text equal_sign; Doc.space];
print_private_flag td.ptype_private;
print_record_declaration ~state lds cmt_tbl;
print_record_declaration ~record_loc:td.ptype_loc ~state lds cmt_tbl;
]
| Ptype_variant cds ->
let manifest =
Expand Down Expand Up @@ -1370,8 +1370,8 @@ and print_type_declaration2 ?inline_record_definitions ~state ~rec_flag
manifest;
Doc.concat [Doc.space; Doc.text equal_sign; Doc.space];
print_private_flag td.ptype_private;
print_record_declaration ?inline_record_definitions ~state lds
cmt_tbl;
print_record_declaration ?inline_record_definitions
~record_loc:td.ptype_loc ~state lds cmt_tbl;
]
| Ptype_variant cds ->
let manifest =
Expand Down Expand Up @@ -1465,12 +1465,22 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance)
Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl]

and print_record_declaration ?check_break_from_loc ?inline_record_definitions
~state (lds : Parsetree.label_declaration list) cmt_tbl =
?record_loc ~state (lds : Parsetree.label_declaration list) cmt_tbl =
let get_field_start_line (ld : Parsetree.label_declaration) =
(* For spread fields (...), use the type location instead of pld_loc
because pld_loc may incorrectly include preceding whitespace *)
if ld.pld_name.txt = "..." then ld.pld_type.ptyp_loc.loc_start.pos_lnum
else ld.pld_loc.loc_start.pos_lnum
in
let force_break =
match (check_break_from_loc, lds, List.rev lds) with
match (check_break_from_loc, record_loc, lds) with
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum
| _, first :: _, last :: _ ->
first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum
| None, Some loc, first :: _ ->
(* Check if first field is on a different line than the opening brace *)
loc.loc_start.pos_lnum < get_field_start_line first
| None, None, first :: _ ->
let last = List.hd (List.rev lds) in
get_field_start_line first < last.pld_loc.loc_end.pos_lnum
| _, _, _ -> false
in
Doc.breakable_group ~force_break
Expand Down Expand Up @@ -3223,7 +3233,14 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
* b: 2,
* }` -> record is written on multiple lines, break the group *)
let force_break =
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
match (spread_expr, rows) with
Copy link
Member

Choose a reason for hiding this comment

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

I want to pause here a bit — I don’t think we should merge this part right now.

The change around force_break introduces behaviour that depends on how the user originally formatted their code. That’s a significant precedent: it ties output formatting decisions to source‑layout heuristics.

If we want to allow this kind of stylistic inference, I believe it needs wider agreement within the core team. We should either decide as a group that this is a direction we’re comfortable with, or explicitly document why we don’t want to go that way.

If we do agree in principle, I’d also like to see a short architectural or design record capturing the rationale and boundaries for this sort of logic, so that future contributions have something concrete to refer to.

Finally, even with buy‑in, I’d suggest postponing this until after v12. We’re in the release‑candidate stage, and introducing new formatting heuristics at this point could have more ripple effects than we realise.

So in short: let’s not merge this specific part yet; let’s first get consensus and document it properly.

| Some expr, _ ->
(* If there's a spread, compare with spread expression's location *)
e.pexp_loc.loc_start.pos_lnum < expr.pexp_loc.loc_start.pos_lnum
| None, first_row :: _ ->
(* Otherwise, compare with the first row's location *)
e.pexp_loc.loc_start.pos_lnum < first_row.lid.loc.loc_start.pos_lnum
| None, [] -> false
in
let punning_allowed =
match (spread_expr, rows) with
Expand Down
14 changes: 2 additions & 12 deletions tests/syntax_tests/data/ast-mapping/expected/JSXElements.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ let emptyUnary = ReactDOM.jsx("input", {})

let emptyNonunary = ReactDOM.jsx("div", {})

let emptyUnaryWithAttributes = ReactDOM.jsx(
"input",
{
type_: "text",
},
)
let emptyUnaryWithAttributes = ReactDOM.jsx("input", {type_: "text"})

let emptyNonunaryWithAttributes = ReactDOM.jsx(
"div",
{
className: "container",
},
)
let emptyNonunaryWithAttributes = ReactDOM.jsx("div", {className: "container"})

let elementWithChildren = ReactDOM.jsxs(
"div",
Expand Down
35 changes: 28 additions & 7 deletions tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

module C0 = {
@res.jsxComponentProps
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
type props<'priority, 'text> = {
priority: 'priority,
text?: 'text,
}

let make = ({priority: _, text: ?__text, _}: props<_, _>) => {
let text = switch __text {
Expand All @@ -20,7 +23,10 @@ module C0 = {

module C1 = {
@res.jsxComponentProps
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
type props<'priority, 'text> = {
priority: 'priority,
text?: 'text,
}

let make = ({priority: p, text: ?__text, _}: props<_, _>) => {
let text = switch __text {
Expand All @@ -38,7 +44,9 @@ module C1 = {

module C2 = {
@res.jsxComponentProps
type props<'foo> = {foo?: 'foo}
type props<'foo> = {
foo?: 'foo,
}

let make = ({foo: ?__bar, _}: props<_>) => {
let bar = switch __bar {
Expand All @@ -56,7 +64,11 @@ module C2 = {

module C3 = {
@res.jsxComponentProps
type props<'foo, 'a, 'b> = {foo?: 'foo, a?: 'a, b: 'b}
type props<'foo, 'a, 'b> = {
foo?: 'foo,
a?: 'a,
b: 'b,
}

let make = ({foo: ?__bar, a: ?__a, b, _}: props<_, _, _>) => {
let bar = switch __bar {
Expand All @@ -82,7 +94,10 @@ module C3 = {

module C4 = {
@res.jsxComponentProps
type props<'a, 'x> = {a: 'a, x?: 'x}
type props<'a, 'x> = {
a: 'a,
x?: 'x,
}

let make = ({a: b, x: ?__x, _}: props<_, _>) => {
let x = switch __x {
Expand All @@ -100,7 +115,10 @@ module C4 = {

module C5 = {
@res.jsxComponentProps
type props<'a, 'z> = {a: 'a, z?: 'z}
type props<'a, 'z> = {
a: 'a,
z?: 'z,
}

let make = ({a: (x, y), z: ?__z, _}: props<_, _>) => {
let z = switch __z {
Expand All @@ -124,7 +142,10 @@ module C6 = {
let make: React.component<props>
}
@res.jsxComponentProps
type props<'comp, 'x> = {comp: 'comp, x: 'x}
type props<'comp, 'x> = {
comp: 'comp,
x: 'x,
}

let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>): React.element =>
React.jsx(Comp.make, {})
Expand Down
8 changes: 6 additions & 2 deletions tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ let f = a => Js.Promise.resolve(a + a)

module C0 = {
@res.jsxComponentProps
type props<'a> = {a: 'a}
type props<'a> = {
a: 'a,
}

let make = async ({a, _}: props<_>) => {
let a = await f(a)
Expand All @@ -17,7 +19,9 @@ module C0 = {

module C1 = {
@res.jsxComponentProps
type props<'status> = {status: 'status}
type props<'status> = {
status: 'status,
}

let make = async ({status, _}: props<_>): React.element => {
switch status {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@res.jsxComponentProps
type props<'msg> = {msg: 'msg} // test React JSX file
type props<'msg> = {
msg: 'msg, // test React JSX file
}

let make = ({msg, _}: props<_>): React.element => {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module C0 = {
@res.jsxComponentProps
type props<'a, 'b> = {a?: 'a, b?: 'b}
type props<'a, 'b> = {
a?: 'a,
b?: 'b,
}
let make = ({a: ?__a, b: ?__b, _}: props<_, _>) => {
let a = switch __a {
| Some(a) => a
Expand All @@ -20,7 +23,10 @@ module C0 = {

module C1 = {
@res.jsxComponentProps
type props<'a, 'b> = {a?: 'a, b: 'b}
type props<'a, 'b> = {
a?: 'a,
b: 'b,
}

let make = ({a: ?__a, b, _}: props<_, _>) => {
let a = switch __a {
Expand All @@ -39,7 +45,9 @@ module C1 = {
module C2 = {
let a = "foo"
@res.jsxComponentProps
type props<'a> = {a?: 'a}
type props<'a> = {
a?: 'a,
}

let make = ({a: ?__a, _}: props<_>) => {
let a = switch __a {
Expand All @@ -57,7 +65,9 @@ module C2 = {

module C3 = {
@res.jsxComponentProps
type props<'disabled> = {disabled?: 'disabled}
type props<'disabled> = {
disabled?: 'disabled,
}

let make = ({disabled: ?__everythingDisabled, _}: props<bool>) => {
let everythingDisabled = switch __everythingDisabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

module Foo = {
@res.jsxComponentProps @live
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@module("Foo")
external component: React.component<props<int, string>> = "component"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

module V4A = {
@res.jsxComponentProps
type props<'msg> = {msg: 'msg}
type props<'msg> = {
msg: 'msg,
}

let make = ({msg, _}: props<_>): React.element => {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
Expand Down
16 changes: 12 additions & 4 deletions tests/syntax_tests/data/ppx/react/expected/forwardRef.resi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module V4C: {

module ForwardRef: {
@res.jsxComponentProps
type props<'ref> = {ref?: 'ref}
type props<'ref> = {
ref?: 'ref,
}

let make: React.component<props<React.ref<Js.Nullable.t<inputRef>>>>
}
Expand All @@ -34,7 +36,9 @@ module V4CUncurried: {

module ForwardRef: {
@res.jsxComponentProps
type props<'ref> = {ref?: 'ref}
type props<'ref> = {
ref?: 'ref,
}

let make: React.component<props<React.ref<Js.Nullable.t<inputRef>>>>
}
Expand All @@ -56,7 +60,9 @@ module V4A: {

module ForwardRef: {
@res.jsxComponentProps
type props<'ref> = {ref?: 'ref}
type props<'ref> = {
ref?: 'ref,
}

let make: React.component<props<React.ref<Js.Nullable.t<inputRef>>>>
}
Expand All @@ -76,7 +82,9 @@ module V4AUncurried: {

module ForwardRef: {
@res.jsxComponentProps
type props<'ref> = {ref?: 'ref}
type props<'ref> = {
ref?: 'ref,
}

let make: React.component<props<React.ref<Js.Nullable.t<inputRef>>>>
}
Expand Down
4 changes: 3 additions & 1 deletion tests/syntax_tests/data/ppx/react/expected/interface.res.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module A = {
@res.jsxComponentProps
type props<'x> = {x: 'x}
type props<'x> = {
x: 'x,
}
let make = ({x, _}: props<_>): React.element => React.string(x)
let make = {
let \"Interface$A" = (props: props<_>) => make(props)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module A: {
@res.jsxComponentProps
type props<'x> = {x: 'x}
type props<'x> = {
x: 'x,
}
let make: React.component<props<string>>
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@res.jsxComponentProps type props<'x, 'ref> = {x: 'x, ref?: 'ref}
@res.jsxComponentProps
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}
let make = (
{x, _}: props<string, ReactDOM.Ref.currentDomRef>,
ref: Js.Nullable.t<ReactDOM.Ref.currentDomRef>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
@res.jsxComponentProps type props<'x, 'ref> = {x: 'x, ref?: 'ref}
@res.jsxComponentProps
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}
let make: React.component<props<string, ReactDOM.Ref.currentDomRef>>
10 changes: 8 additions & 2 deletions tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

module C4A0 = {
@res.jsxComponentProps
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
type props<'T_open, 'T_type> = {
@as("open") _open: 'T_open,
@as("type") _type: 'T_type,
}

let make = ({@as("open") _open, @as("type") _type, _}: props<_, string>): React.element =>
React.string(_open)
Expand All @@ -14,7 +17,10 @@ module C4A0 = {
}
module C4A1 = {
@res.jsxComponentProps @live
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
type props<'T_open, 'T_type> = {
@as("open") _open: 'T_open,
@as("type") _type: 'T_type,
}

external make: React.component<props<string, string>> = "default"
}
Expand Down
Loading
Loading