Skip to content

Commit 707bd27

Browse files
committed
Add more checks and test cases
1 parent 7163cb0 commit 707bd27

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Framework/AspNetCoreAnalyzers/src/SourceGenerators/PublicTopLevelProgramGenerator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2020
{
2121
return null;
2222
}
23+
// If the discovered `Program` type is an interface, struct or generic type then its not
24+
// generated and has been defined in source, so we can skip it
25+
if (program.TypeKind == TypeKind.Struct || program.TypeKind == TypeKind.Interface || program.IsGenericType)
26+
{
27+
return null;
28+
}
29+
// If there are multiple partial declarations, then do nothing since we don't want
30+
// to trample on visibility explicitly set by the user
2331
if (program.DeclaringSyntaxReferences.Length > 1)
2432
{
2533
return null;

src/Framework/AspNetCoreAnalyzers/test/SourceGenerators/PublicTopLevelProgramGeneratorTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ public partial class Program { }
4646
await VerifyCS.VerifyAsync(source);
4747
}
4848

49+
[Fact]
50+
public async Task DoesNotGeneratesSource_IfProgramDeclaresExplicitInternalAccess()
51+
{
52+
var source = """
53+
using Microsoft.AspNetCore.Builder;
54+
55+
var app = WebApplication.Create();
56+
57+
app.MapGet("/", () => "Hello, World!");
58+
59+
app.Run();
60+
61+
internal partial class Program { }
62+
""";
63+
64+
await VerifyCS.VerifyAsync(source);
65+
}
66+
4967
[Fact]
5068
public async Task DoesNotGeneratorSource_ExplicitPublicProgramClass()
5169
{
@@ -85,6 +103,30 @@ public static void Main()
85103
app.Run();
86104
}
87105
}
106+
""";
107+
108+
await VerifyCS.VerifyAsync(source);
109+
}
110+
111+
[Theory]
112+
[InlineData("interface")]
113+
[InlineData("struct")]
114+
public async Task DoesNotGeneratorSource_ExplicitInternalProgramType(string type)
115+
{
116+
var source = $$"""
117+
using Microsoft.AspNetCore.Builder;
118+
119+
internal {{type}} Program
120+
{
121+
public static void Main(string[] args)
122+
{
123+
var app = WebApplication.Create();
124+
125+
app.MapGet("/", () => "Hello, World!");
126+
127+
app.Run();
128+
}
129+
}
88130
""";
89131

90132
await VerifyCS.VerifyAsync(source);

0 commit comments

Comments
 (0)