Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve function body braces and distinguish open vs. X.() #1826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions formatTest/typeCheckedTests/expected_output/attributes.re
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ let x = [@attrEverything] (true && false);
/**
* How attribute parsings respond to other syntactic constructs.
*/
let add = a => [@onRet] a;
let add = a =>
[@onRet]
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening here

Copy link
Member Author

@jordwalke jordwalke Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input file actually has braces. The old test output was stripping the braces and now this diff preserves them.
This is what the input has:

let add(a) { [@onRet] a };

It doesn't distinguish between [@a] { x } and { [@a] x } as you can see.

a;
};

let add = a => [@onRet] a;

Expand Down Expand Up @@ -382,8 +386,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
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening here then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing. The attributes were specified with braces, and before this diff they were getting refmted away.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean these will always get formatted like this now or something?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the braces I guess they will get formatted how they were previously.

Copy link
Member Author

@jordwalke jordwalke Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Previously, the braces were getting formatted away. Now, we preserve whether or not you included additional braces. You can have it format whichever way you want - and it will preserve your intent.

null;
}
]
key2: [@onType2] Js.t(int),
[@bs.get
{
null;
}
]
key3: [@onType2] Js.t(int),
key4: Js.t([@justOnInt] int),
};

Expand Down Expand Up @@ -533,7 +547,13 @@ let res =

let res =
switch (x) {
| _ => [@attr] String.(Array.(concat))
| _ =>
[@attr]
{
open String;
open Array;
concat;
}
};

/* GADT */
Expand Down Expand Up @@ -602,10 +622,6 @@ type tttttt = {
mutable x: int,
};

let tmp =
/** On if statement */
(if (true) {true} else {false});

type foo =
option(
[@foo
Expand Down
181 changes: 181 additions & 0 deletions formatTest/typeCheckedTests/expected_output/braces.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
let preserveThis = "preserveThis";

let x = a => {
preserveThis;
};

let x = a => {
/* Even with comment */
preserveThis;
};

let res = {
/**
* doc comment.
*/
(if (true) {()} else {()});
();
};

let x = a => {
/* It would be okay for this comment to get formatted into
* the braces but ideally it wouldn't. */
preserveThis;
};

type recordType = {
recordTypeFieldOne: int,
recordTypeFieldTwo: string,
};

let x =
{
recordTypeFieldOne: 0,
recordTypeFieldTwo: "two",
}.recordTypeFieldOne;

/**
* Use a pointless wrapping.
*/
let x =
{
{
recordTypeFieldOne: 0,
recordTypeFieldTwo: "two",
};
}.recordTypeFieldOne;

let createRecord = () => {
recordTypeFieldOne: 0,
recordTypeFieldTwo: "two",
};

let x =
{
createRecord();
}.recordTypeFieldOne;

let y = [@attr] 3;

/* The attribute on the explicit braces should also be preserved */
let y =
[@attr]
{
3;
};

let myFun = (. a, b, c) => a + b + c;

let result = {
myFun(. 0, 1, 2);
};

let result =
[@attr]
{
myFun(. 0, 1, 2);
};

let tmp =
/** On if statement */
{
if (true) {true} else {false};
};

let tmp = {
let tmp = false;
/** On if statement */
(if (tmp) {true} else {false});
};

let tmp = {
let tmp = false;
/** On if statement */
(if (tmp) {"true"} else {"false"});
"but the if statement wasn't the last";
};

module Callbacks = {
let runThisCallback = (s, next) => {
s ++ next("!");
();
};
};

let result =
Callbacks.runThisCallback("hi", s =>
s ++ "concatThis!"
);

let result =
Callbacks.runThisCallback("hi", s => {
s ++ "!";
});

Callbacks.runThisCallback("hi", s =>
s ++ "concatThis!"
);

Callbacks.runThisCallback("hi", s => {
s ++ "concatThis!";
});

Callbacks.runThisCallback("hi", s => {
let s = s ++ s;
s ++ "concatThis!";
});

let test = {
open Callbacks;
();
34;
};

/* Even though these braces aren't needed we will preserve them because they
* were requested */
let test = {
open Callbacks;
open String;
34;
};

/* You can have unecessary braces around inline opens even */
let test = {
Callbacks.(String.(34));
};

/* Even though these braces aren't needed we will preserve them because they
* were requested */
let test = {
open Callbacks;
String.("hello" ++ "!");
};

let test = {
open Callbacks;
[@attr] String.("hello" ++ "!");
};

/* Doesn't currently parse.
let test = {
[@attr1]
open Callbacks;
String.("hello" ++ "!");
};
*/
let test = {
Callbacks.(
{
let f = 0;
f;
}
);
};

let test =
Callbacks.(
{
let f = 0;
f;
}
);
22 changes: 15 additions & 7 deletions formatTest/typeCheckedTests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,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 @@ -174,8 +177,9 @@ let notReallyJSX = (~foo, ~bar, children) => {
displayName: "test",
};

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

/* end of setup */
let (/><) = (a, b) => a + b;
Expand Down Expand Up @@ -465,7 +469,7 @@ let div = (~children) => 1;

[@JSX] ((() => div)())(~children=[]);

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

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

let myFun = () =>
let myFun = () => {
<>
<Namespace.Foo
intended=true
Expand All @@ -510,6 +517,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 @@ -13,12 +13,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 @@ -49,10 +55,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 @@ -66,9 +74,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 @@ -134,8 +148,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
Loading