Skip to content

Commit

Permalink
Preserve function body braces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anmonteiro committed Oct 9, 2018
1 parent d3a50cd commit 0c5c67c
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 166 deletions.
20 changes: 17 additions & 3 deletions formatTest/typeCheckedTests/expected_output/attributes.re
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ let x = [@attrEverything] (true && false);
/**
* How attribute parsings respond to other syntactic constructs.
*/
let add = a => [@onRet] a;
let add = a =>
[@onRet]
{
a;
};
let add = a => [@onRet] a;
let add = [@onEntireFunction] (a => a);

Expand Down Expand Up @@ -342,8 +346,18 @@ type classAttributesOnKeys = {
.
[@bs.set] key1: string,
/* The follow two are the same */
[@bs.get null] key2: [@onType2] Js.t(int),
[@bs.get null] key3: [@onType2] Js.t(int),
[@bs.get
{
null;
}
]
key2: [@onType2] Js.t(int),
[@bs.get
{
null;
}
]
key3: [@onType2] Js.t(int),
key4: Js.t([@justOnInt] int),
};

Expand Down
55 changes: 24 additions & 31 deletions formatTest/typeCheckedTests/expected_output/fastPipe.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ let c = true;
let t3: bool = !a->b->c;

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

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

Expand Down Expand Up @@ -69,18 +70,16 @@ let saveStatus = Pristine;

let t7: string =
<Foo>
{
(
switch (saveStatus) {
| Pristine => [0]
| Saved => [1]
| Saving => [2]
| Unsaved => [3]
}
)
->Foo.map(Foo.plusOne)
->Foo.toString
}
{(
switch (saveStatus) {
| Pristine => [0]
| Saved => [1]
| Saving => [2]
| Unsaved => [3]
}
)
->Foo.map(Foo.plusOne)
->Foo.toString}
</Foo>;

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

let t10: string =
<Foo>
{
blocks
->foo
->Foo.map(Foo.plusOne)
->Foo.toString
}
{blocks
->foo
->Foo.map(Foo.plusOne)
->Foo.toString}
</Foo>;

let t11: string =
<Foo>
{
blocks
->foo
->Foo.map(Foo.plusOne)
->Foo.map(Foo.plusOne)
->Foo.toString
}
{blocks
->foo
->Foo.map(Foo.plusOne)
->Foo.map(Foo.plusOne)
->Foo.toString}
</Foo>;

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

let t14: string =
<FooLabeled>
{
items
->FooLabeled.map(~f=FooLabeled.plusOne)
->FooLabeled.toString
}
{items
->FooLabeled.map(~f=FooLabeled.plusOne)
->FooLabeled.toString}
</FooLabeled>;

let c = (a, b) => a + b;
Expand Down
22 changes: 15 additions & 7 deletions formatTest/typeCheckedTests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,32 @@ module Namespace = {
};

module Optional1 = {
let createElement = (~required, ~children, ()) =>
let createElement = (~required, ~children, ()) => {
switch (required) {
| Some(a) => {displayName: a}
| None => {displayName: "nope"}
};
};
};

module Optional2 = {
let createElement =
(~optional=?, ~children, ()) =>
(~optional=?, ~children, ()) => {
switch (optional) {
| Some(a) => {displayName: a}
| None => {displayName: "nope"}
};
};
};

module DefaultArg = {
let createElement =
(~default=Some("foo"), ~children, ()) =>
(~default=Some("foo"), ~children, ()) => {
switch (default) {
| Some(a) => {displayName: a}
| None => {displayName: "nope"}
};
};
};

