-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
UseMauiCommunityToolkit()
Analyzers (#486)
* Rename `Source Generators` folder -> `Analyzers` * Add Analyzer * Update Analyzer * Add Code Fix + Unit Test * Port Unit Test to Xunit * Add Tests, Update CodeFix * Fix Typo * Update Messaging * Add Using Directive CodeFix * Update Unit Tests * Update BaseHandlerTest.cs * Fixed typo * Created a helper method to grab the InvocationExpressionSyntax * improved the code remove some comments; make SyntaxFactory static, pass the cancellationToken * Remove UseCommunityToolkitAnalyzerTests * Added a verification to check other methods in the class * Fixed typo * Added more checks to make sure we find all cases * Update UseCommunityToolkitInitializationAnalyzer.cs * Update RESX * Update azure-pipelines.yml * Update azure-pipelines.yml * Create UseCommunityToolkitInitializationAnalyzerTests.cs * Update azure-pipelines.yml * Update azure-pipelines.yml Co-authored-by: pictos <[email protected]>
- Loading branch information
Showing
20 changed files
with
585 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/CommunityToolkit.Maui.Analyzers.CodeFixes/CodeFixResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/CommunityToolkit.Maui.Analyzers.CodeFixes/CodeFixResources.resx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<root> | ||
<resheader name="resmimetype"> | ||
<value>text/microsoft-resx</value> | ||
</resheader> | ||
<resheader name="version"> | ||
<value>2.0</value> | ||
</resheader> | ||
<resheader name="reader"> | ||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<resheader name="writer"> | ||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<data name="Initialize .NET MAUI Community Toolkit Before UseMauiApp" xml:space="preserve"> | ||
<value>Add `.UseMauiCommunityToolkit()`</value> | ||
<comment>Add `.UseMauiCommunityToolkit()`</comment> | ||
</data> | ||
</root> |
23 changes: 23 additions & 0 deletions
23
...ommunityToolkit.Maui.Analyzers.CodeFixes/CommunityToolkit.Maui.Analyzers.CodeFixes.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
<RootNamespace>CommunityToolkit.Maui.Analyzers</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CommunityToolkit.Maui.Analyzers\CommunityToolkit.Maui.Analyzers.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="CodeFixResources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="CodeFixResources.resx" /> | ||
<EmbeddedResource Update="CodeFixResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="CodeFixResources.Designer.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
src/CommunityToolkit.Maui.Analyzers.CodeFixes/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"profiles": { | ||
"CommunityToolkit.Maui.Sample": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "..\\..\\samples\\CommunityToolkit.Maui.Sample\\CommunityToolkit.Maui.Sample.csproj" | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...lkit.Maui.Analyzers.CodeFixes/UseCommunityToolkitInitializationAnalyzerCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq.Expressions; | ||
using System.Reflection.Metadata.Ecma335; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using CommunityToolkit.Maui.Analyzers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Rename; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace CommunityToolkit.Maui.Analyzers; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(UseCommunityToolkitInitializationAnalyzerCodeFixProvider)), Shared] | ||
public class UseCommunityToolkitInitializationAnalyzerCodeFixProvider : CodeFixProvider | ||
{ | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(UseCommunityToolkitInitializationAnalyzer.DiagnosticId); | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
|
||
var diagnostic = context.Diagnostics.First(); | ||
var diagnosticSpan = diagnostic.Location.SourceSpan; | ||
|
||
// Find the type declaration identified by the diagnostic. | ||
var declaration = root?.FindToken(diagnosticSpan.Start).Parent?.AncestorsAndSelf().OfType<InvocationExpressionSyntax>().Last() ?? throw new InvalidOperationException(); | ||
|
||
// Register a code action that will invoke the fix. | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
title: CodeFixResources.Initialize__NET_MAUI_Community_Toolkit_Before_UseMauiApp, | ||
createChangedDocument: c => AddUseCommunityToolkit(context.Document, declaration, c), | ||
equivalenceKey: nameof(CodeFixResources.Initialize__NET_MAUI_Community_Toolkit_Before_UseMauiApp)), | ||
diagnostic); | ||
} | ||
|
||
async Task<Document> AddUseCommunityToolkit(Document document, InvocationExpressionSyntax invocationExpression, CancellationToken cancellationToken) | ||
{ | ||
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
if (root is null) | ||
{ | ||
return document; | ||
} | ||
|
||
var updatedInvocationExpression = | ||
InvocationExpression( | ||
MemberAccessExpression( | ||
SyntaxKind.SimpleMemberAccessExpression, invocationExpression, IdentifierName("UseMauiCommunityToolkit"))); | ||
|
||
var mauiCommunityToolkitUsingStatement = | ||
UsingDirective( | ||
QualifiedName( | ||
IdentifierName("CommunityToolkit"), | ||
IdentifierName("Maui"))); | ||
|
||
var newRoot = root.ReplaceNode(invocationExpression, updatedInvocationExpression); | ||
|
||
if (newRoot is CompilationUnitSyntax compilationSyntax) | ||
{ | ||
newRoot = compilationSyntax.AddUsings(mauiCommunityToolkitUsingStatement).NormalizeWhitespace(); | ||
} | ||
|
||
var newDocument = document.WithSyntaxRoot(newRoot); | ||
|
||
return newDocument; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<UseMaui>true</UseMaui> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GF</CompilerGeneratedFilesOutputPath> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.7.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="All" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" PrivateAssets="All" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CommunityToolkit.Maui.Analyzers.CodeFixes\CommunityToolkit.Maui.Analyzers.CodeFixes.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
17 changes: 17 additions & 0 deletions
17
...mmunityToolkit.Maui.Analyzers.UnitTests/UseCommunityToolkitInitializationAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace CommunityToolkit.Maui.Analyzers.UnitTests; | ||
|
||
public class UseCommunityToolkitInitializationAnalyzerTests | ||
{ | ||
[Fact] | ||
public void UseCommunityToolkitInitializationAnalyzerId() | ||
{ | ||
Assert.Equal("MCT001", UseCommunityToolkitInitializationAnalyzer.DiagnosticId); | ||
} | ||
} |
Oops, something went wrong.