Skip to content

Commit

Permalink
Fix regressions (#1264)
Browse files Browse the repository at this point in the history
* Fixed regression with named or patterns in match clauses.

* Fix multiline if branch expression.

* Bump to 4.3.0-alpha-007
  • Loading branch information
nojaf authored Nov 21, 2020
1 parent 5659cc2 commit f66f4a1
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 33 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.3.0-alpha-007 - 11/2020

* Fix regressions. [#1264](https://github.com/fsprojects/fantomas/pull/1264)

### 4.3.0-alpha-006 - 11/2020

* Feature Clarify constructors. [#1217](https://github.com/fsprojects/fantomas/issues/1217)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<Version>4.3.0-alpha-006</Version>
<Version>4.3.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.CoreGlobalTool/Fantomas.CoreGlobalTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<ToolCommandName>fantomas</ToolCommandName>
<PackAsTool>True</PackAsTool>
<Version>4.3.0-alpha-006</Version>
<Version>4.3.0-alpha-007</Version>
<AssemblyName>fantomas-tool</AssemblyName>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Extras/Fantomas.Extras.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.3.0-alpha-006</Version>
<Version>4.3.0-alpha-007</Version>
<Description>Utility package for Fantomas</Description>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>4.3.0-alpha-006</Version>
<Version>4.3.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
<TargetFramework>net5.0</TargetFramework>
<WarningsAsErrors>FS0025</WarningsAsErrors>
Expand Down
40 changes: 40 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,43 @@ let funcs =
let n2Score = modifierScore n2
if n1Score = n2Score then n1.DisplayName.CompareTo n2.DisplayName else n1Score.CompareTo n2Score)
"""

[<Test>]
let ``multiline then clause`` () =
formatSourceString false """
[<EntryPoint>]
let main argv =
let fileToFile (inFile: string) (outFile: string) =
try
use buffer =
if hasByteOrderMark
then new StreamWriter(new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite),
Encoding.UTF8)
else new StreamWriter(outFile)
buffer.Flush()
with exn ->
eprintfn "The following exception occurred while formatting %s: %O" inFile exn
0
""" config
|> prepend newline
|> should equal """
[<EntryPoint>]
let main argv =
let fileToFile (inFile: string) (outFile: string) =
try
use buffer =
if hasByteOrderMark then
new StreamWriter(
new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite),
Encoding.UTF8
)
else
new StreamWriter(outFile)
buffer.Flush()
with exn -> eprintfn "The following exception occurred while formatting %s: %O" inFile exn
0
"""
56 changes: 56 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -821,3 +821,59 @@ let draftToken =
DraftToken.Create kind token
"""

[<Test>]
let ``named pat or in clauses`` () =
formatSourceString false """
let (|MFMember|MFStaticMember|MFConstructor|MFOverride|) (mf: MemberFlags) =
match mf.MemberKind with
| MemberKind.ClassConstructor
| MemberKind.Constructor -> MFConstructor()
| MemberKind.Member
| MemberKind.PropertyGet
| MemberKind.PropertySet
| MemberKind.PropertyGetSet as mk ->
if mf.IsInstance && mf.IsOverrideOrExplicitImpl
then MFOverride mk
elif mf.IsInstance
then MFMember mk
else MFStaticMember mk
""" config
|> prepend newline
|> should equal """
let (|MFMember|MFStaticMember|MFConstructor|MFOverride|) (mf: MemberFlags) =
match mf.MemberKind with
| MemberKind.ClassConstructor
| MemberKind.Constructor -> MFConstructor()
| MemberKind.Member
| MemberKind.PropertyGet
| MemberKind.PropertySet
| MemberKind.PropertyGetSet as mk ->
if mf.IsInstance && mf.IsOverrideOrExplicitImpl
then MFOverride mk
elif mf.IsInstance
then MFMember mk
else MFStaticMember mk
"""

