From b04e9540921426ff5232a4a2751ee5649129d7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Sun, 23 Oct 2022 16:25:25 -0400 Subject: [PATCH] MA0116 and MA0117 do not consider [CascadingParameter] --- README.md | 4 ++-- docs/README.md | 12 ++++++------ docs/Rules/MA0116.md | 2 +- docs/Rules/MA0117.md | 8 ++------ .../ParameterAttributeForRazorComponentAnalyzer.cs | 14 ++++++-------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index bac2298dd..b006b0382 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,8 @@ If you are already using other analyzers, you can check [which rules are duplica |[MA0113](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0113.md)|Design|Use DateTime.UnixEpoch|ℹ️|✔️|❌| |[MA0114](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0114.md)|Design|Use DateTimeOffset.UnixEpoch|ℹ️|✔️|❌| |[MA0115](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0115.md)|Usage|Unknown component parameter|⚠️|✔️|❌| -|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\]|⚠️|✔️|❌| -|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\]|⚠️|✔️|❌| +|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\]|⚠️|✔️|❌| +|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\]|⚠️|✔️|❌| |[MA0118](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0118.md)|Design|\[JSInvokable\] methods must be public|⚠️|✔️|❌| |[MA0119](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0119.md)|Design|JSRuntime must not be used in OnInitialized or OnInitializedAsync|⚠️|✔️|❌| |[MA0120](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0120.md)|Performance|Use InvokeVoidAsync when the returned value is not used|ℹ️|✔️|❌| diff --git a/docs/README.md b/docs/README.md index 0db1b975a..7d22378d7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -115,8 +115,8 @@ |[MA0113](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0113.md)|Design|Use DateTime.UnixEpoch|ℹ️|✔️|❌| |[MA0114](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0114.md)|Design|Use DateTimeOffset.UnixEpoch|ℹ️|✔️|❌| |[MA0115](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0115.md)|Usage|Unknown component parameter|⚠️|✔️|❌| -|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\]|⚠️|✔️|❌| -|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\]|⚠️|✔️|❌| +|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\]|⚠️|✔️|❌| +|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\]|⚠️|✔️|❌| |[MA0118](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0118.md)|Design|\[JSInvokable\] methods must be public|⚠️|✔️|❌| |[MA0119](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0119.md)|Design|JSRuntime must not be used in OnInitialized or OnInitializedAsync|⚠️|✔️|❌| |[MA0120](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0120.md)|Performance|Use InvokeVoidAsync when the returned value is not used|ℹ️|✔️|❌| @@ -466,10 +466,10 @@ dotnet_diagnostic.MA0114.severity = suggestion # MA0115: Unknown component parameter dotnet_diagnostic.MA0115.severity = warning - # MA0116: Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] or [CascadingParameter] + # MA0116: Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] dotnet_diagnostic.MA0116.severity = warning - # MA0117: Parameters with [EditorRequired] attributes should also be marked as [Parameter] or [CascadingParameter] + # MA0117: Parameters with [EditorRequired] attributes should also be marked as [Parameter] dotnet_diagnostic.MA0117.severity = warning # MA0118: [JSInvokable] methods must be public @@ -827,10 +827,10 @@ dotnet_diagnostic.MA0114.severity = none # MA0115: Unknown component parameter dotnet_diagnostic.MA0115.severity = none - # MA0116: Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] or [CascadingParameter] + # MA0116: Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] dotnet_diagnostic.MA0116.severity = none - # MA0117: Parameters with [EditorRequired] attributes should also be marked as [Parameter] or [CascadingParameter] + # MA0117: Parameters with [EditorRequired] attributes should also be marked as [Parameter] dotnet_diagnostic.MA0117.severity = none # MA0118: [JSInvokable] methods must be public diff --git a/docs/Rules/MA0116.md b/docs/Rules/MA0116.md index 6fad5f037..ec16875b5 100644 --- a/docs/Rules/MA0116.md +++ b/docs/Rules/MA0116.md @@ -1,4 +1,4 @@ -# MA0116 - Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\] +# MA0116 - Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\] Detect when `[SupplyParameterFromQuery]` attributes are used without the `[Parameter]` attributes. diff --git a/docs/Rules/MA0117.md b/docs/Rules/MA0117.md index 77048e3dc..6ff0534f2 100644 --- a/docs/Rules/MA0117.md +++ b/docs/Rules/MA0117.md @@ -1,6 +1,6 @@ -# MA0117 - Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\] or \[CascadingParameter\] +# MA0117 - Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\] -Detect when `[EditorRequired]` attributes are used without either the `[Parameter]` or `[CascadingParameter]` attributes. +Detect when `[EditorRequired]` attributes are used without the `[Parameter]` attribute. ````c# using Microsoft.AspNetCore.Components; @@ -12,9 +12,5 @@ using Microsoft.AspNetCore.Components; [Parameter] [EditorRequired] // ok as the property has the Parameter attribute public int B { get; set; } - - [CascadingParameter] - [EditorRequired] // ok as the property has the CascadingParameter attribute - public int B { get; set; } } ```` diff --git a/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs b/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs index 94ddf0329..a4889f663 100644 --- a/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/ParameterAttributeForRazorComponentAnalyzer.cs @@ -9,8 +9,8 @@ public sealed class ParameterAttributeForRazorComponentAnalyzer : DiagnosticAnal { private static readonly DiagnosticDescriptor s_supplyParameterFromQueryRule = new( RuleIdentifiers.SupplyParameterFromQueryRequiresParameterAttributeForRazorComponent, - title: "Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] or [CascadingParameter]", - messageFormat: "Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] or [CascadingParameter]", + title: "Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter]", + messageFormat: "Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter]", RuleCategories.Design, DiagnosticSeverity.Warning, isEnabledByDefault: true, @@ -19,8 +19,8 @@ public sealed class ParameterAttributeForRazorComponentAnalyzer : DiagnosticAnal private static readonly DiagnosticDescriptor s_editorRequiredRule = new( RuleIdentifiers.EditorRequiredRequiresParameterAttributeForRazorComponent, - title: "Parameters with [EditorRequired] attributes should also be marked as [Parameter] or [CascadingParameter]", - messageFormat: "Parameters with [EditorRequired] attributes should also be marked as [Parameter] or [CascadingParameter]", + title: "Parameters with [EditorRequired] attributes should also be marked as [Parameter]", + messageFormat: "Parameters with [EditorRequired] attributes should also be marked as [Parameter]", RuleCategories.Design, DiagnosticSeverity.Warning, isEnabledByDefault: true, @@ -49,17 +49,15 @@ private sealed class AnalyzerContext public AnalyzerContext(Compilation compilation) { ParameterSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.ParameterAttribute"); - CascadingParameterSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.CascadingParameterAttribute"); SupplyParameterFromQuerySymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute"); EditorRequiredSymbol = compilation.GetBestTypeByMetadataName("Microsoft.AspNetCore.Components.EditorRequiredAttribute"); } public INamedTypeSymbol? ParameterSymbol { get; } - public INamedTypeSymbol? CascadingParameterSymbol { get; } public INamedTypeSymbol? SupplyParameterFromQuerySymbol { get; } public INamedTypeSymbol? EditorRequiredSymbol { get; } - public bool IsValid => (ParameterSymbol != null || CascadingParameterSymbol != null) && (SupplyParameterFromQuerySymbol != null || EditorRequiredSymbol != null); + public bool IsValid => ParameterSymbol != null && (SupplyParameterFromQuerySymbol != null || EditorRequiredSymbol != null); internal void AnalyzeProperty(SymbolAnalysisContext context) { @@ -76,7 +74,7 @@ internal void AnalyzeProperty(SymbolAnalysisContext context) if (property.HasAttribute(EditorRequiredSymbol, inherits: false)) { - if (!property.HasAttribute(ParameterSymbol, inherits: false) && !property.HasAttribute(CascadingParameterSymbol, inherits: false)) + if (!property.HasAttribute(ParameterSymbol, inherits: false)) { context.ReportDiagnostic(s_editorRequiredRule, property); }