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

Don't consider attribute above field a proper candidate for trivia #912

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,4 +1306,25 @@ type VersionMismatchDuringDeserializationException(message: string, innerExcepti
type VersionMismatchDuringDeserializationException(message : string,
innerException : System.Exception) =
inherit System.Exception(message, innerException)
"""
"""

[<Test>]
let ``type record declaration with attributes, 910`` () =
formatSourceString false """type Commenter =
{ [<JsonProperty("display_name")>]
DisplayName: string }

type Message =
{ [<JsonProperty("body")>]
Body: string }
""" config
|> prepend newline
|> should equal """
type Commenter =
{ [<JsonProperty("display_name")>]
DisplayName: string }

type Message =
{ [<JsonProperty("body")>]
Body: string }
"""
10 changes: 8 additions & 2 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,25 @@ let private triviaBetweenAttributeAndParentBinding (triviaNodes: TriviaNodeAssig
| MainNode("SynModuleSigDecl.NestedModule")
| MainNode("ValSpfn")
| MainNode("SynMemberDefn.Member")
| MainNode("SynMemberDefn.LetBindings") -> true
| MainNode("SynMemberDefn.LetBindings")
| MainNode("Field") -> true
| _ -> false
)
|> List.pairwise

filteredNodes |> List.tryFind (function
| f, a when (f.Type = MainNode("Field")
&& a.Type = MainNode("SynAttributeList")
&& f.Range.StartLine = a.Range.StartLine
&& a.Range.StartLine + 1 = f.Range.EndLine) -> true
| a, p when (a.Type = MainNode("SynAttributeList") && a.Range.StartLine < line && a.Range.StartLine = a.Range.EndLine) ->
match p.Type with
| MainNode("SynModuleDecl.Let") when (p.Range.StartLine > line) -> true
| MainNode("SynAttributeList") when (p.Range.StartLine > line) -> true
| MainNode("SynModuleDecl.Types") when (p.Range.StartLine > line) -> true
| _ -> false
| _ -> false)
|> Option.map fst
|> Option.bind (fun (a,_) -> if a.Type = MainNode("SynAttributeList") then Some a else None)

let private findASTNodeOfTypeThatContains (nodes: TriviaNodeAssigner list) typeName range =
nodes
Expand Down