Skip to content

Commit

Permalink
Further process equals and with keyword range in AST.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 22, 2022
1 parent fb0a0f4 commit d21ec81
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 162 deletions.
25 changes: 25 additions & 0 deletions src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,28 @@ type Subject<'a> private () =
/// Each notification is broadcasted to all subscribed observers.
static member broadcast = new System.Reactive.Subjects.Subject<'a>()
"""

[<Test>]
let ``comments after equals sign in read/write property`` () =
formatSourceString
false
"""
type Foo() =
member this.MyReadWriteProperty
with get () = //comment get
myInternalValue
and set (value) = // comment set
myInternalValue <- value
"""
config
|> prepend newline
|> should
equal
"""
type Foo() =
member this.MyReadWriteProperty
with get () = //comment get
myInternalValue
and set (value) = // comment set
myInternalValue <- value
"""
54 changes: 54 additions & 0 deletions src/Fantomas.Tests/KeepIndentInBranchTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,3 +2147,57 @@ module Foo =
leftSet.SymmetricExceptWith(FooBarBaz.keys rightThings)
|> ignore
"""

[<Test>]
let ``comment after match keyword`` () =
formatSourceString
false
"""
match // comment
Stream.peel rest with
| None -> failwith "oh no"
| Some longName ->
longName
|> Map.map (fun _ -> TypedTerm.force<int>)
"""
config
|> prepend newline
|> should
equal
"""
match // comment
Stream.peel rest with
| None -> failwith "oh no"
| Some longName ->
longName
|> Map.map (fun _ -> TypedTerm.force<int>)
"""

[<Test>]
let ``comment after match bang keyword`` () =
formatSourceString
false
"""
match! // comment
Stream.peel rest with
| None -> failwith "oh no"
| Some longName ->
longName
|> Map.map (fun _ -> TypedTerm.force<int>)
"""
config
|> prepend newline
|> should
equal
"""
match! // comment
Stream.peel rest with
| None -> failwith "oh no"
| Some longName ->
longName
|> Map.map (fun _ -> TypedTerm.force<int>)
"""
38 changes: 38 additions & 0 deletions src/Fantomas.Tests/ModuleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,41 @@ module Input
let modules = [ 109024; 137172; 80445; 80044 ]
"""

[<Test>]
let ``comment after equals sign in named module`` () =
formatSourceString
false
"""
module Foo = // comment
let bar = 9
"""
config
|> prepend newline
|> should
equal
"""
module Foo = // comment
let bar = 9
"""

[<Test>]
let ``comment after equals sign in named module, signature file`` () =
formatSourceString
true
"""
namespace Meh
module Foo = // comment
val bar : int
"""
config
|> prepend newline
|> should
equal
"""
namespace Meh
module Foo = // comment
val bar: int
"""
Original file line number Diff line number Diff line change
Expand Up @@ -1164,3 +1164,41 @@ type Foo =
static member Baz : int
"""

[<Test>]
let ``comment after equals in record field`` () =
formatSourceString
false
"""
{ A = //comment
B }
"""
config
|> prepend newline
|> should
equal
"""
{
A = //comment
B
}
"""

[<Test>]
let ``comment after equals in anonymous record field`` () =
formatSourceString
false
"""
{| A = //comment
B |}
"""
config
|> prepend newline
|> should
equal
"""
{|
A = //comment
B
|}
"""
56 changes: 56 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,3 +2047,59 @@ match!
| None -> false
| Some balance -> someRetrievedBalance = balance
"""

[<Test>]
let ``comment after match keyword`` () =
formatSourceString
false
"""
match // foo
a with
| B b -> ()
"""
config
|> prepend newline
|> should
equal
"""
match // foo
a with
| B b -> ()
"""

[<Test>]
let ``comment after match bang keyword`` () =
formatSourceString
false
"""
match! // foo
a with
| B b -> ()
"""
config
|> prepend newline
|> should
equal
"""
match! // foo
a with
| B b -> ()
"""

[<Test>]
let ``comment after with keyword in match bang`` () =
formatSourceString
false
"""
match!
a with // foo
| B b -> ()
"""
config
|> prepend newline
|> should
equal
"""
match! a with // foo
| B b -> ()
"""
51 changes: 51 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,3 +1894,54 @@ let parse (checker: FSharpChecker) (parsingOptions: FSharpParsingOptions) { File
})
|> Async.Parallel
"""

