Skip to content

Commit

Permalink
MA0116 and MA0122 are disabled on ASP.NET Core 8 (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou authored Jul 12, 2023
1 parent a5de4bf commit ba0aad1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -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; }
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ private Task<Project> 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/");
Expand Down
1 change: 1 addition & 0 deletions tests/Meziantou.Analyzer.Test/Helpers/TargetFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public enum TargetFramework
AspNetCore5_0,
AspNetCore6_0,
AspNetCore7_0,
AspNetCore8_0,
WindowsDesktop5_0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -83,7 +103,7 @@ await CreateProjectBuilder()
}

[Fact]
public async Task SupplyParameterFromQuery_NonRoutable ()
public async Task SupplyParameterFromQuery_NonRoutable()
{
const string SourceCode = """
using Microsoft.AspNetCore.Components;
Expand Down

0 comments on commit ba0aad1

Please sign in to comment.