Skip to content

Commit

Permalink
Merge pull request #884 from ltrzesniewski/fix-883
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski authored Sep 8, 2022
2 parents ba948d0 + 904e5ae commit 7aa911d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// PropertyChanged.g.cs
// <auto-generated/>
#nullable enable
#pragma warning disable CS0067
#pragma warning disable CS8019
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Whatever
{
partial class Class1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs eventArgs)
{
PropertyChanged?.Invoke(this, eventArgs);
}
}
}
21 changes: 21 additions & 0 deletions PropertyChanged.Fody.Analyzer.Tests/CodeGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ public async Task CodeIsGeneratedForPartialClassWithAttribute()
const string source = @"
using PropertyChanged;
[AddINotifyPropertyChangedInterface]
public partial class Class1
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
";
var generated = await RunGenerator(source);

await VerifyCompilation(source, generated);
await Verify(JoinResults(generated));
}

[Fact]
public async Task CodeIsGeneratedForPartialClassWithAttributeInFileScopedNamespace()
{
const string source = @"
using PropertyChanged;
namespace Whatever;
[AddINotifyPropertyChangedInterface]
public partial class Class1
{
Expand Down
4 changes: 2 additions & 2 deletions PropertyChanged.Fody.Analyzer/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public static bool HasNoPropertyChangedEvent(this ClassDeclarationSyntax classDe
return classDeclaration.Members.OfType<EventFieldDeclarationSyntax>().SelectMany(member => member.Declaration.Variables).All(variable => variable.Identifier.Text != "PropertyChanged");
}

public static bool AreAllContainingTypesPartialClasses(this ClassDeclarationSyntax classDeclaration)
public static bool AreAllContainingTypesPartialClasses(this ClassDeclarationSyntax? classDeclaration)
{
while (classDeclaration?.Parent is { } parent)
{
if (parent.IsKind(SyntaxKind.NamespaceDeclaration) || parent.IsKind(SyntaxKind.CompilationUnit))
if ((SyntaxKind)parent.RawKind is SyntaxKind.NamespaceDeclaration or SyntaxKind.FileScopedNamespaceDeclaration or SyntaxKind.CompilationUnit)
return true;

if (parent is not ClassDeclarationSyntax parentClass || !parentClass.Modifiers.Any(SyntaxKind.PartialKeyword))
Expand Down

0 comments on commit 7aa911d

Please sign in to comment.