Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Sep 9, 2024
1 parent 965681b commit f3a2148
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using static ReactWithDotNet.RoslynHelper;

namespace ReactWithDotNet;

Expand Down Expand Up @@ -67,6 +68,8 @@ public void Execute(GeneratorExecutionContext context)
lines.Add("}");
continue;
}

var found = FindClassByNameAndNamespace(context.Compilation, propertyTypeName, GetNamespace(classDeclaration));

lines.Add($"if ({propertyTypeName}HasValueChecker.HasValue(value.{propertyName}))");
lines.Add("{");
Expand All @@ -92,21 +95,7 @@ public void Initialize(GeneratorInitializationContext context)
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}

// Helper to extract namespace
static string GetNamespace(ClassDeclarationSyntax classDeclaration)
{
var fileScopedNamespaceName = classDeclaration.FirstAncestorOrSelf<FileScopedNamespaceDeclarationSyntax>()?.Name;
if (fileScopedNamespaceName != null)
{
return fileScopedNamespaceName.ToString();
}

var namespaceDeclaration = classDeclaration.Ancestors()
.OfType<NamespaceDeclarationSyntax>()
.FirstOrDefault();

return namespaceDeclaration?.Name.ToString() ?? "global";
}


class SyntaxReceiver : ISyntaxReceiver
{
Expand Down
61 changes: 61 additions & 0 deletions ReactWithDotNet.SourceGenerator/RosalynHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace ReactWithDotNet;


using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;

static class RoslynHelper
{
public static string GetNamespace(ClassDeclarationSyntax classDeclaration)
{
var fileScopedNamespaceName = classDeclaration.FirstAncestorOrSelf<FileScopedNamespaceDeclarationSyntax>()?.Name;
if (fileScopedNamespaceName != null)
{
return fileScopedNamespaceName.ToString();
}

var namespaceDeclaration = classDeclaration.Ancestors()
.OfType<NamespaceDeclarationSyntax>()
.FirstOrDefault();

return namespaceDeclaration?.Name.ToString() ?? "global";
}
/// <summary>
/// Finds a class declaration by its name and namespace in the given compilation.
/// </summary>
/// <param name="compilation">The compilation to search through.</param>
/// <param name="className">The name of the class to find.</param>
/// <param name="namespaceName">The namespace of the class to find.</param>
/// <returns>The ClassDeclarationSyntax if found, null otherwise.</returns>
public static ClassDeclarationSyntax FindClassByNameAndNamespace(Compilation compilation, string className, string namespaceName)
{
foreach (var syntaxTree in compilation.SyntaxTrees)
{
var root = syntaxTree.GetRoot();

// Find all class declarations in the current syntax tree
var classDeclarations = root.DescendantNodes()
.OfType<ClassDeclarationSyntax>();

// Loop through each class declaration and check its name and namespace
foreach (var classDeclaration in classDeclarations)
{
// Check if the class name matches
if (classDeclaration.Identifier.Text == className)
{


if (GetNamespace(classDeclaration) == namespaceName)
{
return classDeclaration; // Class found
}
}
}
}

return null; // Class not found
}
}


0 comments on commit f3a2148

Please sign in to comment.