Skip to content

Commit

Permalink
Add parenthesis when type abbreviation is a tuple struct. Fixes #605 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Dec 26, 2019
1 parent 4e471fe commit 24a9fd1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/Fantomas.Tests/StructTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,23 @@ let t = struct (1, 2)
match t with
| struct (x, y) -> ()
"""

[<Test>]
let ``struct tuple type abbreviation, 605`` () =
formatSourceString false "type TupleStruct = (struct (string * string))" config
|> prepend newline
|> should equal """
type TupleStruct = (struct (string * string))
"""

[<Test>]
let ``struct tuple type abbreviation, sigfile`` () =
formatSourceString true """namespace meh
type TupleStruct = (struct (string * string))""" config
|> prepend newline
|> should equal """
namespace meh
type TupleStruct = (struct (string * string))
"""
26 changes: 23 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,16 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s, preferPos
| Simple TDSRNone ->
typeName
| Simple(TDSRTypeAbbrev t) ->
let genTypeAbbrev = genType astContext false t
let genTypeAbbrev =
let needsParenthesis =
match t with
| SynType.Tuple(isStruct, typeNames, _) ->
(isStruct && List.length typeNames > 1)
| _ -> false

ifElse needsParenthesis sepOpenT sepNone +>
genType astContext false t +>
ifElse needsParenthesis sepCloseT sepNone

let genMembers =
ifElse (List.isEmpty ms)
Expand Down Expand Up @@ -1703,8 +1712,19 @@ and genSigTypeDefn astContext (SigTypeDef(ats, px, ao, tds, tcs, tdr, ms, s, pre

| SigSimple TDSRNone ->
typeName
| SigSimple(TDSRTypeAbbrev t) ->
typeName +> sepEq +> sepSpace +> genType astContext false t
| SigSimple(TDSRTypeAbbrev t) ->
let genTypeAbbrev =
let needsParenthesis =
match t with
| SynType.Tuple(isStruct, typeNames, _) ->
(isStruct && List.length typeNames > 1)
| _ -> false

ifElse needsParenthesis sepOpenT sepNone +>
genType astContext false t +>
ifElse needsParenthesis sepCloseT sepNone

typeName +> sepEq +> sepSpace +> genTypeAbbrev
| SigSimple(TDSRException(ExceptionDefRepr(ats, px, ao, uc))) ->
genExceptionBody astContext ats px ao uc

Expand Down

0 comments on commit 24a9fd1

Please sign in to comment.