diff --git a/CHANGELOG.md b/CHANGELOG.md index 414994aaf7..fa2fae2c37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.3.1 - 2024-03-30 + +### Fixed +* HashDirective before closing `]`. [#3070](https://github.com/fsprojects/fantomas/issues/3070) + ## 6.3.0 - 2024-03-15 ### Miscellaneous diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs index b7efb4c7f9..9daf5ba2fe 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs @@ -95,6 +95,65 @@ let x y = [ ] """ +[] +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 + ] +""" + +[] +let ``hash directive before closing list bracket, nested let binding`` () = + formatSourceString + """ +let foo bar = + let tfms = [ + #if NET6_0_OR_GREATER + "net6.0" + #endif + #if NET7_0_OR_GREATER + "net7.0" + #endif + ] + () +""" + config + |> prepend newline + |> should + equal + """ +let foo bar = + let tfms = + [ +#if NET6_0_OR_GREATER + "net6.0" +#endif +#if NET7_0_OR_GREATER + "net7.0" +#endif + ] + + () +""" + [] let ``synbinding function with array`` () = formatSourceString diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index 31540dcb3f..c2f1e6aca9 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -936,7 +936,14 @@ let addParenIfAutoNln expr f = let indentSepNlnUnindentUnlessStroustrup f (e: Expr) (ctx: Context) = let shouldUseStroustrup = - isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e) ctx + let isArrayOrListWithHashDirectiveBeforeClosingBracket () = + match e with + | Expr.ArrayOrList node -> Seq.isEmpty node.Closing.ContentBefore + | _ -> true + + isStroustrupStyleExpr ctx.Config e + && canSafelyUseStroustrup (Expr.Node e) ctx + && isArrayOrListWithHashDirectiveBeforeClosingBracket () if shouldUseStroustrup then f e ctx