Skip to content

Commit

Permalink
Fix WinUI source generator error
Browse files Browse the repository at this point in the history
  • Loading branch information
BurkusCat committed Nov 18, 2023
1 parent 087efde commit 8b361f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/Burkus.Mvvm.Maui.SourceGenerators/AppReceiver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using System.Collections.Generic;

namespace Burkus.Mvvm.Maui;

Expand All @@ -8,16 +9,16 @@ namespace Burkus.Mvvm.Maui;
/// </summary>
internal class AppReceiver : ISyntaxReceiver
{
public ClassDeclarationSyntax? AppClass { get; private set; }
public List<ClassDeclarationSyntax> AppClasses { get; } = new List<ClassDeclarationSyntax>();

public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// look for a class declaration named App
if (syntaxNode is ClassDeclarationSyntax classDeclaration &&
classDeclaration.Identifier.ValueText == "App")
if (syntaxNode is ClassDeclarationSyntax classDeclaration
&& classDeclaration.Identifier.ValueText == "App")
{
// store the first one found
AppClass ??= classDeclaration;
// store all the ones found
AppClasses.Add(classDeclaration);
}
}
}
25 changes: 19 additions & 6 deletions src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ public void Execute(GeneratorExecutionContext context)
if (context.SyntaxReceiver is not AppReceiver receiver)
return;

var semanticModel = context.Compilation.GetSemanticModel(receiver.AppClass.SyntaxTree);
var isPartial = receiver.AppClass.Modifiers
INamedTypeSymbol chosenAppSymbol = null;

foreach (var appClass in receiver.AppClasses)
{
var semanticModel = context.Compilation.GetSemanticModel(appClass.SyntaxTree);

var isPartial = appClass.Modifiers
.Any(m => m.IsKind(SyntaxKind.PartialKeyword));

// check if the App class is partial and inherits from Application
var appSymbol = semanticModel.GetDeclaredSymbol(receiver.AppClass);
if (appSymbol is null || !isPartial || !appSymbol.BaseType.Equals(semanticModel.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Application")))
// check if the App class is partial and inherits from Application
var appSymbol = semanticModel.GetDeclaredSymbol(appClass);

if (appSymbol is not null && isPartial && appSymbol.BaseType.Equals(semanticModel.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Application")))

Check warning on line 36 in src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-plugin-ci

Use 'SymbolEqualityComparer' when comparing symbols
{
chosenAppSymbol = appSymbol;
break;
}
}

if (chosenAppSymbol == null)
{
throw new Exception("You must have a partial class called \"App\" that inherits from Application in your .NET MAUI project.");
return;
}

// get the MAUI program we are running in
Expand Down
4 changes: 2 additions & 2 deletions tests/DemoApp.UnitTests/DemoApp.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.5.2" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 8b361f6

Please sign in to comment.