Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich transformed Oak. #2869

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Fantomas.Core.Tests/CodeFormatterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,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