From 118d47fed23860971ff318e72f3becfd8c04cf1f Mon Sep 17 00:00:00 2001 From: Artyom Sovetnikov <2056864+elringus@users.noreply.github.com> Date: Fri, 5 Jan 2024 21:47:07 +0300 Subject: [PATCH] fix js space (#132) --- .../Pack/BindingTest.cs | 21 +++++++++++++++++++ .../NamespaceBuilder/NamespaceBuilder.cs | 2 +- .../Pack/BindingGenerator/BindingGenerator.cs | 10 ++++++++- src/cs/Directory.Build.props | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs b/src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs index 0646c57a..cd31ff17 100644 --- a/src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs +++ b/src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs @@ -169,6 +169,27 @@ public void DifferentSpacesWithSameRootAssignedUnderSameObject () """); } + [Fact] + public void DifferentSpacesStartingEquallyAreNotAssignedToSameObject () + { + AddAssembly( + WithClass("Foo", "[JSInvokable] public static void Method () { }"), + WithClass("FooBar.Baz", "[JSInvokable] public static void Method () { }") + ); + Execute(); + Contains( + """ + export const Foo = { + method: () => getExports().Foo_MockClass.Method() + }; + export const FooBar = { + Baz: { + method: () => getExports().FooBar_Baz_MockClass.Method() + } + }; + """); + } + [Fact] public void BindingsFromMultipleSpacesAssignedToRespectiveObjects () { diff --git a/src/cs/Bootsharp.Publish/Common/NamespaceBuilder/NamespaceBuilder.cs b/src/cs/Bootsharp.Publish/Common/NamespaceBuilder/NamespaceBuilder.cs index eb40e006..1bd9220c 100644 --- a/src/cs/Bootsharp.Publish/Common/NamespaceBuilder/NamespaceBuilder.cs +++ b/src/cs/Bootsharp.Publish/Common/NamespaceBuilder/NamespaceBuilder.cs @@ -25,7 +25,7 @@ public string Build (Type type) return space; } - private IEnumerable CollectAttributes (System.Reflection.Assembly assembly) + private IEnumerable CollectAttributes (Assembly assembly) { return assembly.CustomAttributes.Where(a => a.AttributeType.Name == attributeName); } diff --git a/src/cs/Bootsharp.Publish/Pack/BindingGenerator/BindingGenerator.cs b/src/cs/Bootsharp.Publish/Pack/BindingGenerator/BindingGenerator.cs index 46ae7b10..b9ecc075 100644 --- a/src/cs/Bootsharp.Publish/Pack/BindingGenerator/BindingGenerator.cs +++ b/src/cs/Bootsharp.Publish/Pack/BindingGenerator/BindingGenerator.cs @@ -69,7 +69,8 @@ private bool ShouldCloseNamespace () private void CloseNamespace () { - var target = nextBinding is null ? 0 : nextBinding.Namespace.Count(c => c == '.'); + var target = (nextBinding is null || GetRoot(nextBinding) != GetRoot(binding)) ? 0 + : nextBinding.Namespace.Count(c => c == '.'); for (; level >= target; level--) if (level == 0) builder.Append("\n};"); else builder.Append($"\n{Pad(level)}}}"); @@ -132,6 +133,13 @@ private void EmitEnum (Type @enum) builder.Append($"{Comma()}\n{Pad(level + 1)}{@enum.Name}: {{ {fields} }}"); } + private string GetRoot (Binding binding) + { + var firstDotIdx = binding.Namespace.IndexOf('.'); + if (firstDotIdx < 0) return binding.Namespace; + return binding.Namespace[..firstDotIdx]; + } + private string Pad (int level) => new(' ', level * 4); private string Comma () => builder[^1] == '{' ? "" : ","; private bool ShouldWait (MethodMeta method) => diff --git a/src/cs/Directory.Build.props b/src/cs/Directory.Build.props index 3dc375c0..a3ad7c23 100644 --- a/src/cs/Directory.Build.props +++ b/src/cs/Directory.Build.props @@ -1,6 +1,6 @@ - 0.1.1 + 0.1.2 Elringus javascript typescript ts js wasm node deno bun interop codegen https://bootsharp.com