Skip to content

Commit

Permalink
Enrich transformed Oak. (#2869)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jun 20, 2023
1 parent 7f58913 commit 62dadb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Fantomas.Core.Tests/CodeFormatterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,22 @@ let b = 0
match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding _ -> Assert.Pass()
| _ -> Assert.Fail()

[<Test>]
let ``transform parsedInput contains trivia in Oak`` () =
let source =
"""
module A
// foo
let b = 0
"""

let ast, _ =
Fantomas.FCS.Parse.parseFile false (FSharp.Compiler.Text.SourceText.ofString source) []

let oak = CodeFormatter.TransformAST(ast, source)

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding node -> Assert.True node.HasContentBefore
| _ -> Assert.Fail()
4 changes: 3 additions & 1 deletion src/Fantomas.Core/CodeFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ type CodeFormatter =
static member TransformAST ast = ASTTransformer.mkOak None ast

static member TransformAST(ast, source) =
ASTTransformer.mkOak (Some(SourceText.ofString source)) ast
let sourceText = SourceText.ofString source
let oak = ASTTransformer.mkOak (Some sourceText) ast
Trivia.enrichTree FormatConfig.Default sourceText ast oak

static member FormatOakAsync(oak: Oak) : Async<string> =
async {
Expand Down

0 comments on commit 62dadb3

Please sign in to comment.