Skip to content

Commit

Permalink
Use module or namespace as FsAstType. Fixes fsprojects#2141. (fsproje…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored and jindraivanek committed Mar 30, 2022
1 parent 1557ec8 commit c5fb3ef
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [4.7.2] - 2022-03-11

### Fixed
* Reordering comments on modules. [#2141](https://github.com/fsprojects/fantomas/issues/2141)

## [4.7.1] - 2022-03-08

### Fixed
Expand Down
125 changes: 125 additions & 0 deletions src/Fantomas.Tests/ModuleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,128 @@ namespace Meh
module Foo = // comment
val bar: int
"""

[<Test>]
let ``comment above named module with xml doc, 2141`` () =
formatSourceString
false
"""
// Boring copyright notice
(* Some other preamble *)
/// This module is amazing it's full of helpful lookup queries
module Queries
"""
config
|> prepend newline
|> should
equal
"""
// Boring copyright notice
(* Some other preamble *)
/// This module is amazing it's full of helpful lookup queries
module Queries
"""

[<Test>]
let ``comment before declared namespace`` () =
formatSourceString
false
"""
// some comment
namespace Blah
let a = 0
"""
config
|> prepend newline
|> should
equal
"""
// some comment
namespace Blah
let a = 0
"""

[<Test>]
let ``comment before global namespace`` () =
formatSourceString
false
"""
// some comment
namespace global
let a = 0
"""
config
|> prepend newline
|> should
equal
"""
// some comment
namespace global
let a = 0
"""

[<Test>]
let ``comment before declared namespace in signature file`` () =
formatSourceString
true
"""
// some comment
namespace Blah
val a : int
"""
config
|> prepend newline
|> should
equal
"""
// some comment
namespace Blah
val a: int
"""

[<Test>]
let ``comment before global namespace in signature file`` () =
formatSourceString
true
"""
// some comment
namespace global
val a : int
"""
config
|> prepend newline
|> should
equal
"""
// some comment
namespace global
val a: int
"""

[<Test>]
let ``comment before named module in signature file`` () =
formatSourceString
true
"""
// some comment
module Meh
val a : int
"""
config
|> prepend newline
|> should
equal
"""
// some comment
module Meh
val a: int
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ type ExtensibleDumper = A | B
let trivias = Map.find [ "DEBUG" ] triviaNodes

match trivias with
| [ { Type = LongIdent_
| [ { Type = SynModuleOrNamespace_DeclaredNamespace
ContentAfter = [ Directive "#if EXTENSIBLE_DUMPER\n#if DEBUG\n\n#endif\n#endif" ] } ] -> pass ()
| _ -> Assert.Fail(sprintf "Unexpected trivia %A" trivias)

Expand Down
15 changes: 13 additions & 2 deletions src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ module private Ast =

let rec visitSynModuleOrNamespace (modOrNs: SynModuleOrNamespace) : TriviaNodeAssigner list =
match modOrNs with
| SynModuleOrNamespace (longIdent, _, kind, decls, _, attrs, _, _range) ->
| SynModuleOrNamespace (longIdent, _, kind, decls, _, attrs, _, range) ->
let moduleOrNamespace =
match kind with
| SynModuleOrNamespaceKind.DeclaredNamespace -> [ mkNode SynModuleOrNamespace_DeclaredNamespace range ]
| SynModuleOrNamespaceKind.GlobalNamespace -> [ mkNode SynModuleOrNamespace_GlobalNamespace range ]
| SynModuleOrNamespaceKind.NamedModule -> [ mkNode SynModuleOrNamespace_NamedModule range ]
| SynModuleOrNamespaceKind.AnonModule -> []

let longIdentNodes =
match kind, decls with
| SynModuleOrNamespaceKind.AnonModule, _ :: _ -> []
| _ -> visitLongIdentIncludingFullRange longIdent

[ yield! longIdentNodes
[ yield! moduleOrNamespace
yield! longIdentNodes
yield! (visitSynAttributeLists attrs)
yield! (decls |> List.collect visitSynModuleDecl) ]

Expand Down Expand Up @@ -1403,6 +1411,9 @@ module private Ast =

let moduleOrNamespaceSig =
match kind with
| SynModuleOrNamespaceKind.DeclaredNamespace ->
[ mkNode SynModuleOrNamespaceSig_DeclaredNamespace range ]
| SynModuleOrNamespaceKind.GlobalNamespace -> [ mkNode SynModuleOrNamespaceSig_GlobalNamespace range ]
| SynModuleOrNamespaceKind.NamedModule -> [ mkNode SynModuleOrNamespaceSig_NamedModule range ]
| _ -> []

Expand Down
11 changes: 9 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ and genModuleOrNamespaceKind (kind: SynModuleOrNamespaceKind) =
| SynModuleOrNamespaceKind.GlobalNamespace -> !- "namespace global"
| SynModuleOrNamespaceKind.AnonModule -> sepNone

and genModuleOrNamespace astContext (ModuleOrNamespace (ats, px, ao, lids, mds, isRecursive, moduleKind)) =
and genModuleOrNamespace astContext (ModuleOrNamespace (ats, px, ao, lids, mds, isRecursive, moduleKind, range)) =
let sepModuleAndFirstDecl =
let firstDecl = List.tryHead mds

Expand All @@ -131,7 +131,7 @@ and genModuleOrNamespace astContext (ModuleOrNamespace (ats, px, ao, lids, mds,

let lidsFullRange =
match lids with
| [] -> range.Zero
| [] -> FSharp.Compiler.Text.range.Zero
| (_, r) :: _ -> Range.unionRanges r (List.last lids |> snd)

let moduleOrNamespace =
Expand All @@ -154,6 +154,11 @@ and genModuleOrNamespace astContext (ModuleOrNamespace (ats, px, ao, lids, mds,
+> ifElse (moduleKind = SynModuleOrNamespaceKind.AnonModule) genTriviaForAnonModuleIdent moduleOrNamespace
+> sepModuleAndFirstDecl
+> genModuleDeclList astContext mds
|> (match moduleKind with
| SynModuleOrNamespaceKind.AnonModule -> id
| SynModuleOrNamespaceKind.DeclaredNamespace -> genTriviaFor SynModuleOrNamespace_DeclaredNamespace range
| SynModuleOrNamespaceKind.GlobalNamespace -> genTriviaFor SynModuleOrNamespace_GlobalNamespace range
| SynModuleOrNamespaceKind.NamedModule -> genTriviaFor SynModuleOrNamespace_NamedModule range)

and genSigModuleOrNamespace astContext (SigModuleOrNamespace (ats, px, ao, lids, mds, isRecursive, moduleKind, range)) =
let sepModuleAndFirstDecl =
Expand Down Expand Up @@ -187,6 +192,8 @@ and genSigModuleOrNamespace astContext (SigModuleOrNamespace (ats, px, ao, lids,
+> sepModuleAndFirstDecl
+> genSigModuleDeclList astContext mds
|> (match moduleKind with
| SynModuleOrNamespaceKind.DeclaredNamespace -> genTriviaFor SynModuleOrNamespaceSig_DeclaredNamespace range
| SynModuleOrNamespaceKind.GlobalNamespace -> genTriviaFor SynModuleOrNamespaceSig_GlobalNamespace range
| SynModuleOrNamespaceKind.NamedModule -> genTriviaFor SynModuleOrNamespaceSig_NamedModule range
| _ -> id)

Expand Down
7 changes: 4 additions & 3 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ let (|ParsedImplFileInput|) (ParsedImplFileInput.ParsedImplFileInput (_, _, _, _
let (|ParsedSigFileInput|) (ParsedSigFileInput.ParsedSigFileInput (_, _, _, hs, mns)) = (hs, mns)

let (|ModuleOrNamespace|)
(SynModuleOrNamespace.SynModuleOrNamespace (LongIdentPieces lids, isRecursive, kind, mds, px, ats, ao, _))
(SynModuleOrNamespace.SynModuleOrNamespace (LongIdentPieces lids, isRecursive, kind, mds, px, ats, ao, range))
=
(ats, px, ao, lids, mds, isRecursive, kind)
(ats, px, ao, lids, mds, isRecursive, kind, range)

let (|SigModuleOrNamespace|)
(SynModuleOrNamespaceSig.SynModuleOrNamespaceSig (LongIdentPieces lids, isRecursive, kind, mds, px, ats, ao, range))
Expand All @@ -251,7 +251,8 @@ let (|SigModuleOrNamespace|)

let (|EmptyFile|_|) (input: ParsedInput) =
match input with
| ImplFile (ParsedImplFileInput (_, [ ModuleOrNamespace (_, _, _, _, [], _, SynModuleOrNamespaceKind.AnonModule) ])) ->
| ImplFile (ParsedImplFileInput (_,
[ ModuleOrNamespace (_, _, _, _, [], _, SynModuleOrNamespaceKind.AnonModule, _) ])) ->
Some input
| SigFile (ParsedSigFileInput (_,
[ SigModuleOrNamespace (_, _, _, _, [], _, SynModuleOrNamespaceKind.AnonModule, _) ])) ->
Expand Down
20 changes: 7 additions & 13 deletions src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ type TriviaIndex = TriviaIndex of int * int
type FsAstType =
| Ident_
| LongIdent_ // namespace or module identifier
// Modules and namespaces cannot really be trusted
// Their range can be influenced by non code constructs (like comments)
// | SynModuleOrNamespace_AnonModule
// | SynModuleOrNamespace_DeclaredNamespace
// | SynModuleOrNamespace_GlobalNamespace
// | SynModuleOrNamespace_NamedModule
// | SynModuleOrNamespace_AnonModule, pick first child node instead
| SynModuleOrNamespace_DeclaredNamespace
| SynModuleOrNamespace_GlobalNamespace
| SynModuleOrNamespace_NamedModule
| SynModuleDecl_ModuleAbbrev
| SynModuleDecl_NestedModule
| SynModuleDecl_NestedModule_Module
Expand Down Expand Up @@ -361,13 +359,9 @@ type FsAstType =
| ParsedHashDirective_
| ParsedHashDirectiveArgument_String
| ParsedHashDirectiveArgument_SourceIdentifier
// Modules and namespaces cannot really be trusted
// Update 2022: this is not entirely true anymore.
// TODO: investigate what the status of this is.
// Their range can be influenced by non code constructs (like comments)
// | SynModuleOrNamespaceSig_AnonModule
// | SynModuleOrNamespaceSig_DeclaredNamespace
// | SynModuleOrNamespaceSig_GlobalNamespace
// | SynModuleOrNamespaceSig_AnonModule, pick first child node
| SynModuleOrNamespaceSig_DeclaredNamespace
| SynModuleOrNamespaceSig_GlobalNamespace
| SynModuleOrNamespaceSig_NamedModule
| SynModuleSigDecl_ModuleAbbrev
| SynModuleSigDecl_NestedModule
Expand Down

0 comments on commit c5fb3ef

Please sign in to comment.