Skip to content

Commit

Permalink
MA0116 and MA0117 do not consider [CascadingParameter]
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Oct 23, 2022
1 parent 208c1dd commit b04e954
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|<span title='Info'>ℹ️</span>|✔️||
|[MA0114](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0114.md)|Design|Use DateTimeOffset.UnixEpoch|<span title='Info'>ℹ️</span>|✔️||
|[MA0115](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0115.md)|Usage|Unknown component parameter|<span title='Warning'>⚠️</span>|✔️||
|[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\]|<span title='Warning'>⚠️</span>|✔️||
|[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\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0118](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0118.md)|Design|\[JSInvokable\] methods must be public|<span title='Warning'>⚠️</span>|✔️||
|[MA0119](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0119.md)|Design|JSRuntime must not be used in OnInitialized or OnInitializedAsync|<span title='Warning'>⚠️</span>|✔️||
|[MA0120](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0120.md)|Performance|Use InvokeVoidAsync when the returned value is not used|<span title='Info'>ℹ️</span>|✔️||
Expand Down
12 changes: 6 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@
|[MA0113](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0113.md)|Design|Use DateTime.UnixEpoch|<span title='Info'>ℹ️</span>|✔️||
|[MA0114](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0114.md)|Design|Use DateTimeOffset.UnixEpoch|<span title='Info'>ℹ️</span>|✔️||
|[MA0115](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0115.md)|Usage|Unknown component parameter|<span title='Warning'>⚠️</span>|✔️||
|[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\]|<span title='Warning'>⚠️</span>|✔️||
|[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\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0116](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0116.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes should also be marked as \[Parameter\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0117](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0117.md)|Design|Parameters with \[EditorRequired\] attributes should also be marked as \[Parameter\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0118](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0118.md)|Design|\[JSInvokable\] methods must be public|<span title='Warning'>⚠️</span>|✔️||
|[MA0119](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0119.md)|Design|JSRuntime must not be used in OnInitialized or OnInitializedAsync|<span title='Warning'>⚠️</span>|✔️||
|[MA0120](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0120.md)|Performance|Use InvokeVoidAsync when the returned value is not used|<span title='Info'>ℹ️</span>|✔️||
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/Rules/MA0116.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
8 changes: 2 additions & 6 deletions docs/Rules/MA0117.md
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; }
}
````
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down

0 comments on commit b04e954

Please sign in to comment.