Skip to content

Commit

Permalink
Correct the range for recursive types (#2669)
Browse files Browse the repository at this point in the history
* Correct range for recursive type with attributes.

* Add regression test for xml doc before recursive type.
  • Loading branch information
nojaf committed Jan 2, 2023
1 parent 3595dd0 commit a968f11
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/Fantomas.Core.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3336,3 +3336,76 @@ type MyType
) =
let x = 5
"""

[<Test>]
let ``trivia before attributes in recursive type, 2361 `` () =
formatSourceString
false
"""
module Primitives =
type BlockHeight =
| BlockHeight of uint32
and
#if !NoDUsAsStructs
[<Struct>]
#endif
BlockHeightOffset16 =
| BlockHeightOffset16 of uint16
"""
config
|> prepend newline
|> should
equal
"""
module Primitives =
type BlockHeight = BlockHeight of uint32
and
#if !NoDUsAsStructs
[<Struct>]
#endif
BlockHeightOffset16 = BlockHeightOffset16 of uint16
"""

[<Test>]
let ``xml doc before recursive type, 2360`` () =
formatSourceString
false
"""
module Primitives =
type BlockHeight =
| BlockHeight of uint32
/// **Description**
///
/// 16bit relative block height used for `OP_CSV` locks,
/// Since OP_CSV allow only block number of 0 ~ 65535, it is safe
/// to restrict into the range smaller than BlockHeight
and
#if !NoDUsAsStructs
[<Struct>]
#endif
BlockHeightOffset16 =
| BlockHeightOffset16 of uint16
"""
config
|> prepend newline
|> should
equal
"""
module Primitives =
type BlockHeight = BlockHeight of uint32
/// **Description**
///
/// 16bit relative block height used for `OP_CSV` locks,
/// Since OP_CSV allow only block number of 0 ~ 65535, it is safe
/// to restrict into the range smaller than BlockHeight
and
#if !NoDUsAsStructs
[<Struct>]
#endif
BlockHeightOffset16 = BlockHeightOffset16 of uint16
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Core/CodePrinter2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3161,9 +3161,9 @@ let genTypeDefn (td: TypeDefn) =
genXml typeName.XmlDoc
+> onlyIfNot hasAndKeyword (genAttributes typeName.Attributes)
+> genSingleTextNode typeName.LeadingKeyword
+> onlyIf hasTriviaAfterLeadingKeyword indent
+> onlyIf hasAndKeyword (sepSpace +> genOnelinerAttributes typeName.Attributes)
+> sepSpace
+> onlyIf hasTriviaAfterLeadingKeyword indent
+> genAccessOpt typeName.Accessibility
+> genTypeAndParam (genIdentListNode typeName.Identifier) typeName.TypeParameters
+> onlyIfNot typeName.Constraints.IsEmpty (sepSpace +> genTypeConstraints typeName.Constraints)
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas.Core/Fangorn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,8 @@ let mkTypeDefn
let startRange =
if not px.IsEmpty then
px.Range
elif leadingKeyword.Text = "and" then
(leadingKeyword :> Node).Range
else
match ats with
| [] -> (leadingKeyword :> Node).Range
Expand Down

0 comments on commit a968f11

Please sign in to comment.