Skip to content

Commit 7154796

Browse files
committed
Preserve function body braces
This is an alternative to reasonml#1826 implementing just the brace preservation code, to make it easier to be reviewed. I think this is now in a better place to get into the codebase given that we have `Reason_attributes` outside of `Reason_pprint_ast` and that we now remove our own attributes before printing to OCaml. I think we can probably refactor this code further in future PRs, as well as add other stylistic preservation changes.
1 parent af651a6 commit 7154796

20 files changed

+358
-166
lines changed

formatTest/typeCheckedTests/expected_output/attributes.re

+17-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ let x = [@attrEverything] (true && false);
9999
/**
100100
* How attribute parsings respond to other syntactic constructs.
101101
*/
102-
let add = a => [@onRet] a;
102+
let add = a =>
103+
[@onRet]
104+
{
105+
a;
106+
};
103107
let add = a => [@onRet] a;
104108
let add = [@onEntireFunction] (a => a);
105109

@@ -342,8 +346,18 @@ type classAttributesOnKeys = {
342346
.
343347
[@bs.set] key1: string,
344348
/* The follow two are the same */
345-
[@bs.get null] key2: [@onType2] Js.t(int),
346-
[@bs.get null] key3: [@onType2] Js.t(int),
349+
[@bs.get
350+
{
351+
null;
352+
}
353+
]
354+
key2: [@onType2] Js.t(int),
355+
[@bs.get
356+
{
357+
null;
358+
}
359+
]
360+
key3: [@onType2] Js.t(int),
347361
key4: Js.t([@justOnInt] int),
348362
};
349363

formatTest/typeCheckedTests/expected_output/fastPipe.re

+24-31
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ let c = true;
2626
let t3: bool = !a->b->c;
2727

2828
/* parse fast pipe with underscore application correct */
29-
let doStuff = (a: int, b: int, c: int): int =>
29+
let doStuff = (a: int, b: int, c: int): int => {
3030
a + 2 * b + 3 * c;
31+
};
3132

3233
let (|.) = (a, f) => f(a);
3334

@@ -69,18 +70,16 @@ let saveStatus = Pristine;
6970

7071
let t7: string =
7172
<Foo>
72-
{
73-
(
74-
switch (saveStatus) {
75-
| Pristine => [0]
76-
| Saved => [1]
77-
| Saving => [2]
78-
| Unsaved => [3]
79-
}
80-
)
81-
->Foo.map(Foo.plusOne)
82-
->Foo.toString
83-
}
73+
{(
74+
switch (saveStatus) {
75+
| Pristine => [0]
76+
| Saved => [1]
77+
| Saving => [2]
78+
| Unsaved => [3]
79+
}
80+
)
81+
->Foo.map(Foo.plusOne)
82+
->Foo.toString}
8483
</Foo>;
8584

8685
let genItems = f => List.map(f, items);
@@ -99,23 +98,19 @@ let foo = xs => List.concat([xs, xs]);
9998

10099
let t10: string =
101100
<Foo>
102-
{
103-
blocks
104-
->foo
105-
->Foo.map(Foo.plusOne)
106-
->Foo.toString
107-
}
101+
{blocks
102+
->foo
103+
->Foo.map(Foo.plusOne)
104+
->Foo.toString}
108105
</Foo>;
109106

110107
let t11: string =
111108
<Foo>
112-
{
113-
blocks
114-
->foo
115-
->Foo.map(Foo.plusOne)
116-
->Foo.map(Foo.plusOne)
117-
->Foo.toString
118-
}
109+
{blocks
110+
->foo
111+
->Foo.map(Foo.plusOne)
112+
->Foo.map(Foo.plusOne)
113+
->Foo.toString}
119114
</Foo>;
120115