[<Test>]
let ``named pat or in function syntax`` () =
formatSourceString false """
let rec (|DoExprAttributesL|_|) =
function
| DoExpr _
| Attributes _ as x :: DoExprAttributesL (xs, ys) -> Some(x :: xs, ys)
| DoExpr _
| Attributes _ as x :: ys -> Some([ x ], ys)
| _ -> None
""" config
|> prepend newline
|> should equal """
let rec (|DoExprAttributesL|_|) =
function
| DoExpr _
| Attributes _ as x :: DoExprAttributesL (xs, ys) -> Some(x :: xs, ys)
| DoExpr _
| Attributes _ as x :: ys -> Some([ x ], ys)
| _ -> None
"""
37 changes: 17 additions & 20 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type ASTContext =
/// This is required to correctly detect the setting SpaceBeforeMember
IsMemberDefinition: bool
/// Check whether the context is inside a SynExpr.DotIndexedGet
IsInsideDotIndexed: bool }
IsInsideDotIndexed: bool
/// Inside a SynPat of MatchClause
IsInsideMatchClausePattern: bool }
static member Default =
{ TopLevelModuleName = ""
IsFirstChild = false
Expand All @@ -50,7 +52,8 @@ type ASTContext =
IsFirstTypeParam = false
IsInsideDotGet = false
IsMemberDefinition = false
IsInsideDotIndexed = false }
IsInsideDotIndexed = false
IsInsideMatchClausePattern = false }

let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx: Context) =
match functionOrMethod, arg with
Expand Down Expand Up @@ -2226,7 +2229,7 @@ and genExpr astContext synExpr ctx =
|| futureNlnCheck (!- "if " +> genExpr astContext e1) ctx

let isIfBranchMultiline =
futureNlnCheck (genExpr astContext e2) ctx
futureNlnCheck (!- "then " +> genExpr astContext e2) ctx

let isElseBranchMultiline =
match enOpt with
Expand Down Expand Up @@ -4090,27 +4093,18 @@ and genClause astContext hasBar (Clause (p, e, eo)) =
let arrowRange =
mkRange "arrowRange" p.Range.End e.Range.Start

let pat ctx =
match p with
| PatOrs (ph :: _ as allPats) when (List.length allPats > 1) ->
allPats
|> List.pairwise
|> List.fold (fun acc (prev, current) ->
let barRange =
mkRange "bar range" prev.Range.End current.Range.Start

(sepNln
+> enterNodeTokenByName barRange BAR
+> sepBar
+> genPat astContext current) acc) (genPat astContext ph ctx)
| _ -> genPat astContext p ctx

let body =
optPre (!- " when ") sepNone eo (genExpr astContext)
+> sepArrow
+> leaveNodeTokenByName arrowRange RARROW
+> clauseBody e

let astCtx =
{ astContext with
IsInsideMatchClausePattern = true }

let pat = genPat astCtx p

genTriviaBeforeClausePipe p.Range
+> ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body)

Expand Down Expand Up @@ -4335,7 +4329,10 @@ and genComplexPats astContext node =

and genPatRecordFieldName astContext (PatRecordFieldName (s1, s2, p)) =
ifElse (s1 = "") (!-(sprintf "%s = " s2)) (!-(sprintf "%s.%s = " s1 s2))
+> genPat astContext p
+> genPat
{ astContext with
IsInsideMatchClausePattern = false }
p // see issue 1252.

and genPatWithIdent astContext (ido, p) =
opt (sepEq +> sepSpace) ido (!-)
Expand All @@ -4352,7 +4349,7 @@ and genPat astContext pat =
mkRange "bar range" p1.Range.End p2.Range.Start

genPat astContext p1
+> sepSpace
+> ifElse astContext.IsInsideMatchClausePattern sepNln sepSpace
+> enterNodeTokenByName barRange BAR
-- "| "
+> genPat astContext p2
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.3.0-alpha-006</Version>
<Version>4.3.0-alpha-007</Version>
<Description>Source code formatter for F#</Description>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
8 changes: 0 additions & 8 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,6 @@ let (|PatOr|_|) =
| SynPat.Or (p1, p2, _) -> Some(p1, p2)
| _ -> None

let rec (|PatOrs|_|) =
function
| PatOr (PatOrs pats, p) -> Some(pats @ [ p ])
| PatOr (p, PatOrs pats) -> Some(p :: pats)
| PatOr (p1, p2) -> Some [ p1; p2 ]
| _ -> None

let (|PatAnds|_|) =
function
| SynPat.Ands (ps, _) -> Some ps
Expand Down Expand Up @@ -1148,7 +1141,6 @@ let (|PatTyped|_|) =
| SynPat.Typed (p, t, _) -> Some(p, t)
| _ -> None

// of hier
let (|PatNamed|_|) =
function
| SynPat.Named (p, IdentOrKeyword (OpNameFullInPattern (s, _)), _, ao, _) -> Some(ao, p, s)
Expand Down

0 comments on commit f66f4a1

Please sign in to comment.