diff --git a/CHANGELOG.md b/CHANGELOG.md index b4558db7a6..62f9def8db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### Fixed +* Incorrect indentation of compiler-conditional accessibility modifier for module definition. [#2867](https://github.com/fsprojects/fantomas/issues/2867) + ## [6.0.1] - 2023-04-19 ### Fixed diff --git a/src/Fantomas.Core.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Core.Tests/CompilerDirectivesTests.fs index a908a74cab..460142e85c 100644 --- a/src/Fantomas.Core.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Core.Tests/CompilerDirectivesTests.fs @@ -3206,3 +3206,83 @@ fun config -> #if LOGGING_DEBUG #endif """ + +[] +let ``incorrect indentation of compiler-conditional accessibility modifier for module definition, 2867`` () = + formatSourceString + false + """ +module +#if DEBUG +#else + internal +#endif + A = + let f x = x + x +""" + config + |> prepend newline + |> should + equal + """ +module +#if DEBUG +#else + internal +#endif + A = + let f x = x + x +""" + +[] +let ``incorrect indentation of compiler-conditional accessibility modifier for module definition, no defines`` () = + formatSourceStringWithDefines + [] + """ +module +#if DEBUG +#else + internal +#endif + A = + let f x = x + x +""" + config + |> prepend newline + |> should + equal + """ +module +#if DEBUG +#else + internal +#endif + A = + let f x = x + x +""" + +[] +let ``incorrect indentation of compiler-conditional accessibility modifier for module definition, DEBUG`` () = + formatSourceStringWithDefines + [ "DEBUG" ] + """ +module +#if DEBUG +#else + internal +#endif + A = + let f x = x + x +""" + config + |> prepend newline + |> should + equal + """ +module +#if DEBUG +#else +#endif + A = + let f x = x + x +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 67ea9875af..ef787fa2a0 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -3203,6 +3203,15 @@ let genImplicitConstructor (node: ImplicitConstructorNode) = +> sepSpace) node.Self +let hasTriviaAfterLeadingKeyword (identifier: IdentListNode) (accessibility: SingleTextNode option) = + let beforeAccess = + match accessibility with + | Some n -> n.HasContentBefore + | _ -> false + + let beforeIdentifier = identifier.HasContentBefore + beforeAccess || beforeIdentifier + let genTypeDefn (td: TypeDefn) = let typeDefnNode = TypeDefn.TypeDefnNode td let typeName = typeDefnNode.TypeName @@ -3213,13 +3222,7 @@ let genTypeDefn (td: TypeDefn) = // Workaround for https://github.com/fsprojects/fantomas/issues/628 let hasTriviaAfterLeadingKeyword = - let beforeAccess = - match typeName.Accessibility with - | Some n -> n.HasContentBefore - | _ -> false - - let beforeIdentifier = typeName.Identifier.HasContentBefore - beforeAccess || beforeIdentifier + hasTriviaAfterLeadingKeyword typeName.Identifier typeName.Accessibility genXml typeName.XmlDoc +> onlyIfNot hasAndKeyword (genAttributes typeName.Attributes) @@ -3712,13 +3715,19 @@ let genModuleDecl (md: ModuleDecl) = +> genIdentListNode node.Alias |> genNode (ModuleDecl.Node md) | ModuleDecl.NestedModule node -> + // Workaround for https://github.com/fsprojects/fantomas/issues/2867 + let hasTriviaAfterLeadingKeyword = + hasTriviaAfterLeadingKeyword node.Identifier node.Accessibility + genXml node.XmlDoc +> genAttributes node.Attributes +> genSingleTextNode node.Module +> sepSpace + +> onlyIf hasTriviaAfterLeadingKeyword indent +> genAccessOpt node.Accessibility +> onlyIf node.IsRecursive (sepSpace +> !- "rec" +> sepSpace) +> genIdentListNode node.Identifier + +> onlyIf hasTriviaAfterLeadingKeyword unindent +> sepSpace +> genSingleTextNode node.Equals +> indentSepNlnUnindent (