Skip to content

Commit 4e2e44b

Browse files
committed
Add validation when registered type/method is outside in generator referenced project.
1 parent 58c5791 commit 4e2e44b

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

sandbox/FilterShareProject/Class1.cs

+8
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@ public override Task InvokeAsync(ConsoleAppContext context, CancellationToken ca
99
Console.WriteLine("TAKO");
1010
return Next.InvokeAsync(context, cancellationToken);
1111
}
12+
}
13+
14+
public class OtherProjectCommand
15+
{
16+
public void Execute(int x)
17+
{
18+
Console.WriteLine("Hello?");
19+
}
1220
}

sandbox/GeneratorSandbox/Program.cs

+8-26
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
using ConsoleAppFramework;
2+
using FilterShareProject;
23

34

4-
var t = new Test();
55

66
var app = ConsoleApp.Create();
77

8-
app.Add("foo", t.Handle);
8+
var v = new OtherProjectCommand();
9+
// app.Add("", v.Execute);
10+
app.Add<MyProjectCommand>();
911

1012
app.Run(args);
1113

1214

13-
public partial class Test
15+
16+
public class MyProjectCommand
1417
{
15-
public void Handle(
16-
bool a1,
17-
bool a2,
18-
bool a3,
19-
bool a4,
20-
bool a5,
21-
bool a6,
22-
bool a7,
23-
bool a8,
24-
bool a9,
25-
bool a10,
26-
bool a11,
27-
bool a12,
28-
bool a13,
29-
bool a14,
30-
bool a15,
31-
bool a16,
32-
bool a17,
33-
bool a18,
34-
bool a19,
35-
string foo = null
36-
)
18+
public void Execute(int x)
3719
{
38-
Console.Write("ok");
20+
Console.WriteLine("Hello?");
3921
}
4022
}

src/ConsoleAppFramework/DiagnosticDescriptors.cs

+4
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,8 @@ public static DiagnosticDescriptor Create(int id, string title, string messageFo
110110
public static DiagnosticDescriptor ClassIsStaticOrAbstract { get; } = Create(
111111
13,
112112
"ConsoleAppBuilder.Add<T> class does not allow static or abstract classes.");
113+
114+
public static DiagnosticDescriptor DefinedInOtherProject { get; } = Create(
115+
14,
116+
"ConsoleAppFramework cannot register type/method in another project outside the SourceGenerator referenced project.");
113117
}

src/ConsoleAppFramework/Parser.cs

+12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
7979
return [];
8080
}
8181

82+
if (type.DeclaringSyntaxReferences.Length == 0)
83+
{
84+
context.ReportDiagnostic(DiagnosticDescriptors.DefinedInOtherProject, node.GetLocation());
85+
return [];
86+
}
87+
8288
var publicMethods = type.GetMembers()
8389
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
8490
.OfType<IMethodSymbol>()
@@ -381,6 +387,12 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
381387

382388
Command? ParseFromMethodSymbol(IMethodSymbol methodSymbol, bool addressOf, string commandName, FilterInfo[] typeFilters)
383389
{
390+
if (methodSymbol.DeclaringSyntaxReferences.Length == 0)
391+
{
392+
context.ReportDiagnostic(DiagnosticDescriptors.DefinedInOtherProject, node.GetLocation());
393+
return null;
394+
}
395+
384396
var docComment = methodSymbol.DeclaringSyntaxReferences[0].GetSyntax().GetDocumentationCommentTriviaSyntax();
385397
var summary = "";
386398
Dictionary<string, string>? parameterDescriptions = null;

0 commit comments

Comments
 (0)