Skip to content

Commit

Permalink
Expose TransformAST api. (#2868)
Browse files Browse the repository at this point in the history
* Expose TransformAST api.

* Add additional unit test.

* Revert changelog entry
  • Loading branch information
nojaf committed Jun 2, 2023
1 parent f95faa9 commit b2a67ba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/Fantomas.Core.Tests/CodeFormatterTests.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Fantomas.Core.Tests.CodeFormatterTests

open Fantomas.Core.SyntaxOak
open NUnit.Framework
open Fantomas.Core
open Fantomas.Core.Tests.TestHelpers
Expand Down Expand Up @@ -39,7 +40,7 @@ let main _ =
|> ignore

[<Test>]
let ``trivia is parsed for Oak`` () =
let ``trivia is transformed to Oak`` () =
let oak =
CodeFormatter.ParseOakAsync(false, "let a = 0\n // foo")
|> Async.RunSynchronously
Expand Down Expand Up @@ -67,3 +68,43 @@ let ``parsed oak can be formatted back to source`` () =
|> Async.RunSynchronously

Assert.AreEqual(source, formatted)

[<Test>]
let ``transform parsedInput to Oak`` () =
let source =
"""
module A
#if DEBUG
let b = 0
#endif
"""

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

let oak = CodeFormatter.TransformAST(ast, source)

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding _ -> Assert.Pass()
| _ -> Assert.Fail()

[<Test>]
let ``transform parsedInput created with additional defines to Oak`` () =
let source =
"""
module A
#if DEBUG
let b = 0
#endif
"""

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

let oak = CodeFormatter.TransformAST ast

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding _ -> Assert.Pass()
| _ -> Assert.Fail()
5 changes: 5 additions & 0 deletions src/Fantomas.Core/CodeFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type CodeFormatter =
oak, defines.Value)
}

static member TransformAST ast = ASTTransformer.mkOak None ast

static member TransformAST(ast, source) =
ASTTransformer.mkOak (Some(SourceText.ofString source)) ast

static member FormatOakAsync(oak: Oak) : Async<string> =
async {
let context = Context.Context.Create FormatConfig.Default
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Core/CodeFormatter.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ type CodeFormatter =
/// Parse a source string to SyntaxOak
static member ParseOakAsync: isSignature: bool * source: string -> Async<(Oak * string list) array>

/// Transform a ParsedInput to an Oak
static member TransformAST: ast: ParsedInput -> Oak

/// Transform a ParsedInput to an Oak
static member TransformAST: ast: ParsedInput * source: string -> Oak

/// Format SyntaxOak to string
static member FormatOakAsync: oak: Oak -> Async<string>

Expand Down

0 comments on commit b2a67ba

Please sign in to comment.