Skip to content

Commit 89856e1

Browse files
Fix #1218: Multiple namespaces in same file
1 parent febc7c3 commit 89856e1

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

build.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Target "FableCLI" (fun _ ->
196196
Target "FableCoreJS" buildCoreJS
197197
Target "FableSplitter" buildSplitter
198198
Target "NUnitPlugin" buildNUnitPlugin
199+
Target "JsonConverter" buildJsonConverter
199200
Target "RunTestsJS" runTestsJS
200201

201202
Target "PublishPackages" (fun () ->

src/dotnet/Fable.Compiler/FSharp2Fable.fs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ type private DeclInfo(com, fileName) =
953953
let publicNameConflicts name =
954954
let conflicts = publicNames.Contains name
955955
if conflicts then
956-
"Public types, modules or functions with same name at same level are not supported: " + name
956+
"Public namespaces, modules, types or functions with same name at same level are not supported: " + name
957957
|> addError com fileName None
958958
else
959959
publicNames.Add name
@@ -1205,11 +1205,26 @@ and private transformDeclarations (com: IFableCompiler) ctx decls =
12051205
declInfo.GetDeclarations(com)
12061206

12071207
let private getRootModuleAndDecls decls =
1208+
let (|CommonNamespace|_|) = function
1209+
| (FSharpImplementationFileDeclaration.Entity(ent, subDecls))::restDecls
1210+
when ent.IsNamespace ->
1211+
let commonName = ent.CompiledName
1212+
(Some subDecls, restDecls) ||> List.fold (fun acc decl ->
1213+
match acc, decl with
1214+
| (Some subDecls), (FSharpImplementationFileDeclaration.Entity(ent, subDecls2)) ->
1215+
if ent.CompiledName = commonName
1216+
then Some(subDecls@subDecls2)
1217+
else None
1218+
| _ -> None)
1219+
|> Option.map (fun subDecls -> ent, subDecls)
1220+
| _ -> None
12081221
let rec getRootModuleAndDecls outerEnt decls =
12091222
match decls with
12101223
| [FSharpImplementationFileDeclaration.Entity (ent, decls)]
12111224
when ent.IsFSharpModule || ent.IsNamespace ->
12121225
getRootModuleAndDecls (Some ent) decls
1226+
| CommonNamespace(ent, decls) ->
1227+
getRootModuleAndDecls (Some ent) decls
12131228
| decls -> outerEnt, decls
12141229
getRootModuleAndDecls None decls
12151230

tests/Main/Fable.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Compile Include="Util/Aether.fs" />
1010
<Compile Include="Util/Util.fs" />
1111
<Compile Include="Util/Util2.fs" />
12+
<Compile Include="Util/Util3.fs" />
1213
<Compile Include="../../tests_external/Util3.fs" />
1314
<Compile Include="ApplicativeTests.fs" />
1415
<Compile Include="ArithmeticTests.fs" />

tests/Main/MiscTests.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ let ``Type abbreviation in namespace compiles``() = // See #140
137137
let h = Util2.H(5)
138138
equal "5" h.Value
139139

140+
[<Test>]
141+
let ``Multiple namespaces in same file work``() = // See #1218
142+
A.C.Helper.Add5(9) |> equal 14
143+
140144
let inline f x y = x + y
141145

142146
[<Test>]
@@ -866,7 +870,7 @@ let ``While with isNone doesn't hang with Some ()``() =
866870
Trampoline.run (fun _ -> Trampoline.Break ()) () |> ignore
867871

868872

869-
let inline (|HasLength|) x =
873+
let inline (|HasLength|) x =
870874
fun () -> (^a: (member Length: int) x)
871875

872876
let inline length (HasLength f) = f()

tests/Main/Util/Util3.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test that a file with multiple namespaces works, see #1218
2+
3+
// Different namespace together with namespaces
4+
// sharing a prefix doesn't work
5+
// namespace My.Namespace
6+
7+
// Duplicating namespaces doesn't work
8+
// namespace Fable.Tests.A.C
9+
10+
// Empty namespaces work
11+
namespace Fable.Tests.A.D
12+
13+
// Multiple mamespaces sharing prefix work
14+
namespace Fable.Tests.A.B
15+
type Helper =
16+
static member Add2(x) = x + 2
17+
18+
namespace Fable.Tests.A.C
19+
type Helper =
20+
static member Add5(x) = Fable.Tests.A.B.Helper.Add2(x + 3)

0 commit comments

Comments
 (0)