Skip to content

Commit

Permalink
Style guide lambda (fsprojects#1858)
Browse files Browse the repository at this point in the history
* WIP lambda as last argument

* Update remaining test according to style proposal.

* Add test for multiline non lambda argument.

* Print trivia for function keyword from ast range.

* multiline non lambda argument, match lambda
  • Loading branch information
nojaf authored and jindraivanek committed Mar 30, 2022
1 parent 04e8156 commit 990c284
Show file tree
Hide file tree
Showing 24 changed files with 893 additions and 661 deletions.
109 changes: 49 additions & 60 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Fantomas.Tests.TestHelper

// the current behavior results in a compile error since the |> is merged to the last line
[<Test>]
let ``no nln before lambda #503`` () =
let ``no nln before lambda, #503`` () =
formatSourceString
false
"""
Expand All @@ -22,11 +22,10 @@ let a =
"""
let a =
b
|> List.exists
(fun p ->
p.a
&& p.b
|> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
|> List.exists (fun p ->
p.a
&& p.b
|> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
"""

// compile error due to expression starting before the beginning of the function expression
Expand Down Expand Up @@ -127,21 +126,19 @@ module Caching =
(address: PublicAddress)
(currency: Currency)
: NotFresh<decimal> =
lock
cacheFiles.CachedNetworkData
(fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
compoundBalance
None
currency
address
sessionCachedNetworkData
())
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
compoundBalance
None
currency
address
sessionCachedNetworkData
())
()
"""
Expand Down Expand Up @@ -174,17 +171,15 @@ module Caching =
(address: PublicAddress)
(currency: Currency)
: NotFresh<decimal> =
lock
cacheFiles.CachedNetworkData
(fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
())
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
())
()
"""
Expand Down Expand Up @@ -214,16 +209,14 @@ let ``should not split parameters over multiple lines when they do not exceed pa
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency) : NotFresh<decimal> =
lock
cacheFiles.CachedNetworkData
(fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance None currency address sessionCachedNetworkData
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance None currency address sessionCachedNetworkData
())
())
()
"""
Expand Down Expand Up @@ -253,16 +246,14 @@ let ``should not split single parameter over multiple lines when it does not exc
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency) : NotFresh<decimal> =
lock
cacheFiles.CachedNetworkData
(fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance
())
())
()
"""
Expand Down Expand Up @@ -737,16 +728,14 @@ module Foo =
let blah =
it
|> List.iter
(fun (_, output) ->
thing
|> Map.iter
(fun key value ->
match value with
| Ok (TestResult.Failure f) -> failwith ""
| Error e -> failwith ""
| _ -> () // hi!
))
|> List.iter (fun (_, output) ->
thing
|> Map.iter (fun key value ->
match value with
| Ok (TestResult.Failure f) -> failwith ""
| Error e -> failwith ""
| _ -> () // hi!
))
"""

