Skip to content
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
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,11 +40,51 @@ 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
|> Array.head
|> fst

Assert.True(oak.ModulesOrNamespaces.[0].HasContentAfter)

[<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 false 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