-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add analyzer to warn when SupplyParameterFromForm properties have non-default initializers #63110
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
Changes from 3 commits
f6c6877
7f0fdf9
8f7c639
93a68fa
0305c3f
3c8fd5a
28d5aba
41245e2
cb2088e
023fae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,4 +180,13 @@ | |
<data name="ComponentParametersShouldBeAutoProperties_Title" xml:space="preserve"> | ||
<value>Component parameters should be auto properties</value> | ||
</data> | ||
<data name="SupplyParameterFromFormShouldNotHavePropertyInitializer_Description" xml:space="preserve"> | ||
<value>Initializing a property decorated with [SupplyParameterFromForm] can be overwritten with null during a form post. To ensure the property is never null, move the initialization to a component lifecycle method.</value> | ||
|
||
</data> | ||
<data name="SupplyParameterFromFormShouldNotHavePropertyInitializer_Format" xml:space="preserve"> | ||
<value>Property '{0}' has [SupplyParameterFromForm] and a property initializer. This can be overwritten with null during form posts.</value> | ||
</data> | ||
<data name="SupplyParameterFromFormShouldNotHavePropertyInitializer_Title" xml:space="preserve"> | ||
<value>Property with [SupplyParameterFromForm] should not have initializer</value> | ||
</data> | ||
</root> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.AspNetCore.Components.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class SupplyParameterFromFormAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public SupplyParameterFromFormAnalyzer() | ||
{ | ||
SupportedDiagnostics = ImmutableArray.Create( | ||
DiagnosticDescriptors.SupplyParameterFromFormShouldNotHavePropertyInitializer); | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
context.RegisterCompilationStartAction(context => | ||
{ | ||
if (!ComponentSymbols.TryCreate(context.Compilation, out var symbols)) | ||
{ | ||
// Types we need are not defined. | ||
return; | ||
} | ||
|
||
context.RegisterSyntaxNodeAction(context => | ||
{ | ||
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node; | ||
|
||
// Check if property has an initializer | ||
if (propertyDeclaration.Initializer == null) | ||
{ | ||
return; | ||
} | ||
|
||
var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration); | ||
if (propertySymbol == null) | ||
{ | ||
return; | ||
} | ||
|
||
// Check if property has [SupplyParameterFromForm] attribute | ||
if (!ComponentFacts.IsSupplyParameterFromForm(symbols, propertySymbol)) | ||
{ | ||
return; | ||
} | ||
|
||
// Check if the containing type inherits from ComponentBase | ||
var containingType = propertySymbol.ContainingType; | ||
if (!ComponentFacts.IsComponentBase(symbols, containingType)) | ||
{ | ||
return; | ||
} | ||
|
||
var propertyLocation = propertySymbol.Locations.FirstOrDefault(); | ||
if (propertyLocation != null) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.SupplyParameterFromFormShouldNotHavePropertyInitializer, | ||
propertyLocation, | ||
propertySymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))); | ||
} | ||
}, SyntaxKind.PropertyDeclaration); | ||
}); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.