CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

Observable Property used on a field with escaped name is not generating correctly #710

Closed GetGet99 closed 1 year ago

GetGet99 commented 1 year ago

Describe the bug

Observable Property used on a field with an escaped name (the name starting with the @ sign such as @event @param @int) is not generating correctly.

Steps to reproduce

  1. Create a project with the MVVM Toolkit installed
  2. Add in this code snippet
[ObservableProperty]
double @event;
  1. Type Event somewhere in a method and Ctrl + Click it to go to the definition
  2. The output should look like this
/// <inheritdoc cref="event"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public double Event
{
    get => event;
    set
    {
        if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(event, value))
        {
            OnEventChanging(value);
            OnEventChanging(default, value);
            OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Event);
            event = value;
            OnEventChanged(value);
            OnEventChanged(default, value);
            OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Event);
        }
    }
}

Expected behavior

Every single event in the generated code should be replaced with @event because event is treated as a C# keyword.

/// <inheritdoc cref="@event"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public double Event
{
    get => @event;
    set
    {
        if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(@event, value))
        {
            OnEventChanging(value);
            OnEventChanging(default, value);
            OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Event);
            @event = value;
            OnEventChanged(value);
            OnEventChanged(default, value);
            OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Event);
        }
    }
}

Screenshots

image

IDE and version

VS 2022

IDE version

17.6.2

Nuget packages

Nuget package version(s)

8.2.0

Help us help you

No, just wanted to report this