[<Test>]
Expand Down
16 changes: 6 additions & 10 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ let print_30_permut () =
/// declare and initialize
let permutation: int array =
Array.init
n
(fun i ->
Console.Write(i + 1)
i)
Array.init n (fun i ->
Console.Write(i + 1)
i)
permutation
"""
Expand All @@ -81,11 +79,9 @@ let print_30_permut () =
/// declare and initialize
let permutation: int array =
Array.init
n
(fun (i, j) ->
Console.Write(i + 1)
i)
Array.init n (fun (i, j) ->
Console.Write(i + 1)
i)
permutation
"""
Expand Down
74 changes: 35 additions & 39 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,10 @@ type FunctionComponent =
static member inline Lazy(f: 'Props -> ReactElement, fallback: ReactElement) : LazyFunctionComponent<'Props> =
#if FABLE_COMPILER
let elemType =
ReactBindings.React.``lazy``
(fun () ->
// React.lazy requires a default export
(importValueDynamic f)
.``then`` (fun x -> createObj [ "default" ==> x ]))
ReactBindings.React.``lazy`` (fun () ->
// React.lazy requires a default export
(importValueDynamic f)
.``then`` (fun x -> createObj [ "default" ==> x ]))
fun props ->
ReactElementType.create
Expand Down Expand Up @@ -892,11 +891,10 @@ type FunctionComponent =
static member inline Lazy(f: 'Props -> ReactElement, fallback: ReactElement) : LazyFunctionComponent<'Props> =
#if FABLE_COMPILER
let elemType =
ReactBindings.React.``lazy``
(fun () ->
// React.lazy requires a default export
(importValueDynamic f)
.``then`` (fun x -> createObj [ "default" ==> x ]))
ReactBindings.React.``lazy`` (fun () ->
// React.lazy requires a default export
(importValueDynamic f)
.``then`` (fun x -> createObj [ "default" ==> x ]))
fun props ->
ReactElementType.create
Expand Down Expand Up @@ -1255,13 +1253,12 @@ let ``endif in lambda`` () =
|> should
equal
"""
foo
(fun x ->
()
foo (fun x ->
()
#if DEF
()
()
#endif
)
)
"""

[<Test>]
Expand Down Expand Up @@ -2216,41 +2213,40 @@ let getDefaultProxyFor =
equal
"""
let getDefaultProxyFor =
memoize
(fun (url: string) ->
let uri = Uri url
memoize (fun (url: string) ->
let uri = Uri url
let getDefault () =
let getDefault () =
#if CUSTOM_WEBPROXY
let result =
{ new IWebProxy with
member __.Credentials = null
let result =
{ new IWebProxy with
member __.Credentials = null
member __.Credentials
with set _value = ()
member __.Credentials
with set _value = ()
member __.GetProxy _ = null
member __.IsBypassed(_host: Uri) = true }
member __.GetProxy _ = null
member __.IsBypassed(_host: Uri) = true }
#else
let result = WebRequest.GetSystemWebProxy()
let result = WebRequest.GetSystemWebProxy()
#endif
#if CUSTOM_WEBPROXY
let proxy = result
let proxy = result
#else
let address = result.GetProxy uri
let address = result.GetProxy uri
if address = uri then
null
else
let proxy = WebProxy address
proxy.BypassProxyOnLocal <- true
if address = uri then
null
else
let proxy = WebProxy address
proxy.BypassProxyOnLocal <- true
#endif
proxy.Credentials <- CredentialCache.DefaultCredentials
proxy
proxy.Credentials <- CredentialCache.DefaultCredentials
proxy
match calcEnvProxies.Force().TryFind uri.Scheme with
| Some p -> if p.GetProxy uri <> uri then p else getDefault ()
| None -> getDefault ())
match calcEnvProxies.Force().TryFind uri.Scheme with
| Some p -> if p.GetProxy uri <> uri then p else getDefault ()
| None -> getDefault ())
"""

[<Test>]
Expand Down
67 changes: 28 additions & 39 deletions src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,23 +1668,21 @@ let initDb () =
let createSql = readSqlFile "create"
using
(connection ())
(fun conn ->
task {
do! conn.OpenAsync()
let! _ = conn.ExecuteAsync(createSql)
using (connection ()) (fun conn ->
task {
do! conn.OpenAsync()
let! _ = conn.ExecuteAsync(createSql)
#if DEBUG
let! hasClients = hasClients ()
let! hasClients = hasClients ()
if not (hasClients) then
let seedSql = readSqlFile "seed"
let! _ = conn.ExecuteAsync(seedSql)
()
#else
if not (hasClients) then
let seedSql = readSqlFile "seed"
let! _ = conn.ExecuteAsync(seedSql)
()
#else
()
#endif
})
})
"""

[<Test>]
Expand Down Expand Up @@ -1763,32 +1761,23 @@ let ``don't add extra newline before do bang`` () =
"""
let sendPushNotifications =
allSubscriptions
|> List.map
(fun (user, subscriptions) ->
subscriptions
|> List.filter (fun s -> s.Origin = origin)
|> List.map
(fun s ->
task {
try
let ps =
PushSubscription(s.Endpoint, s.P256DH, s.Auth)
do! webPushClient.SendNotificationAsync(ps, payload, vapidDetails)
with
| :? WebPushException as wpex ->
log.LogError(sprintf "Couldn't send notification to %s, %A" user.UserId wpex)
do!
filterSubscriptionsAndPersist
managementToken
user.UserId
subscriptions
s.Origin
s.Endpoint
}
:> Task)
|> Task.WhenAll)
|> List.map (fun (user, subscriptions) ->
subscriptions
|> List.filter (fun s -> s.Origin = origin)
|> List.map (fun s ->
task {
try
let ps =
PushSubscription(s.Endpoint, s.P256DH, s.Auth)
do! webPushClient.SendNotificationAsync(ps, payload, vapidDetails)
with
| :? WebPushException as wpex ->
log.LogError(sprintf "Couldn't send notification to %s, %A" user.UserId wpex)
do! filterSubscriptionsAndPersist managementToken user.UserId subscriptions s.Origin s.Endpoint
}
:> Task)
|> Task.WhenAll)
"""

[<Test>]
Expand Down
Loading

0 comments on commit 990c284

Please sign in to comment.