Skip to content

Commit

Permalink
Preserve trivia after attributes before name of nested module. Fixes f…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored and jindraivanek committed Mar 30, 2022
1 parent d2616fd commit fe71526
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,58 @@ let x: int = 99
// bar again, cuz why not
let x: int = 99
"""

[<Test>]
let ``comment between attribute and nested module, 2016`` () =
formatSourceString
false
"""
[<AutoOpen>]
// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
module AsyncOptionCEExtensions =
type AsyncOptionBuilder with
member inline __.Source(s: #seq<_>) = s
"""
config
|> prepend newline
|> should
equal
"""
[<AutoOpen>]
// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
module AsyncOptionCEExtensions =
type AsyncOptionBuilder with
member inline __.Source(s: #seq<_>) = s
"""

[<Test>]
let ``comment between attribute and nested module, signature file`` () =
formatSourceString
true
"""
[<AutoOpen>]
// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
module AsyncOptionCEExtensions =
type AsyncOptionBuilder with
member inline Source : string -> string
"""
config
|> prepend newline
|> should
equal
"""
[<AutoOpen>]
// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
module AsyncOptionCEExtensions =
type AsyncOptionBuilder with
member inline Source: string -> string
"""
16 changes: 16 additions & 0 deletions src/Fantomas/AstExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ type SynField with
| SynField (attributes = _ :: _; idOpt = None) -> None
| SynField (attributes = attrs; idOpt = Some ident) -> hasLinesBetweenAttributesAndFirstNode attrs ident.idRange

type SynModuleDecl with
member this.AfterAttributesBeforeNestedModuleName: Range option =
match this with
| SynModuleDecl.NestedModule(moduleInfo = SynComponentInfo(attributes = [])) -> None
| SynModuleDecl.NestedModule(moduleInfo = SynComponentInfo (attributes = attrs; longId = lid :: _)) ->
hasLinesBetweenAttributesAndFirstNode attrs lid.idRange
| _ -> None

type SynModuleSigDecl with
member this.AfterAttributesBeforeNestedModuleName: Range option =
match this with
| SynModuleSigDecl.NestedModule(moduleInfo = SynComponentInfo(attributes = [])) -> None
| SynModuleSigDecl.NestedModule(moduleInfo = SynComponentInfo (attributes = attrs; longId = lid :: _)) ->
hasLinesBetweenAttributesAndFirstNode attrs lid.idRange
| _ -> None

// TODO: Remove when https://github.com/dotnet/fsharp/pull/12441 is part of FCS
type SynExceptionDefnRepr with
member this.FullRange: range =
Expand Down
12 changes: 12 additions & 0 deletions src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ module private Ast =
decls |> List.map visit

let finalContinuation (nodes: TriviaNodeAssigner list list) : TriviaNodeAssigner list =
let afterAttributesBeforeNestedModule =
ast.AfterAttributesBeforeNestedModuleName
|> Option.map (mkNode SynModuleDecl_NestedModule_AfterAttributesBeforeModuleName)
|> Option.toList

[ mkNode SynModuleDecl_NestedModule range
yield! afterAttributesBeforeNestedModule
yield! visitSynComponentInfo sci
yield! (List.collect id nodes) ]
|> finalContinuation
Expand Down Expand Up @@ -1349,7 +1355,13 @@ module private Ast =
List.map visit decls

let finalContinuation (nodes: TriviaNodeAssigner list list) : TriviaNodeAssigner list =
let afterAttributesBeforeNestedModule =
ast.AfterAttributesBeforeNestedModuleName
|> Option.map (mkNode SynModuleSigDecl_NestedModule_AfterAttributesBeforeModuleName)
|> Option.toList

[ yield mkNode SynModuleSigDecl_NestedModule range
yield! afterAttributesBeforeNestedModule
yield! visitSynComponentInfo sci
yield! (List.collect id nodes) ]
|> finalContinuation
Expand Down
9 changes: 8 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ and genModuleDecl astContext (node: SynModuleDecl) =
| NestedModule (ats, px, ao, s, isRecursive, mds) ->
genPreXmlDoc px
+> genAttributes astContext ats
+> genAfterAttributesBefore
SynModuleDecl_NestedModule_AfterAttributesBeforeModuleName
node.AfterAttributesBeforeNestedModuleName
+> (!- "module ")
+> opt sepSpace ao genAccess
+> ifElse isRecursive (!- "rec ") sepNone
Expand Down Expand Up @@ -419,7 +422,11 @@ and genSigModuleDecl astContext node =
| SigModuleAbbrev (s1, s2) -> !- "module " -- s1 +> sepEq +> sepSpace -- s2
| SigNamespaceFragment m -> failwithf "NamespaceFragment is not supported yet: %O" m
| SigNestedModule (ats, px, ao, s, mds) ->
genPreXmlDoc px +> genAttributes astContext ats
genPreXmlDoc px
+> genAttributes astContext ats
+> genAfterAttributesBefore
SynModuleSigDecl_NestedModule_AfterAttributesBeforeModuleName
node.AfterAttributesBeforeNestedModuleName
-- "module "
+> opt sepSpace ao genAccess
-- s
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type FsAstType =
// | SynModuleOrNamespace_NamedModule
| SynModuleDecl_ModuleAbbrev
| SynModuleDecl_NestedModule
| SynModuleDecl_NestedModule_AfterAttributesBeforeModuleName
| SynModuleDecl_Let
| SynModuleDecl_DoExpr
| SynModuleDecl_Types
Expand Down Expand Up @@ -343,6 +344,7 @@ type FsAstType =
// | SynModuleOrNamespaceSig_NamedModule
| SynModuleSigDecl_ModuleAbbrev
| SynModuleSigDecl_NestedModule
| SynModuleSigDecl_NestedModule_AfterAttributesBeforeModuleName
| SynModuleSigDecl_Types
| SynModuleSigDecl_Open
| SynModuleSigDecl_OpenType
Expand Down

0 comments on commit fe71526

Please sign in to comment.