From 919491f9dac53949cb7ceaef2d3a7fc279bc3d76 Mon Sep 17 00:00:00 2001 From: nojaf Date: Sat, 30 Mar 2024 10:55:57 +0100 Subject: [PATCH] Check if expression is isArrayOrListWithHashDirectiveBeforeClosingBracket in indentSepNlnUnindentUnlessStroustrup --- CHANGELOG.md | 5 ++++ .../SynBindingFunctionExpressionTests.fs | 25 +++++++++++++++++++ src/Fantomas.Core/Context.fs | 9 ++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 414994aaf7..c7afb9bf63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### 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..c9813448ed 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs @@ -95,6 +95,31 @@ 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 ``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