Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use workaround from 628 for nested modules, too #2870

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 80 additions & 0 deletions src/Fantomas.Core.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3206,3 +3206,83 @@ fun config ->
#if LOGGING_DEBUG
#endif
"""

[<Test>]
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
"""

[<Test>]
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
"""

[<Test>]
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
"""
23 changes: 16 additions & 7 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down