Skip to content

Commit

Permalink
Add validation when registered type/method is outside in generator re…
Browse files Browse the repository at this point in the history
…ferenced project.
  • Loading branch information
neuecc committed Jul 29, 2024
1 parent 58c5791 commit 4e2e44b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
8 changes: 8 additions & 0 deletions sandbox/FilterShareProject/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public override Task InvokeAsync(ConsoleAppContext context, CancellationToken ca
Console.WriteLine("TAKO");
return Next.InvokeAsync(context, cancellationToken);
}
}

public class OtherProjectCommand
{
public void Execute(int x)
{
Console.WriteLine("Hello?");
}
}
34 changes: 8 additions & 26 deletions sandbox/GeneratorSandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
using ConsoleAppFramework;
using FilterShareProject;


var t = new Test();

var app = ConsoleApp.Create();

app.Add("foo", t.Handle);
var v = new OtherProjectCommand();
// app.Add("", v.Execute);
app.Add<MyProjectCommand>();

app.Run(args);


public partial class Test

public class MyProjectCommand
{
public void Handle(
bool a1,
bool a2,
bool a3,
bool a4,
bool a5,
bool a6,
bool a7,
bool a8,
bool a9,
bool a10,
bool a11,
bool a12,
bool a13,
bool a14,
bool a15,
bool a16,
bool a17,
bool a18,
bool a19,
string foo = null
)
public void Execute(int x)
{
Console.Write("ok");
Console.WriteLine("Hello?");
}
}
4 changes: 4 additions & 0 deletions src/ConsoleAppFramework/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ public static DiagnosticDescriptor Create(int id, string title, string messageFo
public static DiagnosticDescriptor ClassIsStaticOrAbstract { get; } = Create(
13,
"ConsoleAppBuilder.Add<T> class does not allow static or abstract classes.");

public static DiagnosticDescriptor DefinedInOtherProject { get; } = Create(
14,
"ConsoleAppFramework cannot register type/method in another project outside the SourceGenerator referenced project.");
}
12 changes: 12 additions & 0 deletions src/ConsoleAppFramework/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
return [];
}

if (type.DeclaringSyntaxReferences.Length == 0)
{
context.ReportDiagnostic(DiagnosticDescriptors.DefinedInOtherProject, node.GetLocation());
return [];
}

var publicMethods = type.GetMembers()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.OfType<IMethodSymbol>()
Expand Down Expand Up @@ -381,6 +387,12 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod

Command? ParseFromMethodSymbol(IMethodSymbol methodSymbol, bool addressOf, string commandName, FilterInfo[] typeFilters)
{
if (methodSymbol.DeclaringSyntaxReferences.Length == 0)
{
context.ReportDiagnostic(DiagnosticDescriptors.DefinedInOtherProject, node.GetLocation());
return null;
}

var docComment = methodSymbol.DeclaringSyntaxReferences[0].GetSyntax().GetDocumentationCommentTriviaSyntax();
var summary = "";
Dictionary<string, string>? parameterDescriptions = null;
Expand Down

0 comments on commit 4e2e44b

Please sign in to comment.