Skip to content

Commit

Permalink
Update FCS to 'Allow ParsedHashDirectives to take non string argument…
Browse files Browse the repository at this point in the history
…s', commit 836d4e0603442d6053c8d439993a022501cae494
  • Loading branch information
nojaf committed Jun 18, 2024
1 parent 6b1ce03 commit c2d2cfd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ Some common use cases include:
<!-- https://www.gresearch.co.uk/blog/article/improve-nuget-restores-with-static-graph-evaluation/ -->
<RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>
<ServerGarbageCollection>true</ServerGarbageCollection>
<LangVersion>preview</LangVersion>
<OtherFlags>$(OtherFlags) --test:GraphBasedChecking --test:ParallelOptimization --test:ParallelIlxGen --strict-indentation+</OtherFlags>
</PropertyGroup>

<!-- Versions -->
<PropertyGroup>
<FCSCommitHash>050271d631956a4e0d0484a583d38236b727a46d</FCSCommitHash>
<FCSCommitHash>836d4e0603442d6053c8d439993a022501cae494</FCSCommitHash>
</PropertyGroup>

<PropertyGroup>
Expand Down
42 changes: 42 additions & 0 deletions src/Fantomas.Core.Tests/HashDirectiveTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,45 @@ type FSharpTokenizerColorState =
| TripleQuoteStringInComment = 14
| InitialState = 0
"""

[<Test>]
let ``#help with string`` () =
formatSourceString
"""
#help "List.map"
"""
config
|> prepend newline
|> should
equal
"""
#help "List.map"
"""

[<Test>]
let ``#help without string`` () =
formatSourceString
"""
#help List.map
"""
config
|> prepend newline
|> should
equal
"""
#help List.map
"""

[<Test>]
let ``#nowarn with integer`` () =
formatSourceString
"""
#nowarn 1182
"""
config
|> prepend newline
|> should
equal
"""
#nowarn 1182
"""
9 changes: 7 additions & 2 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 6 additions & 2 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SingleTextNode, IdentListNode> 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
(
Expand Down

0 comments on commit c2d2cfd

Please sign in to comment.