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
48 changes: 48 additions & 0 deletions docs/Formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ReScript Formatter

## Philosophy

The ReScript formatter is **opinionated**. Formatting decisions are made by the core team based on our collective judgment and vision for the language. We do not aim to accommodate every stylistic preference or engage in extended debates about formatting choices.

The formatter currently has **no configuration settings**, and we aspire to keep it that way. This ensures that ReScript code looks consistent across all projects and teams, eliminating style debates and configuration overhead.

## Decision Making

- **Core team consensus is final**: When the core team reaches consensus on a formatting decision, that decision stands. There is no requirement for community-wide agreement or extensive discussion.

- **Community input is welcome but not binding**: We appreciate suggestions and feedback from the community, but these can be closed without extensive justification if the core team is not aligned with the proposal.

- **No endless style discussions**: We are not interested in protracted debates about formatting preferences. The formatter exists to provide consistent, automated formatting—not to serve as a platform for style negotiations.

## Prior Decisions

The following are examples of formatting decisions the core team has made. This list is not exhaustive, and these decisions do not create binding precedents for future discussions. The core team retains full discretion to make different decisions in similar cases.

- **Smart linebreaks for pipe chains**: The formatter preserves user-introduced linebreaks in pipe chains (`->`), allowing users to control multiline formatting. See [forum announcement](https://forum.rescript-lang.org/t/ann-smart-linebreaks-for-pipe-chains/4734).

- **Preserve multilineness for records**: The formatter preserves multiline formatting for record types and values when users introduce linebreaks. See [issue #7961](https://github.com/rescript-lang/rescript/issues/7961).

**Important**: These examples are provided for reference only. They do not establish rules or precedents that constrain future formatting decisions. The core team may choose different approaches in similar situations based on current consensus.

## Guidelines for Contributors

### Submitting Formatting Issues

- You may open issues to report bugs or propose improvements
- Understand that proposals may be closed if they don't align with core team vision
- Avoid reopening closed issues unless there's new technical information
- Respect that "the core team isn't feeling it" is a valid reason for closure

### What We Consider

- Technical correctness and consistency
- Alignment with ReScript's design philosophy
- Maintainability and simplicity of the formatter implementation
- Core team consensus

### What We Generally Avoid

- Style preferences that don't align with our vision
- Using comparisons to other formatters as the sole justification for changes (while we may align with other formatters on many decisions, we make choices based on our own judgment, not because another formatter does it)
- Requests that would significantly complicate the formatter implementation
- Debates about subjective formatting choices
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
Loading
Loading