module LotsOfArguments = {
Expand Down Expand Up @@ -175,8 +178,9 @@ let notReallyJSX = (~foo, ~bar, children) => {
displayName: "test",
};

let fakeRender = (el: component) =>
let fakeRender = (el: component) => {
el.displayName;
};

/* end of setup */

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

let myFun = () =>
let myFun = () => {
<>
<Namespace.Foo
intended=true
Expand All @@ -405,10 +409,13 @@ let myFun = () =>
<Foo />
</Namespace.Foo>
</>;
};

let myFun = () => <> </>;
let myFun = () => {
<> </>;
};

let myFun = () =>
let myFun = () => {
<>
<Namespace.Foo
intended=true
Expand All @@ -429,6 +436,7 @@ let myFun = () =>
<Foo />
</Namespace.Foo>
</>;
};

/**
* Children should wrap without forcing attributes to.
Expand Down
48 changes: 33 additions & 15 deletions formatTest/typeCheckedTests/expected_output/oo.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ class virtual stack ('a) (init) = {
Some(hd);
| [] => None
};
pub push = hd => v = [hd, ...v];
initializer (
print_string("initializing object")
);
pub explicitOverrideTest = a => a + 1;
pri explicitOverrideTest2 = a => a + 1;
pub push = hd => {
v = [hd, ...v];
};
initializer {
print_string("initializing object");
};
pub explicitOverrideTest = a => {
a + 1;
};
pri explicitOverrideTest2 = a => {
a + 1;
};
};

let tmp = {
Expand Down Expand Up @@ -50,10 +56,12 @@ class virtual stackWithAttributes ('a) (init) = {
Some(hd);
| [] => None
};
pub push = hd => v = [hd, ...v];
initializer (
print_string("initializing object")
);
pub push = hd => {
v = [hd, ...v];
};
initializer {
print_string("initializing object");
};
};

class extendedStack ('a) (init) = {
Expand All @@ -67,9 +75,15 @@ class extendedStackAcknowledgeOverride
(init) = {
inherit (class stack('a))(init);
val dummy = ();
pub implementMe = i => i + 1;
pub! explicitOverrideTest = a => a + 2;
pri! explicitOverrideTest2 = a => a + 2;
pub implementMe = i => {
i + 1;
};
pub! explicitOverrideTest = a => {
a + 2;
};
pri! explicitOverrideTest2 = a => {
a + 2;
};
};

let inst = (new extendedStack)([1, 2]);
Expand Down Expand Up @@ -133,8 +147,12 @@ let anonClosedObject: {
x: int,
y: int,
} = {
pub x = 0;
pub y = 0
pub x = {
0;
};
pub y = {
0;
}
};

let onlyHasX = {pub x = 0};
Expand Down
15 changes: 11 additions & 4 deletions formatTest/typeCheckedTests/expected_output/reasonComments.re
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ let myFunction = /* First arg */
withFirstArg,
/* Second Arg */
andSecondArg,
) =>
withFirstArg + andSecondArg; /* After Semi */
) => {
withFirstArg + andSecondArg;
}; /* After Semi */

type point = {
x: string, /* x field */
Expand Down Expand Up @@ -300,7 +301,11 @@ type intPair2 = (
int,
);

let result = /**/ (2 + 3);
let result =
/**/
{
2 + 3;
};

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

let name_equal = (x, y) => x == y;
let name_equal = (x, y) => {
x == y;
};

let equal = (i1, i2) =>
i1.contents === i2.contents && true; /* most unlikely first */
Expand Down
16 changes: 13 additions & 3 deletions formatTest/typeCheckedTests/expected_output/sequences.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ let twenty = 20;
* printed in reduced form because sequences are a *parse* time construct.
* To ensure these are parsed correctly, adding to an integer.
*/
let result = 0 + twenty;
let result = 0 + twenty;
let result =
0
+ {
twenty;
};
let result =
0
+ {
twenty;
};
let result = 0 + twenty;

let unitValue = ();
Expand Down Expand Up @@ -56,7 +64,9 @@ let cannotPunASingleFieldRecord = {
};
let fourty =
20 + cannotPunASingleFieldRecord.number;
let thisIsASequenceNotPunedRecord = number;
let thisIsASequenceNotPunedRecord = {
number;
};
let fourty = 20 + thisIsASequenceNotPunedRecord;

type recordType = {
Expand Down
6 changes: 3 additions & 3 deletions formatTest/typeCheckedTests/expected_output/trailing.re
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ class virtual
];
pub virtual implementMe:
(int, int) => (int, int);
initializer (
print_string("initializing object")
);
initializer {
print_string("initializing object");
};
};

class extendedStack
Expand Down
Loading

0 comments on commit 0c5c67c

Please sign in to comment.