Skip to content

Commit

Permalink
feat: always break records with 2 or more fields (#2779)
Browse files Browse the repository at this point in the history
* feat: always break records with 2 or more fields

* fix 4.06 test

* fix 4.10 test

* fix 4.12 test

* fix 4.12 test

* fix 4.12 test
  • Loading branch information
anmonteiro authored Jul 29, 2024
1 parent fd4611a commit d7e96a7
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 41 deletions.
12 changes: 9 additions & 3 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2873,7 +2873,7 @@ let printer = object(self:'self)
source_map ~loc:pld.pld_loc recordRow
in
let rows = List.map recordRow lbls in
(* if a record has more than 1 row, always break *)
(* if a record type has more than 1 row, always break *)
let break =
match rows with
| [] | [ _ ] -> Layout.IfNeed
Expand Down Expand Up @@ -6226,7 +6226,7 @@ let printer = object(self:'self)
| (Pexp_record (recordRows, optionalGadt), _, _) ->
forceBreak := true;
let keyWithColon = makeList (maybeQuoteFirstElem li [atom ":"]) in
let value = self#unparseRecord ~forceBreak: true recordRows optionalGadt in
let value = self#unparseRecord ~forceBreak:true recordRows optionalGadt in
label ~space:true keyWithColon value
| (Pexp_extension (s, p), _, _) when s.txt = "mel.obj" ->
forceBreak := true;
Expand Down Expand Up @@ -6283,9 +6283,15 @@ let printer = object(self:'self)
in
firstRow::(getRows l)
in
let break =
(* if a record has more than 1 row, always break *)
match !forceBreak, allRows with
| false, ([] | [ _ ]) -> Layout.IfNeed
| _ -> Layout.Always_rec
in
makeList
~wrap:(lwrap ^ "{" ,"}" ^ rwrap)
~break:(if !forceBreak then Layout.Always else Layout.IfNeed)
~break
~sep:commaTrail
~postSpace:true
(groupAndPrint ~xf:fst ~getLoc:snd ~comments:self#comments allRows)
Expand Down
10 changes: 8 additions & 2 deletions test/4.06/type-trailing.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ Print the formatted file xx
};
let p = {contents: 0};
let {contents: c} = p;
let point = {x: 0, y: 0};
let point2 = {...point, y: 200};
let point = {
x: 0,
y: 0,
};
let point2 = {
...point,
y: 200,
};

let myTuple = (0, 0);
let (i, j) = myTuple;
Expand Down
5 changes: 4 additions & 1 deletion test/4.10/reasonComments-re.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ Print the formatted file
};
/** doc let */
let letter: q = {a: 42, b: "answer"};
let letter: q = {
a: 42,
b: "answer",
};

Type-check basics
$ ocamlc -c -pp 'refmt --print binary' -intf-suffix .rei -impl formatted.re
Expand Down
5 changes: 4 additions & 1 deletion test/4.12/reasonComments-re.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ Print the formatted file
};
/** doc let */
let letter: q = {a: 42, b: "answer"};
let letter: q = {
a: 42,
b: "answer",
};

Type-check basics
$ ocamlc -c -pp 'refmt --print binary' -intf-suffix .rei -impl formatted.re
Expand Down
20 changes: 16 additions & 4 deletions test/basicStructures.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,24 @@ Format basicStructures
/* no punning available for a single field. Can't tell the difference with a scope + expression */
let componentA = {props: props};
/* pun for real */
let componentB = {props, state: ()};
let componentB = {
props,
state: (),
};
/* pun fields with module prefix too */
let foo = {Foo.foo: foo};
let bar = {Foo.foo, bar: 1};
let bar = {bar: 1, Foo.foo};
let bar = {Foo.foo, Bar.bar};
let bar = {
Foo.foo,
bar: 1,
};
let bar = {
bar: 1,
Foo.foo,
};
let bar = {
Foo.foo,
Bar.bar,
};
({M.x, y}) => 1;
Expand Down
52 changes: 44 additions & 8 deletions test/general-syntax-re.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ Format general implementation syntax
y: int,
z: int,
};
let point2D = {x: 20, y: 30};
let point2D = {
x: 20,
y: 30,
};
let point3D: point3D = {
x: 10,
Expand All @@ -282,7 +285,10 @@ Format general implementation syntax
let res1 = printPoint(point2D);
let res2 =
printPoint({x: point3D.x, y: point3D.y});
printPoint({
x: point3D.x,
y: point3D.y,
});
/*
When () were used to indicate sequences, the parser used seq_expr not only
Expand Down Expand Up @@ -310,7 +316,10 @@ Format general implementation syntax
printPoint(
addPoints(
point2D,
{x: point3D.x, y: point3D.y},
{
x: point3D.x,
y: point3D.y,
},
),
);

Expand All @@ -324,10 +333,16 @@ Format general implementation syntax
dateHired: int,
};

let o: person = {name: "bob", age: 10};
let o: person = {
name: "bob",
age: 10,
};

/* Parens needed? Nope! */
let o: person = {name: "bob", age: 10};
let o: person = {
name: "bob",
age: 10,
};

let printPerson = (p: person) => {
let q: person = p;
Expand Down Expand Up @@ -1400,9 +1415,30 @@ Format general implementation syntax
foo(~a=Foo.a);

/* https://github.com/facebook/reason/issues/2155#issuecomment-422077648 */
true ? (Update({...a, b: 1}), None) : x;
true ? {...a, b: 1} : a;
true ? (a, {...a, b: 1}) : a;
true
? (
Update({
...a,
b: 1,
}),
None,
)
: x;
true
? {
...a,
b: 1,
}
: a;
true
? (
a,
{
...a,
b: 1,
},
)
: a;

true ? ([x, ...xs]) => f(x, xs) : a;

Expand Down
10 changes: 8 additions & 2 deletions test/modules.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ Format modules
M.[foo, bar];
};
let z = {
M.{x: 10, y: 20};
M.{
x: 10,
y: 20,
};
};
let z = {
M.(M2.(value));
Expand Down Expand Up @@ -590,7 +593,10 @@ Format modules
};
let z = {
open! M;
{x: 10, y: 20};
{
x: 10,
y: 20,
};
};
let z = {
open! M;
Expand Down
10 changes: 8 additions & 2 deletions test/modules_no_semi.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ Format modules no semi
M.[foo, bar];
};
let z = {
M.{x: 10, y: 20};
M.{
x: 10,
y: 20,
};
};
let z = {
M.(M2.(value));
Expand Down Expand Up @@ -590,7 +593,10 @@ Format modules no semi
};
let z = {
open! M;
{x: 10, y: 20};
{
x: 10,
y: 20,
};
};
let z = {
open! M;
Expand Down
6 changes: 5 additions & 1 deletion test/patternMatching.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ Print the formatted file
| ({from: None, to_: None}: intRange) => (
None: optIntRange
)
| {from, to_} => Some({from, to_});
| {from, to_} =>
Some({
from,
to_,
});

Type-check basics
$ ocamlc -c -pp 'refmt --print binary' -intf-suffix .rei -impl formatted.re
Expand Down
5 changes: 4 additions & 1 deletion test/pexpFun.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Format function expressipns (pexpFun)
switch (x) {
| Bar =>
ReasonReact.UpdateWithSideEffects(
{...state, click: click + 1},
{
...state,
click: click + 1,
},
self => {
let _ = 1;
apply(bar);
Expand Down
18 changes: 15 additions & 3 deletions test/sequences.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,21 @@ Print the formatted file
let b = 0;
let c = 0;
/* All of these will be printed as punned because they have more than one field. */
let firstFieldPunned = {a, b, c};
let sndFieldPunned = {a, b, c};
let thirdFieldPunned = {a, b, c};
let firstFieldPunned = {
a,
b,
c,
};
let sndFieldPunned = {
a,
b,
c,
};
let thirdFieldPunned = {
a,
b,
c,
};
let singlePunAcceptedIfExtended = {
...firstFieldPunned,
a,
Expand Down
40 changes: 30 additions & 10 deletions test/trailing.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ Format trailing
$ refmt ./input.re
let x = {"obj": obj};

let x = {"key": key, "keyTwo": keyTwo};

let x = {...x, "key": key};

let x = {...x, "key": key, "keyTwo": keyTwo};
let x = {
"key": key,
"keyTwo": keyTwo,
};

let x = {
...x,
"key": key,
};

let x = {
...x,
"key": key,
"keyTwo": keyTwo,
};

type t = {. "x": int};

Expand All @@ -18,11 +28,21 @@ Format trailing

let x = {"obj": 0};

let x = {"key": 0, "keyTwo": 1};

let x = {...x, "key": 0};

let x = {...x, "key": 0, "keyTwo": 1};
let x = {
"key": 0,
"keyTwo": 1,
};

let x = {
...x,
"key": 0,
};

let x = {
...x,
"key": 0,
"keyTwo": 1,
};

type t = {. "x": int};

Expand Down
5 changes: 4 additions & 1 deletion test/type-pipeFirst.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ Print the formatted file
x: int,
y: int,
};
let coord = {x: 1, y: 1};
let coord = {
x: 1,
y: 1,
};

/* parses as (coord.x)->a->b->c */
let t2: int = coord.x->a->b->c;
Expand Down
9 changes: 8 additions & 1 deletion test/variants.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,14 @@ Format variants
let result =
switch (
AlsoHasARecord(10, 10, {x: 10, y: 20})
AlsoHasARecord(
10,
10,
{
x: 10,
y: 20,
},
)
) {
| Blah => 1000
| AlsoHasARecord(a, b, {x, y}) =>
Expand Down
5 changes: 4 additions & 1 deletion test/wrapping-re.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,10 @@ Format wrapping in .re files
*/
let result =
acceptsTwoThings(
{age: 20, name: "a"},
{
age: 20,
name: "a",
},
{
fieldOne: 10,
fieldtwo: [10, 20],
Expand Down

0 comments on commit d7e96a7

Please sign in to comment.