Skip to content

Commit 0010ac4

Browse files
shulhinojaf
andauthored
Improve multiline printing of record types and values (#7993)
* WIP * Update tests * Change how force_break is handled in type declaration printing * Fix type declaration with spread * Update tests * Update more tests (ppx) * Update CHANGELOG * Add note on the formatter philosophy --------- Co-authored-by: nojaf <florian.verdonck@outlook.com>
1 parent 4f8b945 commit 0010ac4

27 files changed

+285
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#### :nail_care: Polish
3434

35+
- Formatter: Improve multiline printing of record types and values. https://github.com/rescript-lang/rescript/pull/7993
36+
3537
#### :house: Internal
3638

3739
- Reanalyze: refactor DCE to pure pipeline architecture for order-independence and incremental update support. https://github.com/rescript-lang/rescript/pull/8043

compiler/syntax/src/res_printer.ml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ and print_type_declaration ~state ~name ~equal_sign ~rec_flag i
12821282
manifest;
12831283
Doc.concat [Doc.space; Doc.text equal_sign; Doc.space];
12841284
print_private_flag td.ptype_private;
1285-
print_record_declaration ~state lds cmt_tbl;
1285+
print_record_declaration ~record_loc:td.ptype_loc ~state lds cmt_tbl;
12861286
]
12871287
| Ptype_variant cds ->
12881288
let manifest =
@@ -1370,8 +1370,8 @@ and print_type_declaration2 ?inline_record_definitions ~state ~rec_flag
13701370
manifest;
13711371
Doc.concat [Doc.space; Doc.text equal_sign; Doc.space];
13721372
print_private_flag td.ptype_private;
1373-
print_record_declaration ?inline_record_definitions ~state lds
1374-
cmt_tbl;
1373+
print_record_declaration ?inline_record_definitions
1374+
~record_loc:td.ptype_loc ~state lds cmt_tbl;
13751375
]
13761376
| Ptype_variant cds ->
13771377
let manifest =
@@ -1465,12 +1465,22 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance)
14651465
Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl]
14661466

