Skip to content

Commit 2fbb20d

Browse files
authored
Simplify PublicTopLevelProgramGenerator (dotnet#60222)
* Simplify PublicTopLevelProgramGenerator * Fix * Use expression body * Fix build * Fix build error * Fix accessibility check * Pattern matching * Fix typo * Fix typo * Fix build errors
1 parent 93f7682 commit 2fbb20d

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,27 @@ public partial class Program { }
1717

1818
public void Initialize(IncrementalGeneratorInitializationContext context)
1919
{
20-
var internalGeneratedProgramClass = context.CompilationProvider
20+
var internalGeneratedProgramClass = context.CompilationProvider.Select(static (compilation, cancellationToken) =>
2121
// Get the entry point associated with the compilation, this maps to the Main method definition
22-
.Select(static (compilation, cancellationToken) => compilation.GetEntryPoint(cancellationToken))
2322
// Get the containing symbol of the entry point, this maps to the Program class
24-
.Select(static (symbol, _) => symbol?.ContainingSymbol)
25-
// If the program class is already public, we don't need to generate anything.
26-
.Select(static (symbol, _) => symbol?.DeclaredAccessibility == Accessibility.Public ? null : symbol)
27-
// If the discovered `Program` type is not a class then its not
28-
// generated and has been defined in source, so we can skip it
29-
.Select(static (symbol, _) => symbol is INamedTypeSymbol { TypeKind: TypeKind.Class } ? symbol : null)
30-
// If there are multiple partial declarations, then do nothing since we don't want
31-
// to trample on visibility explicitly set by the user
32-
.Select(static (symbol, _) => symbol is { DeclaringSyntaxReferences: { Length: 1 } declaringSyntaxReferences } ? declaringSyntaxReferences.Single() : null)
23+
compilation.GetEntryPoint(cancellationToken)?.ContainingSymbol is INamedTypeSymbol
24+
{
25+
// If the discovered `Program` type is not a class then its not
26+
// generated and has been defined in source, so we can skip it
27+
// If the program class is already public, we don't need to generate anything.
28+
DeclaredAccessibility: not Accessibility.Public,
29+
TypeKind: TypeKind.Class,
30+
// If there are multiple partial declarations, then do nothing since we don't want
31+
// to trample on visibility explicitly set by the user
32+
DeclaringSyntaxReferences: { Length: 1 } declaringSyntaxReferences
33+
} &&
3334
// If the `Program` class is already declared in user code, we don't need to generate anything.
34-
.Select(static (declaringSyntaxReference, cancellationToken) => declaringSyntaxReference?.GetSyntax(cancellationToken) is ClassDeclarationSyntax ? null : declaringSyntaxReference);
35+
declaringSyntaxReferences.Single().GetSyntax(cancellationToken) is not ClassDeclarationSyntax
36+
);
3537

3638
context.RegisterSourceOutput(internalGeneratedProgramClass, (context, result) =>
3739
{
38-
if (result is not null)
40+
if (result)
3941
{
4042
context.AddSource("PublicTopLevelProgram.Generated.g.cs", PublicPartialProgramClassSource);
4143
}

0 commit comments

Comments
 (0)