Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 13, 2025

This PR implements a new analyzer PersistentStateAnalyzer that applies the same property initializer validation rules to [PersistentState] properties as the existing SupplyParameterFromFormAnalyzer does for [SupplyParameterFromForm] properties.

Problem

Properties decorated with [PersistentState] that have non-default property initializers can have their values overwritten during parameter binding, similar to the issue that already exists with [SupplyParameterFromForm] properties. This can lead to unexpected behavior where the initial value is lost.

Solution

The new PersistentStateAnalyzer (diagnostic ID: BL0009) warns developers when they use property initializers with non-default values on [PersistentState] properties:

public class MyComponent : ComponentBase
{
    // ❌ Warning BL0009: Property initializer can be overwritten
    [PersistentState]
    public string BadProperty { get; set; } = "initial-value";

    [PersistentState] 
    public List<string> BadList { get; set; } = new List<string>();

    // ✅ No warning - default values are allowed
    [PersistentState]
    public string? GoodProperty { get; set; } = null;

    [PersistentState]
    public List<string>? GoodList { get; set; } = default;
}

Implementation Details

The implementation follows the exact same pattern as SupplyParameterFromFormAnalyzer:

  • Infrastructure updates: Added PersistentStateAttribute support to ComponentSymbols, ComponentFacts, and ComponentsApi
  • New analyzer: PersistentStateAnalyzer.cs with the same logic as the existing form analyzer
  • Diagnostic resources: Added localized warning messages for the new BL0009 diagnostic
  • Comprehensive tests: 11 test cases covering all scenarios including inheritance, attribute parameters, and edge cases

Validation

  • All existing tests continue to pass (60/60)
  • Manual testing confirms the analyzer correctly identifies problematic properties while allowing default initializers
  • Both Debug and Release builds work correctly

Fixes #63235.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] [Blazor] Implement property analyzer also for [PersistentState] [Blazor] Implement property analyzer for [PersistentState] attribute Aug 13, 2025
Copilot finished work on behalf of javiercn August 13, 2025 14:42
@Copilot Copilot AI requested a review from javiercn August 13, 2025 14:42
@javiercn javiercn marked this pull request as ready for review August 13, 2025 21:47
@Copilot Copilot AI review requested due to automatic review settings August 13, 2025 21:47
@javiercn javiercn requested a review from a team as a code owner August 13, 2025 21:47
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a new analyzer PersistentStateAnalyzer that warns developers when properties decorated with [PersistentState] have non-default property initializers, similar to the existing SupplyParameterFromFormAnalyzer for [SupplyParameterFromForm] properties.

  • Adds infrastructure support for the PersistentStateAttribute across the analyzer framework
  • Implements the new analyzer with diagnostic ID BL0009 that warns when property initializers can be overwritten
  • Provides comprehensive test coverage with 11 test cases covering various scenarios

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
PersistentStateAnalyzerTest.cs Comprehensive test suite with 11 test cases covering various scenarios including inheritance, attribute parameters, and edge cases
Resources.resx Localized diagnostic messages for the new BL0009 warning
PersistentStateAnalyzer.cs Core analyzer implementation that detects problematic property initializers on PersistentState properties
DiagnosticDescriptors.cs Registration of the new BL0009 diagnostic descriptor
ComponentsApi.cs API constant definitions for PersistentStateAttribute
ComponentSymbols.cs Symbol resolution support for PersistentStateAttribute
ComponentFacts.cs Helper method to identify PersistentState properties

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@javiercn javiercn merged commit 64804f9 into main Aug 14, 2025
29 checks passed
@javiercn javiercn deleted the copilot/fix-63235 branch August 14, 2025 10:02
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Blazor] Implement property analyzer also for [PersistentState]
3 participants