Skip to content

Commit

Permalink
✨ Analyze members from base types
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
angularsen committed Oct 2, 2022
1 parent 8866208 commit 6b27107
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
55 changes: 51 additions & 4 deletions AssignAll/AssignAll.Test/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ private static DiagnosticResult GetMissingAssignmentDiagnosticResult(string crea
return expected;
}

private static DiagnosticResult GetMissingAssignmentDiagnosticResult(int line = 9, int column = 23, params string[] unassignedMemberNames)
private static DiagnosticResult GetMissingAssignmentDiagnosticResult(int line, int column, string typeName, string[] unassignedMemberNames)
{
return GetMissingAssignmentDiagnosticResult("Foo", line, column, 0, unassignedMemberNames);
return GetMissingAssignmentDiagnosticResult(typeName, line, column, 0, unassignedMemberNames);
}

private static DiagnosticResult GetMissingAssignmentDiagnosticResult(params string[] unassignedMemberNames)
Expand Down Expand Up @@ -586,7 +586,8 @@ private class Foo
}
}
";
DiagnosticResult expected = GetMissingAssignmentDiagnosticResult(line: 3, column: 11, "PropInt", "PropString");
DiagnosticResult expected = GetMissingAssignmentDiagnosticResult(line: 3, column: 11, typeName: "Foo",
new[] { "PropInt", "PropString" });
VerifyCSharpDiagnostic(testContent, expected);
}

Expand Down Expand Up @@ -675,7 +676,6 @@ private class Foo
VerifyCSharpDiagnostic(testContent);
}


[Fact]
public void UnassignedMembersWithoutObjectInitializer_AddsNoDiagnostics()
{
Expand All @@ -701,5 +701,52 @@ private class Foo
";
VerifyCSharpDiagnostic(testContent);
}

[Fact]
public void Inheritance_AnalyzesMembersOfBaseTypes()
{
var testContent = @"
// AssignAll enable
// EXAMPLE 004 - Analyzer should also consider public members from any base types.
namespace Samples.ConsoleNet6;
public static class Example004_Inheritance
{
public static void Irrelevant()
{
// This should give analyzer error:
// Missing member assignments in object initializer for type 'Derived'. Properties: BasePropUnassigned, DerivedPropUnassigned
var foo = new Derived
{
// Commented assignments after opening brace.
// BasePropCommented = ,
// DerivedPropCommented = ,
// Assigned property, OK by analyzer
BasePropAssigned = 1,
DerivedPropAssigned = 1,
};
}
}
internal class Base
{
public int BasePropAssigned { get; set; }
public int BasePropCommented { get; set; }
public int BasePropUnassigned { get; set; }
}
internal class Derived : Base
{
public int DerivedPropAssigned { get; set; }
public int DerivedPropCommented { get; set; }
public int DerivedPropUnassigned { get; set; }
}
";
DiagnosticResult expected = GetMissingAssignmentDiagnosticResult(line: 12, column: 19, typeName: "Derived",
new[] { "BasePropUnassigned", "DerivedPropUnassigned" });
VerifyCSharpDiagnostic(testContent, expected);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ internal void AnalyzeObjectInitializers(SyntaxNodeAnalysisContext ctx)
if (objectCreationNamedType == null)
return;

ImmutableArray<ISymbol> members = objectCreationNamedType.GetMembers();
IEnumerable<ISymbol> membersEnumerable = objectCreationNamedType.GetMembers();
var baseType = objectCreationNamedType.BaseType;
for (int i = 0; i < 100; i++) // Max recursion
{
if (baseType == null || baseType.SpecialType == SpecialType.System_Object) break;
membersEnumerable = membersEnumerable.Concat(baseType.GetMembers());
baseType = baseType.BaseType;
}

var members = membersEnumerable.OrderBy(m => m.Name).ToImmutableList();

List<string> assignedMemberNames = objectInitializer.ChildNodes()
.OfType<AssignmentExpressionSyntax>()
Expand Down
8 changes: 8 additions & 0 deletions Samples.ConsoleNet6/Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Samples.ConsoleNet6;

internal class Base
{
public int BasePropAssigned { get; set; }
public int BasePropCommented { get; set; }
public int BasePropUnassigned { get; set; }
}
8 changes: 8 additions & 0 deletions Samples.ConsoleNet6/Derived.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Samples.ConsoleNet6;

internal class Derived : Base
{
public int DerivedPropAssigned { get; set; }
public int DerivedPropCommented { get; set; }
public int DerivedPropUnassigned { get; set; }
}
22 changes: 22 additions & 0 deletions Samples.ConsoleNet6/Example004_Inheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// AssignAll enable
// EXAMPLE 004 - Analyzer should also consider public members from any base types.
namespace Samples.ConsoleNet6;

public static class Example004_Inheritance
{
public static void Irrelevant()
{
// This should give analyzer error:
// Missing member assignments in object initializer for type 'Derived'. Properties: BasePropUnassigned, DerivedPropUnassigned
var foo = new Derived
{
// Commented assignments after opening brace.
// BasePropCommented = ,
// DerivedPropCommented = ,

// Assigned property, OK by analyzer
BasePropAssigned = 1,
DerivedPropAssigned = 1,
};
}
}
2 changes: 1 addition & 1 deletion Samples.ConsoleNet6/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ internal class Foo
public int PropCommented2 { get; set; }
public int PropCommented3 { get; set; }
public int PropUnassigned { get; set; }
}
}

0 comments on commit 6b27107

Please sign in to comment.