14671467
and print_record_declaration ?check_break_from_loc ?inline_record_definitions
1468-
~state (lds : Parsetree.label_declaration list) cmt_tbl =
1468+
?record_loc ~state (lds : Parsetree.label_declaration list) cmt_tbl =
1469+
let get_field_start_line (ld : Parsetree.label_declaration) =
1470+
(* For spread fields (...), use the type location instead of pld_loc
1471+
because pld_loc may incorrectly include preceding whitespace *)
1472+
if ld.pld_name.txt = "..." then ld.pld_type.ptyp_loc.loc_start.pos_lnum
1473+
else ld.pld_loc.loc_start.pos_lnum
1474+
in
14691475
let force_break =
1470-
match (check_break_from_loc, lds, List.rev lds) with
1476+
match (check_break_from_loc, record_loc, lds) with
14711477
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum
1472-
| _, first :: _, last :: _ ->
1473-
first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum
1478+
| None, Some loc, first :: _ ->
1479+
(* Check if first field is on a different line than the opening brace *)
1480+
loc.loc_start.pos_lnum < get_field_start_line first
1481+
| None, None, first :: _ ->
1482+
let last = List.hd (List.rev lds) in
1483+
get_field_start_line first < last.pld_loc.loc_end.pos_lnum
14741484
| _, _, _ -> false
14751485
in
14761486
Doc.breakable_group ~force_break
@@ -3223,7 +3233,14 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
32233233
* b: 2,
32243234
* }` -> record is written on multiple lines, break the group *)
32253235
let force_break =
3226-
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
3236+
match (spread_expr, rows) with
3237+
| Some expr, _ ->
3238+
(* If there's a spread, compare with spread expression's location *)
3239+
e.pexp_loc.loc_start.pos_lnum < expr.pexp_loc.loc_start.pos_lnum
3240+
| None, first_row :: _ ->
3241+
(* Otherwise, compare with the first row's location *)
3242+
e.pexp_loc.loc_start.pos_lnum < first_row.lid.loc.loc_start.pos_lnum
3243+
| None, [] -> false
32273244
in
32283245
let punning_allowed =
32293246
match (spread_expr, rows) with

docs/Formatter.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ReScript Formatter
2+
3+
## Philosophy
4+
5+
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.
6+
7+
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.
8+
9+
## Decision Making
10+
11+
- **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.
12+
13+
- **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.
14+
15+
- **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.
16+
17+
## Prior Decisions
18+
19+
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.
20+
21+
- **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).
22+
23+
- **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).
24+
25+
**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.
26+
27+
## Guidelines for Contributors
28+
29+
### Submitting Formatting Issues
30+
31+
- You may open issues to report bugs or propose improvements
32+
- Understand that proposals may be closed if they don't align with core team vision
33+
- Avoid reopening closed issues unless there's new technical information
34+
- Respect that "the core team isn't feeling it" is a valid reason for closure
35+
36+
### What We Consider
37+
38+
- Technical correctness and consistency
39+
- Alignment with ReScript's design philosophy
40+
- Maintainability and simplicity of the formatter implementation
41+
- Core team consensus
42+
43+
### What We Generally Avoid
44+
45+
- Style preferences that don't align with our vision
46+
- 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)
47+
- Requests that would significantly complicate the formatter implementation
48+
- Debates about subjective formatting choices

tests/syntax_tests/data/ast-mapping/expected/JSXElements.res.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@ let emptyUnary = ReactDOM.jsx("input", {})
22

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

5-
let emptyUnaryWithAttributes = ReactDOM.jsx(
6-
"input",
7-
{
8-
type_: "text",
9-
},
10-
)
5+
let emptyUnaryWithAttributes = ReactDOM.jsx("input", {type_: "text"})
116

12-
let emptyNonunaryWithAttributes = ReactDOM.jsx(
13-
"div",
14-
{
15-
className: "container",
16-
},
17-
)
7+
let emptyNonunaryWithAttributes = ReactDOM.jsx("div", {className: "container"})
188

199
let elementWithChildren = ReactDOM.jsxs(
2010
"div",

tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
module C0 = {
44
@res.jsxComponentProps
5-
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
5+
type props<'priority, 'text> = {
6+
priority: 'priority,
7+
text?: 'text,
8+
}
69

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

2124
module C1 = {
2225
@res.jsxComponentProps
23-
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
26+
type props<'priority, 'text> = {
27+
priority: 'priority,
28+
text?: 'text,
29+
}
2430

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

3945
module C2 = {
4046
@res.jsxComponentProps
41-
type props<'foo> = {foo?: 'foo}
47+
type props<'foo> = {
48+
foo?: 'foo,
49+
}
4250

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

5765
module C3 = {
5866
@res.jsxComponentProps
59-
type props<'foo, 'a, 'b> = {foo?: 'foo, a?: 'a, b: 'b}
67+
type props<'foo, 'a, 'b> = {
68+
foo?: 'foo,
69+
a?: 'a,
70+
b: 'b,
71+
}
6072

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

8395
module C4 = {
8496
@res.jsxComponentProps
85-
type props<'a, 'x> = {a: 'a, x?: 'x}
97+
type props<'a, 'x> = {
98+
a: 'a,
99+
x?: 'x,
100+
}
86101

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

101116
module C5 = {
102117
@res.jsxComponentProps
103-
type props<'a, 'z> = {a: 'a, z?: 'z}
118+
type props<'a, 'z> = {
119+
a: 'a,
120+
z?: 'z,
121+
}
104122

105123
let make = ({a: (x, y), z: ?__z, _}: props<_, _>) => {
106124
let z = switch __z {
@@ -124,7 +142,10 @@ module C6 = {
124142
let make: React.component<props>
125143
}
126144
@res.jsxComponentProps
127-
type props<'comp, 'x> = {comp: 'comp, x: 'x}
145+
type props<'comp, 'x> = {
146+
comp: 'comp,
147+
x: 'x,
148+
}
128149

129150
let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>): React.element =>
130151
React.jsx(Comp.make, {})

tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ let f = a => Js.Promise.resolve(a + a)
22

33
module C0 = {
44
@res.jsxComponentProps
5-
type props<'a> = {a: 'a}
5+
type props<'a> = {
6+
a: 'a,
7+
}
68

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

1820
module C1 = {
1921
@res.jsxComponentProps
20-
type props<'status> = {status: 'status}
22+
type props<'status> = {
23+
status: 'status,
24+
}
2125

2226
let make = async ({status, _}: props<_>): React.element => {
2327
switch status {

tests/syntax_tests/data/ppx/react/expected/commentAtTop.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@res.jsxComponentProps
2-
type props<'msg> = {msg: 'msg} // test React JSX file
2+
type props<'msg> = {
3+
msg: 'msg, // test React JSX file
4+
}
35

46
let make = ({msg, _}: props<_>): React.element => {
57
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})

tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module C0 = {
22
@res.jsxComponentProps
3-
type props<'a, 'b> = {a?: 'a, b?: 'b}
3+
type props<'a, 'b> = {
4+
a?: 'a,
5+
b?: 'b,
6+
}
47
let make = ({a: ?__a, b: ?__b, _}: props<_, _>) => {
58
let a = switch __a {
69
| Some(a) => a
@@ -20,7 +23,10 @@ module C0 = {
2023

2124
module C1 = {
2225
@res.jsxComponentProps
23-
type props<'a, 'b> = {a?: 'a, b: 'b}
26+
type props<'a, 'b> = {
27+
a?: 'a,
28+
b: 'b,
29+
}
2430

2531
let make = ({a: ?__a, b, _}: props<_, _>) => {
2632
let a = switch __a {
@@ -39,7 +45,9 @@ module C1 = {
3945
module C2 = {
4046
let a = "foo"
4147
@res.jsxComponentProps
42-
type props<'a> = {a?: 'a}
48+
type props<'a> = {
49+
a?: 'a,
50+
}
4351

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

5866
module C3 = {
5967
@res.jsxComponentProps
60-
type props<'disabled> = {disabled?: 'disabled}
68+
type props<'disabled> = {
69+
disabled?: 'disabled,
70+
}
6171

6272
let make = ({disabled: ?__everythingDisabled, _}: props<bool>) => {
6373
let everythingDisabled = switch __everythingDisabled {

tests/syntax_tests/data/ppx/react/expected/externalWithCustomName.res.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
module Foo = {
44
@res.jsxComponentProps @live
5-
type props<'a, 'b> = {a: 'a, b: 'b}
5+
type props<'a, 'b> = {
6+
a: 'a,
7+
b: 'b,
8+
}
69

710
@module("Foo")
811
external component: React.component<props<int, string>> = "component"

tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
module V4A = {
44
@res.jsxComponentProps
5-
type props<'msg> = {msg: 'msg}
5+
type props<'msg> = {
6+
msg: 'msg,
7+
}
68

79
let make = ({msg, _}: props<_>): React.element => {
810
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})

0 commit comments

Comments
 (0)