From 57dcf8dd2f668f7802cb400157bb491d62271342 Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 2 May 2023 09:59:54 +0200 Subject: [PATCH] Enrich transformed Oak. --- src/Fantomas.Core.Tests/CodeFormatterTests.fs | 19 +++++++++++++++++++ src/Fantomas.Core/CodeFormatter.fs | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Core.Tests/CodeFormatterTests.fs b/src/Fantomas.Core.Tests/CodeFormatterTests.fs index eacfee3824..8d71f006d1 100644 --- a/src/Fantomas.Core.Tests/CodeFormatterTests.fs +++ b/src/Fantomas.Core.Tests/CodeFormatterTests.fs @@ -88,3 +88,22 @@ let b = 0 match oak.ModulesOrNamespaces.[0].Declarations.[0] with | ModuleDecl.TopLevelBinding _ -> Assert.Pass() | _ -> Assert.Fail() + +[] +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() diff --git a/src/Fantomas.Core/CodeFormatter.fs b/src/Fantomas.Core/CodeFormatter.fs index 4db8fb4127..127291b1e7 100644 --- a/src/Fantomas.Core/CodeFormatter.fs +++ b/src/Fantomas.Core/CodeFormatter.fs @@ -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 = async {