-
-
Notifications
You must be signed in to change notification settings - Fork 49
Add new analyzer to detect invalid Frame.Navigate calls #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -350,3 +350,4 @@ MigrationBackup/ | |
| .ionide/ | ||
| .tools | ||
| docs/memberpage.2.58.0 | ||
| .dotnet | ||
This file contains hidden or 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,27 @@ | ||
| ## WinUIEX1003: Frame.Navigate target must inherit from Page. | ||
|
|
||
| `Frame.Navigate(Type)` only supports types that inherit from `Microsoft.UI.Xaml.Controls.Page`. Passing any other type will fail at runtime, so the analyzer reports this as a build error. | ||
|
|
||
| |Item|Value| | ||
| |-|-| | ||
| |Category|Usage| | ||
| |Enabled|True| | ||
| |Severity|Error| | ||
| |CodeFix|False| | ||
| --- | ||
|
|
||
| ### Example | ||
|
|
||
| This will trigger the analyzer: | ||
| ```cs | ||
| frame.Navigate(typeof(MyViewModel)); | ||
| ``` | ||
|
|
||
| Use a page type instead: | ||
| ```cs | ||
| frame.Navigate(typeof(MyPage)); | ||
| ``` | ||
|
|
||
| ### References | ||
|
|
||
| - [Frame.Navigate(Type) Method](https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.frame.navigate) |
This file contains hidden or 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
71 changes: 71 additions & 0 deletions
71
src/WinUIEx.Analyzers/WinUIEx.Analyzers.Test/WinUIExFrameNavigateAnalyzerTests.cs
This file contains hidden or 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,71 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace WinUIEx.Analyzers.Test | ||
| { | ||
| [TestClass] | ||
| public class WinUIExFrameNavigateAnalyzerTests : BaseAnalyzersUnitTest<WinUIEx.Analyzers.WinUIExFrameNavigateAnalyzer, WinUIEx.Analyzers.WinUIExAnalyzersCodeFixProvider> | ||
| { | ||
| [TestMethod] | ||
| public async Task Frame_Navigate_With_NonPage_Type() | ||
| { | ||
| var testCode = @" | ||
| using System; | ||
| using Microsoft.UI.Xaml.Controls; | ||
| namespace ConsoleApplication1 | ||
| { | ||
| class MyViewModel { } | ||
|
|
||
| class MyClass | ||
| { | ||
| public void MethodName(Frame frame) | ||
| { | ||
| frame.Navigate({|#0:typeof(MyViewModel)|}); | ||
| } | ||
| } | ||
| }"; | ||
| var expected = Diagnostic("WinUIEx1003").WithLocation(0).WithArguments("ConsoleApplication1.MyViewModel", "Microsoft.UI.Xaml.Controls.Page"); | ||
| await VerifyAnalyzerAsync(testCode, expected); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task Frame_Navigate_With_Page_Subclass() | ||
| { | ||
| var testCode = @" | ||
| using System; | ||
| using Microsoft.UI.Xaml.Controls; | ||
| namespace ConsoleApplication1 | ||
| { | ||
| class MyPage : Page { } | ||
|
|
||
| class MyClass | ||
| { | ||
| public void MethodName(Frame frame) | ||
| { | ||
| frame.Navigate(typeof(MyPage)); | ||
| } | ||
| } | ||
| }"; | ||
| await VerifyAnalyzerAsync(testCode); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task Frame_Navigate_With_Page_Base_Type() | ||
| { | ||
| var testCode = @" | ||
| using System; | ||
| using Microsoft.UI.Xaml.Controls; | ||
| namespace ConsoleApplication1 | ||
| { | ||
| class MyClass | ||
| { | ||
| public void MethodName(Frame frame) | ||
| { | ||
| frame.Navigate(typeof(Page)); | ||
| } | ||
| } | ||
| }"; | ||
| await VerifyAnalyzerAsync(testCode); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
src/WinUIEx.Analyzers/WinUIEx.Analyzers/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
src/WinUIEx.Analyzers/WinUIEx.Analyzers/WinUIExFrameNavigateAnalyzer.cs
This file contains hidden or 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,79 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace WinUIEx.Analyzers | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public class WinUIExFrameNavigateAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public const string DiagnosticId1003 = "WinUIEx1003"; | ||
|
|
||
| private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.NavigateTypeTitle), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.NavigateTypeMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.NavigateTypeDescription), Resources.ResourceManager, typeof(Resources)); | ||
| private const string Category = "Usage"; | ||
|
|
||
| private static readonly DiagnosticDescriptor NavigateTypeRule = new DiagnosticDescriptor( | ||
| DiagnosticId1003, | ||
| Title, | ||
| MessageFormat, | ||
| Category, | ||
| DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true, | ||
| description: Description, | ||
| helpLinkUri: "https://dotmorten.github.io/WinUIEx/rules/WinUIEx1003.html"); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(NavigateTypeRule); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterOperationAction(AnalyzeInvocationOperation, OperationKind.Invocation); | ||
| } | ||
|
|
||
| private void AnalyzeInvocationOperation(OperationAnalysisContext context) | ||
| { | ||
| var invocation = context.Operation as IInvocationOperation; | ||
| if (invocation == null || !IsFrameNavigateWithTypeParameter(invocation.TargetMethod) || invocation.Arguments.Length == 0) | ||
| return; | ||
|
|
||
| var typeOfArgument = invocation.Arguments[0].Value as ITypeOfOperation; | ||
| if (typeOfArgument == null || typeOfArgument.TypeOperand == null) | ||
| return; | ||
|
|
||
| var pageType = context.Compilation.GetTypeByMetadataName("Microsoft.UI.Xaml.Controls.Page"); | ||
| if (pageType == null || InheritsFrom(typeOfArgument.TypeOperand, pageType)) | ||
| return; | ||
|
|
||
| var diagnostic = Diagnostic.Create( | ||
| NavigateTypeRule, | ||
| invocation.Arguments[0].Syntax.GetLocation(), | ||
| typeOfArgument.TypeOperand.ToDisplayString(), | ||
| pageType.ToDisplayString()); | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
|
|
||
| private static bool IsFrameNavigateWithTypeParameter(IMethodSymbol method) | ||
| { | ||
| return method != null && | ||
| method.Name == "Navigate" && | ||
| method.ContainingType?.ToDisplayString() == "Microsoft.UI.Xaml.Controls.Frame" && | ||
| method.Parameters.Length > 0 && | ||
| method.Parameters[0].Type.ToDisplayString() == "System.Type"; | ||
| } | ||
|
|
||
| private static bool InheritsFrom(ITypeSymbol type, ITypeSymbol baseType) | ||
| { | ||
| for (var current = type; current != null; current = current.BaseType) | ||
| { | ||
| if (SymbolEqualityComparer.Default.Equals(current, baseType)) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.