Skip to content

Commit

Permalink
Detect correct accessibility for generated record properties
Browse files Browse the repository at this point in the history
  • Loading branch information
eNeRGy164 committed Aug 30, 2024
1 parent 17eaf6f commit db06481
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/DendroDocs.Tool/Analyzers/SourceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ private PropertyDescription CreatePropertyDescription(IPropertySymbol property)
{
var modifiers = property.IsReadOnly ? Modifier.Readonly : 0;

modifiers |= property.DeclaredAccessibility switch
{
Accessibility.Public => Modifier.Public,
Accessibility.Protected => Modifier.Protected,
Accessibility.Internal => Modifier.Internal,
Accessibility.Private => Modifier.Private,
_ => 0
};

var propertyDescription = new PropertyDescription(property.Type.ToDisplayString(), property.Name)
{
Modifiers = modifiers
Expand Down
40 changes: 40 additions & 0 deletions tests/DendroDocs.Tool.Tests/RecordDeclarationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,44 @@ internal record struct NestedC {};
// Assert
types[1].Modifiers.Should().HaveFlag(modifier);
}

[TestMethod]
public void RecordPropertiesShouldHaveCorrectAccessModifiersBasedOnDefault()
{
// Assign
var source =
$$"""
public record Person(string FirstName, string LastName)
{
}
""";

// Act
var types = TestHelper.VisitSyntaxTree(source, "CS0067");

// Assert
types[0].Properties[0].Modifiers.Should().HaveFlag(Modifier.Protected);
types[0].Properties[1].Modifiers.Should().HaveFlag(Modifier.Public);
types[0].Properties[2].Modifiers.Should().HaveFlag(Modifier.Public);
}

[TestMethod]
public void SealedRecordPropertiesShouldHaveCorrectAccessModifiers()
{
// Assign
var source =
$$"""
public sealed record Person(string FirstName, string LastName)
{
}
""";

// Act
var types = TestHelper.VisitSyntaxTree(source, "CS0067");

// Assert
types[0].Properties[0].Modifiers.Should().HaveFlag(Modifier.Private);
types[0].Properties[1].Modifiers.Should().HaveFlag(Modifier.Public);
types[0].Properties[2].Modifiers.Should().HaveFlag(Modifier.Public);
}
}

0 comments on commit db06481

Please sign in to comment.