Skip to content

Commit

Permalink
Check Hash Directive more strictly inside of lists or arrays (#3080)
Browse files Browse the repository at this point in the history
* Check Hash Directive more carefully inside of lists or arrays

* Add changelog entry

---------

Co-authored-by: nojaf <[email protected]>
  • Loading branch information
Linschlager and nojaf committed Apr 16, 2024
1 parent d6620be commit 873d9d7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 6.3.4 - 2024-04-16

### Fixed
* Regression: An empty line or comment at the end of a list breaks Stroustrup formatting. [#3079](https://github.com/fsprojects/fantomas/issues/3079)

## 6.3.3 - 2024-04-12

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,6 @@ let x y = [
]
"""

[<Test>]
let ``hash directive before closing list bracket, 3070`` () =
formatSourceString
"""
let private knownProviders = [
#if !FABLE_COMPILER
(SerilogProvider.isAvailable, SerilogProvider.create)
(MicrosoftExtensionsLoggingProvider.isAvailable, MicrosoftExtensionsLoggingProvider.create)
#endif
]
"""
config
|> prepend newline
|> should
equal
"""
let private knownProviders =
[
#if !FABLE_COMPILER
(SerilogProvider.isAvailable, SerilogProvider.create)
(MicrosoftExtensionsLoggingProvider.isAvailable, MicrosoftExtensionsLoggingProvider.create)
#endif
]
"""

[<Test>]
let ``hash directive before closing list bracket, nested let binding`` () =
formatSourceString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,99 @@ let b = // Build an inbound for the specified subnet.
Tags = Map.empty
}
"""

[<Test>]
let ``hash directive before closing list bracket, 3070`` () =
formatSourceString
"""
let private knownProviders = [
#if !FABLE_COMPILER
(SerilogProvider.isAvailable, SerilogProvider.create)
(MicrosoftExtensionsLoggingProvider.isAvailable, MicrosoftExtensionsLoggingProvider.create)
#endif
]
"""
config
|> prepend newline
|> should
equal
"""
let private knownProviders =
[
#if !FABLE_COMPILER
(SerilogProvider.isAvailable, SerilogProvider.create)
(MicrosoftExtensionsLoggingProvider.isAvailable, MicrosoftExtensionsLoggingProvider.create)
#endif
]
"""

[<Test>]
let ``empty line before closing list bracket, 3079`` () =
formatSourceString
"""
let list = [
someItem
]
"""
config
|> prepend newline
|> should
equal
"""
let list = [
someItem
]
"""

[<Test>]
let ``comment before closing list bracket, 3079`` () =
formatSourceString
"""
let list = [
someItem
// comment
]
"""
config
|> prepend newline
|> should
equal
"""
let list = [
someItem
// comment
]
"""

[<Test>]
let ``comment before closing list bracket with hash directive`` () =
formatSourceString
"""
let list = [
someItem
#if something
item1
#else
item2
#endif
// comment
]
"""
config
|> prepend newline
|> should
equal
"""
let list =
[
someItem
#if something
item1
#else
item2
#endif
// comment
]
"""
7 changes: 6 additions & 1 deletion src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,12 @@ let indentSepNlnUnindentUnlessStroustrup f (e: Expr) (ctx: Context) =
let shouldUseStroustrup =
let isArrayOrListWithHashDirectiveBeforeClosingBracket () =
match e with
| Expr.ArrayOrList node -> Seq.isEmpty node.Closing.ContentBefore
| Expr.ArrayOrList node ->
node.Closing.ContentBefore
|> Seq.forall (fun x ->
match x.Content with
| TriviaContent.Directive _ -> false
| _ -> true)
| _ -> true

isStroustrupStyleExpr ctx.Config e
Expand Down

0 comments on commit 873d9d7

Please sign in to comment.