From ba0aad121ba5fdb01419f40d09aac11a633df383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Tue, 11 Jul 2023 20:53:12 -0400 Subject: [PATCH] MA0116 and MA0122 are disabled on ASP.NET Core 8 (#557) --- ...meterAttributeForRazorComponentAnalyzer.cs | 29 +++++++++++++------ .../Helpers/ProjectBuilder.Validation.cs | 5 ++++ .../Helpers/TargetFramework.cs | 1 + ...AttributeForRazorComponentAnalyzerTests.cs | 22 +++++++++++++- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs b/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs index 51563b173..25d67b4de 100644 --- a/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs @@ -1,4 +1,5 @@ -using System.Collections.Immutable; +using System; +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -56,14 +57,20 @@ public override void Initialize(AnalysisContext context) private sealed class AnalyzerContext { + private static readonly Version Version8 = new(8, 0); + public AnalyzerContext(Compilation compilation) { ParameterSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.ParameterAttribute"); SupplyParameterFromQuerySymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute"); EditorRequiredSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.EditorRequiredAttribute"); RouteAttributeSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.RouteAttribute"); + + AspNetCoreVersion = SupplyParameterFromQuerySymbol?.ContainingAssembly.Identity.Version; } + public Version? AspNetCoreVersion { get; } + public INamedTypeSymbol? ParameterSymbol { get; } public INamedTypeSymbol? SupplyParameterFromQuerySymbol { get; } public INamedTypeSymbol? EditorRequiredSymbol { get; } @@ -73,19 +80,23 @@ public AnalyzerContext(Compilation compilation) internal void AnalyzeProperty(SymbolAnalysisContext context) { + // note: All attributes are sealed, no need for checking inherited types var property = (IPropertySymbol)context.Symbol; - // All attributes are sealed - if (property.HasAttribute(SupplyParameterFromQuerySymbol, inherits: false)) + // https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-8-preview-6/?WT.mc_id=DT-MVP-5003978#cascade-query-string-values-to-blazor-components + if (AspNetCoreVersion < Version8) { - if (!property.HasAttribute(ParameterSymbol, inherits: false)) + if (property.HasAttribute(SupplyParameterFromQuerySymbol, inherits: false)) { - context.ReportDiagnostic(s_supplyParameterFromQueryRule, property); - } + if (!property.HasAttribute(ParameterSymbol, inherits: false)) + { + context.ReportDiagnostic(s_supplyParameterFromQueryRule, property); + } - if (!property.ContainingType.HasAttribute(RouteAttributeSymbol)) - { - context.ReportDiagnostic(s_supplyParameterFromQueryRoutableRule, property); + if (!property.ContainingType.HasAttribute(RouteAttributeSymbol)) + { + context.ReportDiagnostic(s_supplyParameterFromQueryRoutableRule, property); + } } } diff --git a/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs b/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs index aed045222..f4a5b7b83 100644 --- a/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs +++ b/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs @@ -204,6 +204,11 @@ private Task CreateProject() AddNuGetReference("Microsoft.NETCore.App.Ref", "7.0.0", "ref/net7.0/"); AddNuGetReference("Microsoft.AspNetCore.App.Ref", "7.0.0", "ref/net7.0/"); break; + + case TargetFramework.AspNetCore8_0: + AddNuGetReference("Microsoft.NETCore.App.Ref", "8.0.0-preview.6.23329.7", "ref/net8.0/"); + AddNuGetReference("Microsoft.AspNetCore.App.Ref", "8.0.0-preview.6.23329.11", "ref/net8.0/"); + break; case TargetFramework.WindowsDesktop5_0: AddNuGetReference("Microsoft.WindowsDesktop.App.Ref", "5.0.0", "ref/net5.0/"); diff --git a/tests/Meziantou.Analyzer.Test/Helpers/TargetFramework.cs b/tests/Meziantou.Analyzer.Test/Helpers/TargetFramework.cs index 303b994b2..e630a8b1f 100644 --- a/tests/Meziantou.Analyzer.Test/Helpers/TargetFramework.cs +++ b/tests/Meziantou.Analyzer.Test/Helpers/TargetFramework.cs @@ -11,5 +11,6 @@ public enum TargetFramework AspNetCore5_0, AspNetCore6_0, AspNetCore7_0, + AspNetCore8_0, WindowsDesktop5_0, } diff --git a/tests/Meziantou.Analyzer.Test/Rules/ParameterAttributeForRazorComponentAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/ParameterAttributeForRazorComponentAnalyzerTests.cs index a774e3bc8..4921f71f9 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/ParameterAttributeForRazorComponentAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/ParameterAttributeForRazorComponentAnalyzerTests.cs @@ -44,6 +44,26 @@ await CreateProjectBuilder() .ValidateAsync(); } + [Fact] + public async Task SupplyParameterFromQuery_MissingParameter_AspNetCore8() + { + const string SourceCode = """ +using Microsoft.AspNetCore.Components; + +[Route("/test")] +class Test +{ + [SupplyParameterFromQuery] + public int A { get; set; } +} +"""; + + await CreateProjectBuilder() + .WithSourceCode(SourceCode) + .WithTargetFramework(TargetFramework.AspNetCore8_0) + .ValidateAsync(); + } + [Fact] public async Task SupplyParameterFromQuery_WithParameter() { @@ -83,7 +103,7 @@ await CreateProjectBuilder() } [Fact] - public async Task SupplyParameterFromQuery_NonRoutable () + public async Task SupplyParameterFromQuery_NonRoutable() { const string SourceCode = """ using Microsoft.AspNetCore.Components;