Skip to content

Commit

Permalink
fix js space (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
elringus authored Jan 5, 2024
1 parent 6de6283 commit 118d47f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public string Build (Type type)
return space;
}

private IEnumerable<CustomAttributeData> CollectAttributes (System.Reflection.Assembly assembly)
private IEnumerable<CustomAttributeData> CollectAttributes (Assembly assembly)
{
return assembly.CustomAttributes.Where(a => a.AttributeType.Name == attributeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}}");
Expand Down Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion src/cs/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<Authors>Elringus</Authors>
<PackageTags>javascript typescript ts js wasm node deno bun interop codegen</PackageTags>
<PackageProjectUrl>https://bootsharp.com</PackageProjectUrl>
Expand Down

0 comments on commit 118d47f

Please sign in to comment.