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
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#### :nail_care: Polish

- Formatter: Improve multiline printing of record types and values. https://github.com/rescript-lang/rescript/pull/7993

#### :house: Internal

- Reanalyze: refactor DCE to pure pipeline architecture for order-independence and incremental update support. https://github.com/rescript-lang/rescript/pull/8043
Expand Down
33 changes: 8 additions & 25 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 ~record_loc:td.ptype_loc ~state lds cmt_tbl;
print_record_declaration ~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
~record_loc:td.ptype_loc ~state lds cmt_tbl;
print_record_declaration ?inline_record_definitions ~state lds
cmt_tbl;
]
| Ptype_variant cds ->
let manifest =
Expand Down Expand Up @@ -1465,22 +1465,12 @@ 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
?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
~state (lds : Parsetree.label_declaration list) cmt_tbl =
let force_break =
match (check_break_from_loc, record_loc, lds) with
match (check_break_from_loc, lds, List.rev lds) with
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < 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
| _, first :: _, last :: _ ->
first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum
| _, _, _ -> false
in
Doc.breakable_group ~force_break
Expand Down Expand Up @@ -3233,14 +3223,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
* b: 2,
* }` -> record is written on multiple lines, break the group *)
let force_break =
match (spread_expr, rows) with
| 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
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
in
let punning_allowed =
match (spread_expr, rows) with
Expand Down
48 changes: 0 additions & 48 deletions docs/Formatter.md

This file was deleted.

14 changes: 12 additions & 2 deletions tests/syntax_tests/data/ast-mapping/expected/JSXElements.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ 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: 7 additions & 28 deletions tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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 @@ -23,10 +20,7 @@ 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 @@ -44,9 +38,7 @@ 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 @@ -64,11 +56,7 @@ 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 @@ -94,10 +82,7 @@ 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 @@ -115,10 +100,7 @@ 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 @@ -142,10 +124,7 @@ 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: 2 additions & 6 deletions tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ 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 @@ -19,9 +17,7 @@ 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,7 +1,5 @@
@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,9 +1,6 @@
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 @@ -23,10 +20,7 @@ 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 @@ -45,9 +39,7 @@ 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 @@ -65,9 +57,7 @@ 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,10 +2,7 @@

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,9 +2,7 @@

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: 4 additions & 12 deletions tests/syntax_tests/data/ppx/react/expected/forwardRef.resi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ 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 @@ -36,9 +34,7 @@ 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 @@ -60,9 +56,7 @@ 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 @@ -82,9 +76,7 @@ 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: 1 addition & 3 deletions tests/syntax_tests/data/ppx/react/expected/interface.res.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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,8 +1,6 @@
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,8 +1,4 @@
@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
Loading
Loading