121116
let title = "los pilares de la tierra";
@@ -164,11 +159,9 @@ module FooLabeled = {
164159

165160
let t14: string =
166161
<FooLabeled>
167-
{
168-
items
169-
->FooLabeled.map(~f=FooLabeled.plusOne)
170-
->FooLabeled.toString
171-
}
162+
{items
163+
->FooLabeled.map(~f=FooLabeled.plusOne)
164+
->FooLabeled.toString}
172165
</FooLabeled>;
173166

174167
let c = (a, b) => a + b;

formatTest/typeCheckedTests/expected_output/jsx.re

+15-7
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,32 @@ module Namespace = {
102102
};
103103

104104
module Optional1 = {
105-
let createElement = (~required, ~children, ()) =>
105+
let createElement = (~required, ~children, ()) => {
106106
switch (required) {
107107
| Some(a) => {displayName: a}
108108
| None => {displayName: "nope"}
109109
};
110+
};
110111
};
111112

112113
module Optional2 = {
113114
let createElement =
114-
(~optional=?, ~children, ()) =>
115+
(~optional=?, ~children, ()) => {
115116
switch (optional) {
116117
| Some(a) => {displayName: a}
117118
| None => {displayName: "nope"}
118119
};
120+
};
119121
};
120122

121123
module DefaultArg = {
122124
let createElement =
123-
(~default=Some("foo"), ~children, ()) =>
125+
(~default=Some("foo"), ~children, ()) => {
124126
switch (default) {
125127
| Some(a) => {displayName: a}
126128
| None => {displayName: "nope"}
127129
};
130+
};
128131
};
129132

130133
module LotsOfArguments = {
@@ -175,8 +178,9 @@ let notReallyJSX = (~foo, ~bar, children) => {
175178
displayName: "test",
176179
};
177180

178-
let fakeRender = (el: component) =>
181+
let fakeRender = (el: component) => {
179182
el.displayName;
183+
};
180184

181185
/* end of setup */
182186

@@ -384,7 +388,7 @@ let asd2 = [@foo] [@JSX] video(~test=false, 10);
384388
let div = (~children) => 1;
385389
[@JSX] ((() => div)())(~children=[]);
386390

387-
let myFun = () =>
391+
let myFun = () => {
388392
<>
389393
<Namespace.Foo
390394
intended=true
@@ -405,10 +409,13 @@ let myFun = () =>
405409
<Foo />
406410
</Namespace.Foo>
407411
</>;
412+
};
408413

409-
let myFun = () => <> </>;
414+
let myFun = () => {
415+
<> </>;
416+
};
410417

411-
let myFun = () =>
418+
let myFun = () => {
412419
<>
413420
<Namespace.Foo
414421
intended=true
@@ -429,6 +436,7 @@ let myFun = () =>
429436
<Foo />
430437
</Namespace.Foo>
431438
</>;
439+
};
432440

433441
/**
434442
* Children should wrap without forcing attributes to.

formatTest/typeCheckedTests/expected_output/oo.re

+33-15
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ class virtual stack ('a) (init) = {
1414
Some(hd);
1515
| [] => None
1616
};
17-
pub push = hd => v = [hd, ...v];
18-
initializer (
19-
print_string("initializing object")
20-
);
21-
pub explicitOverrideTest = a => a + 1;
22-
pri explicitOverrideTest2 = a => a + 1;
17+
pub push = hd => {
18+
v = [hd, ...v];
19+
};
20+
initializer {
21+
print_string("initializing object");
22+
};
23+
pub explicitOverrideTest = a => {
24+
a + 1;
25+
};
26+
pri explicitOverrideTest2 = a => {
27+
a + 1;
28+
};
2329
};
2430

2531
let tmp = {
@@ -50,10 +56,12 @@ class virtual stackWithAttributes ('a) (init) = {
5056
Some(hd);
5157
| [] => None
5258
};
53-
pub push = hd => v = [hd, ...v];
54-
initializer (
55-
print_string("initializing object")
56-
);
59+
pub push = hd => {
60+
v = [hd, ...v];
61+
};
62+
initializer {
63+
print_string("initializing object");
64+
};
5765
};
5866

5967
class extendedStack ('a) (init) = {
@@ -67,9 +75,15 @@ class extendedStackAcknowledgeOverride
6775
(init) = {
6876
inherit (class stack('a))(init);
6977
val dummy = ();
70-
pub implementMe = i => i + 1;
71-
pub! explicitOverrideTest = a => a + 2;
72-
pri! explicitOverrideTest2 = a => a + 2;
78+
pub implementMe = i => {
79+
i + 1;
80+
};
81+
pub! explicitOverrideTest = a => {
82+
a + 2;
83+
};
84+
pri! explicitOverrideTest2 = a => {
85+
a + 2;
86+
};
7387
};
7488

7589
let inst = (new extendedStack)([1, 2]);
@@ -133,8 +147,12 @@ let anonClosedObject: {
133147
x: int,
134148
y: int,
135149
} = {
136-
pub x = 0;
137-
pub y = 0
150+
pub x = {
151+
0;
152+
};
153+
pub y = {
154+
0;
155+
}
138156
};
139157

140158
let onlyHasX = {pub x = 0};

formatTest/typeCheckedTests/expected_output/reasonComments.re

+11-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ let myFunction = /* First arg */
9494
withFirstArg,
9595
/* Second Arg */
9696
andSecondArg,
97-
) =>
98-
withFirstArg + andSecondArg; /* After Semi */
97+
) => {
98+
withFirstArg + andSecondArg;
99+
}; /* After Semi */
99100

100101
type point = {
101102
x: string, /* x field */
@@ -300,7 +301,11 @@ type intPair2 = (
300301
int,
301302
);
302303

303-
let result = /**/ (2 + 3);
304+
let result =
305+
/**/
306+
{
307+
2 + 3;
308+
};
304309

305310
/* This is not yet idempotent */
306311
/* { */
@@ -333,7 +338,9 @@ let blahCurriedX = x =>
333338
| Black(x) => 0 /* After black */
334339
| Green(x) => 0; /* After second green */ /* On next line after blahCurriedX def */
335340

336-
let name_equal = (x, y) => x == y;
341+
let name_equal = (x, y) => {
342+
x == y;
343+
};
337344

338345
let equal = (i1, i2) =>
339346
i1.contents === i2.contents && true; /* most unlikely first */

formatTest/typeCheckedTests/expected_output/sequences.re

+13-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ let twenty = 20;
2323
* printed in reduced form because sequences are a *parse* time construct.
2424
* To ensure these are parsed correctly, adding to an integer.
2525
*/
26-
let result = 0 + twenty;
27-
let result = 0 + twenty;
26+
let result =
27+
0
28+
+ {
29+
twenty;
30+
};
31+
let result =
32+
0
33+
+ {
34+
twenty;
35+
};
2836
let result = 0 + twenty;
2937

3038
let unitValue = ();
@@ -56,7 +64,9 @@ let cannotPunASingleFieldRecord = {
5664
};
5765
let fourty =
5866
20 + cannotPunASingleFieldRecord.number;
59-
let thisIsASequenceNotPunedRecord = number;
67+
let thisIsASequenceNotPunedRecord = {
68+
number;
69+
};
6070
let fourty = 20 + thisIsASequenceNotPunedRecord;
6171

6272
type recordType = {

formatTest/typeCheckedTests/expected_output/trailing.re

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ class virtual
172172
];
173173
pub virtual implementMe:
174174
(int, int) => (int, int);
175-
initializer (
176-
print_string("initializing object")
177-
);
175+
initializer {
176+
print_string("initializing object");
177+
};
178178
};
179179

180180
class extendedStack

0 commit comments

Comments
 (0)