diff --git a/CHANGELOG.md b/CHANGELOG.md index a545abcb4..0f7dd2ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs index 9daf5ba2f..9204fa88e 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynBindingFunctionExpressionTests.fs @@ -95,31 +95,6 @@ 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 diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs index bb5bbae20..7e9ade191 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs @@ -736,3 +736,99 @@ let b = // Build an inbound for the specified subnet. Tags = Map.empty } """ + +[] +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 ``empty line before closing list bracket, 3079`` () = + formatSourceString + """ +let list = [ + someItem + +] +""" + config + |> prepend newline + |> should + equal + """ +let list = [ + someItem + +] +""" + +[] +let ``comment before closing list bracket, 3079`` () = + formatSourceString + """ +let list = [ + someItem + // comment +] +""" + config + |> prepend newline + |> should + equal + """ +let list = [ + someItem +// comment +] +""" + +[] +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 + ] +""" diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index c2f1e6aca..61db82ec5 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -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