[<Test>]
let ``comment after equals in record field`` () =
formatSourceString
false
"""
{ A = //comment
B }
"""
config
|> prepend newline
|> should
equal
"""
{ A = //comment
B }
"""

[<Test>]
let ``comment after equals in anonymous record field`` () =
formatSourceString
false
"""
{| A = //comment
B |}
"""
config
|> prepend newline
|> should
equal
"""
{| A = //comment
B |}
"""

[<Test>]
let ``comment after equals sign in record type definition`` () =
formatSourceString
false
"""
type Foo = // comment
{ Bar: int }
"""
config
|> prepend newline
|> should
equal
"""
type Foo = // comment
{ Bar: int }
"""
38 changes: 38 additions & 0 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,44 @@ exception BuildException of string * list<string> with
override x.ToString() = x.Data0.ToString() + "\r\n" + (separated "\r\n" x.Data1)
"""

[<Test>]
let ``comment after with keyword in exception type`` () =
formatSourceString
false
"""
exception FooException with // comment
member this.Bar () = ()
"""
config
|> prepend newline
|> should
equal
"""
exception FooException with // comment
member this.Bar() = ()
"""

[<Test>]
let ``comment after with keyword in exception type in signature files`` () =
formatSourceString
true
"""
namespace Moon
exception FooException with // comment
member Bar: unit -> unit
"""
config
|> prepend newline
|> should
equal
"""
namespace Moon
exception FooException with // comment
member Bar: unit -> unit
"""

[<Test>]
let ``type annotations`` () =
formatSourceString
Expand Down
36 changes: 36 additions & 0 deletions src/Fantomas.Tests/UnionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,39 @@ type TransactionType =
| [<CompiledName "External Credit Balance Refund">] ExternalCreditBalanceRefund
| [<CompiledName "Credit Balance Adjustment (Applied from Credit Balance)">] CreditBalanceAdjustmentAppliedFromCreditBalance
"""

[<Test>]
let ``comment after equals in enum`` () =
formatSourceString
false
"""
type Foo = // comment
| Bar = // other comment
1
"""
config
|> prepend newline
|> should
equal
"""
type Foo = // comment
| Bar = // other comment
1
"""

[<Test>]
let ``comment after equals in union`` () =
formatSourceString
false
"""
type Foo = // comment
| Bar of string * int64
"""
config
|> prepend newline
|> should
equal
"""
type Foo = // comment
| Bar of string * int64
"""
8 changes: 2 additions & 6 deletions src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ module private Ast =
visitSynExpr expr
@ (Option.toList ident |> List.map visitIdent)

and visitRecordField (SynExprRecordField ((fieldName, _), equalsRange, synExprOption, blockSeparator)) =
and visitRecordField (SynExprRecordField ((fieldName, _), equalsRange, synExprOption, _blockSeparator)) =
[ yield mkNode RecordField_ fieldName.Range
yield! mkNodeOption RecordField_Equals equalsRange
yield!
Expand Down Expand Up @@ -937,11 +937,7 @@ module private Ast =

| SynPat.Record (pats, range) ->
let continuations: ((TriviaNodeAssigner list -> TriviaNodeAssigner list) -> TriviaNodeAssigner list) list =
pats
|> List.map (fun (_, equalsRange, pat) ->
fun nodes ->
mkNode SynPat_Record_Field_Equals equalsRange
:: visit pat nodes)
pats |> List.map (fun (_, _, pat) -> visit pat)

let finalContinuation (nodes: TriviaNodeAssigner list list) : TriviaNodeAssigner list =
mkNode SynPat_Record range
Expand Down
Loading

0 comments on commit d21ec81

Please sign in to comment.