-
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f6c6877
Initial plan
Copilot 7f0fdf9
Add initial implementation of SupplyParameterFromForm analyzer
Copilot 8f7c639
Complete implementation and testing of SupplyParameterFromForm analyzer
Copilot 93a68fa
Update Resources.resx description per feedback
Copilot 0305c3f
Update Resources.resx description with complete suggested text includ…
Copilot 3c8fd5a
Remove irrelevant test that checks FormName parameter
Copilot 28d5aba
Fix template files to follow BL0008 analyzer guidance
Copilot 41245e2
Update analyzer to ignore default value initializers and revert templ…
Copilot cb2088e
Replace confusing 'default' string literal with 'initial-value' in an…
Copilot 023fae4
Replace remaining string literal 'default' with 'initial-value' in an…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Components/Analyzers/src/SupplyParameterFromFormAnalyzer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// 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; | ||
} | ||
|
||
// Ignore initializers that set to default values (null, default, etc.) | ||
if (IsDefaultValueInitializer(propertyDeclaration.Initializer.Value)) | ||
{ | ||
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); | ||
}); | ||
} | ||
|
||
private static bool IsDefaultValueInitializer(ExpressionSyntax expression) | ||
{ | ||
return expression switch | ||
{ | ||
// null | ||
LiteralExpressionSyntax { Token.ValueText: "null" } => true, | ||
// null! | ||
PostfixUnaryExpressionSyntax { Operand: LiteralExpressionSyntax { Token.ValueText: "null" }, OperatorToken.ValueText: "!" } => true, | ||
// default | ||
LiteralExpressionSyntax literal when literal.Token.IsKind(SyntaxKind.DefaultKeyword) => true, | ||
// default! | ||
PostfixUnaryExpressionSyntax { Operand: LiteralExpressionSyntax literal, OperatorToken.ValueText: "!" } | ||
when literal.Token.IsKind(SyntaxKind.DefaultKeyword) => true, | ||
_ => false | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.