Skip to content

Commit

Permalink
Generator: better support arrays. Avoid namespaces conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Jan 3, 2025
1 parent 52e5ee9 commit c255880
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static string GetFullName(this INamespaceOrTypeSymbol namespaceOrType)
}

var currentNamespace = namespaceOrType.ContainingNamespace;
while (!currentNamespace.IsGlobalNamespace)
while (currentNamespace != null && !currentNamespace.IsGlobalNamespace)
{
stack.Push(currentNamespace.Name);
currentNamespace = currentNamespace.ContainingNamespace;
Expand All @@ -86,7 +86,9 @@ private static string GetName(INamespaceOrTypeSymbol namespaceOrType)
}
else
{
return namespaceOrType.Name;
return namespaceOrType.CanBeReferencedByName
? namespaceOrType.Name
: namespaceOrType.ToString();
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/BlazorBindings.Maui.ComponentGenerator/GeneratedTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ public record GeneratedTypeInfo(
{
public string GetTypeNameAndAddNamespace(string @namespace, string typeName)
{
// Adding random usings might cause conflicts with global usings.
// Therefore, we only add usings for commonly used namespaces.

var @using = Usings.FirstOrDefault(u => u.Namespace == @namespace);
if (@using == null && (@namespace.StartsWith("System.") || @namespace.StartsWith("Microsoft.")))
{
@using = new UsingStatement { Namespace = @namespace, IsUsed = true };
Usings.Add(@using);
}

if (@using != null)
{
Expand All @@ -39,11 +31,24 @@ public string GetTypeNameAndAddNamespace(string @namespace, string typeName)
return $"{aliasedNs}.{typeName}";
}

// Adding random usings might cause conflicts with global usings.
// Therefore, we only add usings for commonly used namespaces.
if (@using == null && (@namespace.StartsWith("System.") || @namespace.StartsWith("Microsoft.")))
{
@using = new UsingStatement { Namespace = @namespace, IsUsed = true };
Usings.Add(@using);
}

return $"global::{@namespace}.{typeName}";
}

public string GetTypeNameAndAddNamespace(ITypeSymbol type)
{
if (type is IArrayTypeSymbol arrayType)
{
return $"{GetTypeNameAndAddNamespace(arrayType.ElementType)}[]";
}

var typeName = type.GetCSharpTypeName();
if (typeName != null)
{
Expand Down

0 comments on commit c255880

Please sign in to comment.