Skip to content

Commit 871436a

Browse files
authored
Merge branch 'master' into fix-pipe-underscore-printing
2 parents fbb4a52 + 0010ac4 commit 871436a

32 files changed

+511
-272
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
- Fix `@val` shadowing (rewrite using `globalThis`). https://github.com/rescript-lang/rescript/pull/8098
2828
- Fix `@scope` shadowing (rewrite using `globalThis`). https://github.com/rescript-lang/rescript/pull/8100
2929
- Formatter: normalize underscore placeholders in pipe expressions to canonical form (e.g., `a->map2(_, fn)` formats to `a->map2(fn)`). https://github.com/rescript-lang/rescript/pull/8033
30+
- Fix rewatch panic on duplicate module name. https://github.com/rescript-lang/rescript/pull/8102
3031

3132
#### :memo: Documentation
3233

3334
#### :nail_care: Polish
3435

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

3740
- 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

rewatch/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn initialize_build(
147147
}
148148

149149
let mut build_state = BuildCommandState::new(project_context, packages, compiler, warn_error);
150-
packages::parse_packages(&mut build_state);
150+
packages::parse_packages(&mut build_state)?;
151151

152152
let compile_assets_state = read_compile_state::read(&mut build_state)?;
153153

rewatch/src/build/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub fn clean(path: &Path, show_progress: bool, plain_output: bool) -> Result<()>
366366

367367
let timing_clean_mjs = Instant::now();
368368
let mut build_state = BuildState::new(project_context, packages, compiler_info);
369-
packages::parse_packages(&mut build_state);
369+
packages::parse_packages(&mut build_state)?;
370370
let root_config = build_state.get_root_config();
371371
let suffix_for_print = match root_config.package_specs {
372372
None => match &root_config.suffix {

0 commit comments

Comments
 (0)