From c2d2cfda3a87c9dd273ed7a3c8a0ff5a1a806c7b Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 18 Jun 2024 16:43:26 +0200 Subject: [PATCH 1/2] Update FCS to 'Allow ParsedHashDirectives to take non string arguments', commit 836d4e0603442d6053c8d439993a022501cae494 --- Directory.Build.props | 3 +- src/Fantomas.Core.Tests/HashDirectiveTests.fs | 42 +++++++++++++++++++ src/Fantomas.Core/ASTTransformer.fs | 9 +++- src/Fantomas.Core/CodePrinter.fs | 8 +++- src/Fantomas.Core/SyntaxOak.fs | 9 +++- 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index a23e0c9b5a..7c22db7421 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -40,13 +40,12 @@ Some common use cases include: true true - preview $(OtherFlags) --test:GraphBasedChecking --test:ParallelOptimization --test:ParallelIlxGen --strict-indentation+ - 050271d631956a4e0d0484a583d38236b727a46d + 836d4e0603442d6053c8d439993a022501cae494 diff --git a/src/Fantomas.Core.Tests/HashDirectiveTests.fs b/src/Fantomas.Core.Tests/HashDirectiveTests.fs index a6edba73e7..233b10aee6 100644 --- a/src/Fantomas.Core.Tests/HashDirectiveTests.fs +++ b/src/Fantomas.Core.Tests/HashDirectiveTests.fs @@ -219,3 +219,45 @@ type FSharpTokenizerColorState = | TripleQuoteStringInComment = 14 | InitialState = 0 """ + +[] +let ``#help with string`` () = + formatSourceString + """ +#help "List.map" +""" + config + |> prepend newline + |> should + equal + """ +#help "List.map" +""" + +[] +let ``#help without string`` () = + formatSourceString + """ +#help List.map +""" + config + |> prepend newline + |> should + equal + """ +#help List.map +""" + +[] +let ``#nowarn with integer`` () = + formatSourceString + """ +#nowarn 1182 +""" + config + |> prepend newline + |> should + equal + """ +#nowarn 1182 +""" diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index d653dcc517..69d4cb84a8 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -100,8 +100,13 @@ let mkParsedHashDirective (creationAide: CreationAide) (ParsedHashDirective(iden args |> List.map (function | ParsedHashDirectiveArgument.String(value, stringKind, range) -> - mkConstString creationAide stringKind value range - | ParsedHashDirectiveArgument.SourceIdentifier(identifier, _, range) -> stn identifier range) + mkConstString creationAide stringKind value range |> Choice1Of2 + | ParsedHashDirectiveArgument.SourceIdentifier(identifier, _, range) -> stn identifier range |> Choice1Of2 + | ParsedHashDirectiveArgument.Int32(value, range) -> + let text = creationAide.TextFromSource (fun () -> $"%A{value}") range + stn text range |> Choice1Of2 + | ParsedHashDirectiveArgument.Ident(value = ident) -> mkIdent ident |> Choice1Of2 + | ParsedHashDirectiveArgument.LongIdent(value = lid) -> mkSynLongIdent lid |> Choice2Of2) ParsedHashDirectiveNode(ident, args, range) diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index afc7f76152..38ba58c8c4 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -196,8 +196,12 @@ let addSpaceBeforeParenInPattern (node: IdentListNode) (ctx: Context) = | _ -> sepSpace ctx let genParsedHashDirective (phd: ParsedHashDirectiveNode) = - !- "#" +> !-phd.Ident +> sepSpace +> col sepSpace phd.Args genSingleTextNode - |> genNode phd + let genArg = + function + | Choice1Of2(stn) -> genSingleTextNode stn + | Choice2Of2(idl) -> genIdentListNode idl + + !- "#" +> !-phd.Ident +> sepSpace +> col sepSpace phd.Args genArg |> genNode phd let genUnit (n: UnitNode) = genSingleTextNode n.OpeningParen +> genSingleTextNode n.ClosingParen diff --git a/src/Fantomas.Core/SyntaxOak.fs b/src/Fantomas.Core/SyntaxOak.fs index 037eca3a9a..c13ecc169e 100644 --- a/src/Fantomas.Core/SyntaxOak.fs +++ b/src/Fantomas.Core/SyntaxOak.fs @@ -146,11 +146,16 @@ type Oak(parsedHashDirectives: ParsedHashDirectiveNode list, modulesOrNamespaces override val Children: Node array = [| yield! nodes parsedHashDirectives; yield! nodes modulesOrNamespaces |] -type ParsedHashDirectiveNode(ident: string, args: SingleTextNode list, range) = +type ParsedHashDirectiveNode(ident: string, args: Choice list, range) = inherit NodeBase(range) member val Ident = ident member val Args = args - override val Children: Node array = [| yield! nodes args |] + + override val Children: Node array = + [| for arg in args do + match arg with + | Choice1Of2(node) -> node + | Choice2Of2(node) -> node |] type ModuleOrNamespaceHeaderNode ( From 9159108527e392022fc6764c3a0567dc0e4d113c Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 18 Jun 2024 16:44:51 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecd2bdc709..534673a5fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### Changed +* Update FCS to 'Allow ParsedHashDirectives to take non string arguments', commit 836d4e0603442d6053c8d439993a022501cae494 [#3096](https://github.com/fsprojects/fantomas/pull/3096) + ## 6.3.9 - 2024-06-10 ### Fixed