-
Notifications
You must be signed in to change notification settings - Fork 193
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
Add Compiler Warning for Unknown Attributes in Razor Components #9144
base: main
Are you sure you want to change the base?
Conversation
This diagnostic will be added to Razor component attributes that cannot be mapped to a Property in the underlying Component
This new intermediate pass will check for any component attributes that cannot be mapped to a valid property in the underlying component type, for the purpose of issuing a warning diagnostic.
…into the Razor Compiler
@dotnet-policy-service agree |
Thanks for this contribution. I do agree that this warning would be valuable (can name specific times it would have saved me doing razor work). Unfortunately we need to change the structure of this a bit. As written it's a breaking change as it introduces diagnostics for existing code on tooling upgrade. That is true even though it's a warning as many customers use At the moment we don't have an established process for this as we do for say the C# compiler. Need to work with the team to figure out how we want to appraoch changes like this. |
This part is going to be particularly difficult, if not straight up impossible, with the current structure of the razor compiler. Consider a partial example like this: Component1.razor <MyComponent UnmatchedAttribute="hi" /> MyComponent.razor @code {
[Parameter] public bool UnrelatedValue { get; set; }
} MyComponent.razor.cs public partial class MyComponent
{
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object> UnmatchedValues { get; set; }
} There is currently no way to understand that that |
@333fred Why? The compiler certainly understands these partial definitions. For example:
<Component2 />
@code {
[Parameter, EditorRequired] public int Param1 { get; set; }
}
using Microsoft.AspNetCore.Components;
namespace BlazorApp1.Shared;
public partial class Component2
{
[Parameter, EditorRequired] public int Param2 { get; set; }
} Gives me two warnings - for missing I think information about all component parameters should be in the corresponding tag helper's |
This commit attempts to solve the issues regarding the handling of 'CaptureUnmatchedValues' in the diagnostic pass. It does this by storing the value of this attribute parameter in 'BoundAttributeDescriptor.Metadata', where it can be later retrieved by the diagnostic analyzer.
I believe I have found a solution that addresses the issues regarding I have created a commit with a rough outline of how I would implement this. In its current state, it does pass all the unit tests I've written, and it works in all the use cases that I could think of. I am not sure how to address @jaredpar's concerns regarding the breaking change. Maybe if we can introduce warning waves for the razor compiler in the future, this PR might be useful as an opt-in warning. |
Summary
This PR introduces a new compiler warning that will be triggered when a Razor component uses an attribute that does not correspond to any known property on the component. This warning aids in early detection of potential mistakes, such as mistyping an attribute name, improving code quality and robustness.
Background
Razor components allow developers to define parameters to receive data from the parent component. Without any feedback, mistakes like mistyping an attribute name or using an attribute that doesn't correspond to any property can lead to unexpected behavior at runtime.
Changes
RZ10021: The attribute 'AttributeName' does not correspond to any of the parent component's parameters.
ComponentUnknownAttributeDiagnosticPass
, a new intermediate node pass that checks for unknown attributes.Incomplete Feature: Handling Arbitrary Parameter Matching
I'm seeking guidance on handling a Razor component's ability to capture unmatched values using a parameter with type
IDictionary<string, object>
and theCaptureUnmatchedValues
attribute set totrue
(Documentation).In order to correctly handle this case in my diagnostic pass, I would need to access some form of Type information about the given component either via Reflection, or component metadata. However, I was so far unable to find relevant information in the
IntermediateNodeWalker
's component visitor mechanism. The relevant part of the code can be found here.Request for Feedback
I'm particularly interested in feedback on:
<TreatWarningsAsErrors>
is enabled).