Skip to content

Commit

Permalink
Add support for SynExpr.LibraryOnlyStaticOptimization. Fixes #1769. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Jun 27, 2021
1 parent 2d2b0ca commit afc6ead
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="ColMultilineItemTests.fs" />
<Compile Include="BarBeforeDiscriminatedUnionDeclarationTests.fs" />
<Compile Include="SpaceBeforeColonTests.fs" />
<Compile Include="LibraryOnlySynExprTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Extras\Fantomas.Extras.fsproj" />
Expand Down
22 changes: 22 additions & 0 deletions src/Fantomas.Tests/LibraryOnlySynExprTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Fantomas.Tests.LibraryOnlySynExprTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

[<Test>]
let ``SynExpr.LibraryOnlyStaticOptimization`` () =
formatSourceString
false
"""
let FromZero () : 'T =
(get32 0 :?> 'T) when 'T : BigInteger = BigInteger.Zero
"""
config
|> prepend newline
|> should
equal
"""
let FromZero () : 'T =
(get32 0 :?> 'T) when 'T: BigInteger = BigInteger.Zero
"""
23 changes: 23 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,13 @@ and genExpr astContext synExpr ctx =
r.EndLine
(r.EndColumn + 1)
)

| LibraryOnlyStaticOptimization (optExpr, constraints, e) ->
genExpr astContext optExpr
+> genSynStaticOptimizationConstraint astContext constraints
+> sepEq
+> sepSpaceOrNlnIfExpressionExceedsPageWidth (genExpr astContext e)

| UnsupportedExpr r ->
raise
<| FormatException(
Expand Down Expand Up @@ -5540,6 +5547,22 @@ and genConstBytes (bytes: byte []) (r: Range) =
| None -> !-(sprintf "%A" bytes)
<| ctx

and genSynStaticOptimizationConstraint
(astContext: ASTContext)
(constraints: SynStaticOptimizationConstraint list)
: Context -> Context =
let genConstraint astContext con =
match con with
| SynStaticOptimizationConstraint.WhenTyparTyconEqualsTycon (t1, t2, _) ->
genTypar astContext t1
+> sepColon
+> sepSpace
+> genType astContext false t2
| SynStaticOptimizationConstraint.WhenTyparIsStruct (t, _) -> genTypar astContext t

!- " when "
+> col sepSpace constraints (genConstraint astContext)

and genTriviaFor (mainNodeName: FsAstType) (range: Range) f ctx =
(enterNodeFor mainNodeName range
+> f
Expand Down
8 changes: 6 additions & 2 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,10 +1092,14 @@ let (|ILEmbedded|_|) =
| SynExpr.LibraryOnlyILAssembly (_, _, _, _, r) -> Some(r)
| _ -> None

let (|LibraryOnlyStaticOptimization|_|) (e: SynExpr) =
match e with
| SynExpr.LibraryOnlyStaticOptimization (constraints, e, optExpr, _) -> Some(optExpr, constraints, e)
| _ -> None

let (|UnsupportedExpr|_|) =
function
// Temprorarily ignore these cases not often used outside FSharp.Core
| SynExpr.LibraryOnlyStaticOptimization (_, _, _, r)
// Temporarily ignore these cases not often used outside FSharp.Core
| SynExpr.LibraryOnlyUnionCaseFieldGet (_, _, _, r)
| SynExpr.LibraryOnlyUnionCaseFieldSet (_, _, _, _, r) -> Some r
| _ -> None
Expand Down

0 comments on commit afc6ead

Please sign in to comment.