From a968f113b82dac925b9edee67da88cb5b179040b Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Wed, 28 Dec 2022 10:02:17 +0100 Subject: [PATCH] Correct the range for recursive types (#2669) * Correct range for recursive type with attributes. * Add regression test for xml doc before recursive type. --- .../TypeDeclarationTests.fs | 73 +++++++++++++++++++ src/Fantomas.Core/CodePrinter2.fs | 2 +- src/Fantomas.Core/Fangorn.fs | 2 + 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs index 97bf3cdf7e..42bb670189 100644 --- a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs @@ -3336,3 +3336,76 @@ type MyType ) = let x = 5 """ + +[] +let ``trivia before attributes in recursive type, 2361 `` () = + formatSourceString + false + """ +module Primitives = + type BlockHeight = + | BlockHeight of uint32 + + and +#if !NoDUsAsStructs + [] +#endif + BlockHeightOffset16 = + | BlockHeightOffset16 of uint16 + +""" + config + |> prepend newline + |> should + equal + """ +module Primitives = + type BlockHeight = BlockHeight of uint32 + + and +#if !NoDUsAsStructs + [] +#endif + BlockHeightOffset16 = BlockHeightOffset16 of uint16 +""" + +[] +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 + [] +#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 + [] +#endif + BlockHeightOffset16 = BlockHeightOffset16 of uint16 +""" diff --git a/src/Fantomas.Core/CodePrinter2.fs b/src/Fantomas.Core/CodePrinter2.fs index ba2002cad1..cc22313f3a 100644 --- a/src/Fantomas.Core/CodePrinter2.fs +++ b/src/Fantomas.Core/CodePrinter2.fs @@ -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) diff --git a/src/Fantomas.Core/Fangorn.fs b/src/Fantomas.Core/Fangorn.fs index 8e4e7655af..6401fe1131 100644 --- a/src/Fantomas.Core/Fangorn.fs +++ b/src/Fantomas.Core/Fangorn.fs @@ -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