Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set record property modifiers correctly #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 91 additions & 44 deletions src/DendroDocs.Tool/Analyzers/SourceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,7 @@ public override void VisitRecordDeclaration(RecordDeclarationSyntax node)

base.VisitRecordDeclaration(node);

// Check for parts that are generated and not declared by the author
var symbol = semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
foreach (var constructor in symbol.Constructors)
{
var constructorDescription = new ConstructorDescription(symbol.Name);

foreach (var parameter in constructor.Parameters)
{
var parameterDescription = new ParameterDescription(parameter.Type.ToDisplayString(), parameter.Name);
constructorDescription.Parameters.Add(parameterDescription);

parameterDescription.HasDefaultValue = parameter.HasExplicitDefaultValue;
}

if (!this.currentType.Constructors.Any(c => c.Parameters.Select(p => p.Type).SequenceEqual(constructorDescription.Parameters.Select(p => p.Type))))
{
this.currentType.AddMember(constructorDescription);

constructorDescription.Modifiers |= constructor.IsSealed ? Modifier.Sealed : 0;

this.EnsureMemberDefaultAccessModifier(constructorDescription);
}
}

foreach (var member in symbol.GetMembers())
{
if (member is IPropertySymbol property)
{
if (this.currentType.Properties.Any(p => p.Name == property.Name))
{
continue;
}

var propertyDescription = new PropertyDescription(property.Type.ToDisplayString(), property.Name);
this.currentType.AddMember(propertyDescription);

propertyDescription.Modifiers |= property.IsReadOnly ? Modifier.Readonly : 0;

this.EnsureMemberDefaultAccessModifier(propertyDescription);
}
}
}
this.ProcessRecordSymbolInformation(node);
}

public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
Expand Down Expand Up @@ -347,4 +304,94 @@ private AttributeArgumentDescription CreateArgumentDescription(AttributeArgument

return new AttributeArgumentDescription(name, typeDisplayString, value);
}

private void ProcessRecordSymbolInformation(RecordDeclarationSyntax node)
{
var symbol = semanticModel.GetDeclaredSymbol(node);
if (symbol == null) return;

this.ProcessConstructors(symbol);
this.ProcessProperties(symbol);
}

private void ProcessConstructors(INamedTypeSymbol symbol)
{
if (this.currentType is null) return;

foreach (var constructor in symbol.Constructors)
{
var constructorDescription = CreateConstructorDescription(constructor, symbol.Name);

if (!this.ConstructorAlreadyExists(constructorDescription))
{
this.currentType.AddMember(constructorDescription);
this.EnsureMemberDefaultAccessModifier(constructorDescription);
}
}
}

private static ConstructorDescription CreateConstructorDescription(IMethodSymbol constructor, string typeName)
{
var constructorDescription = new ConstructorDescription(typeName);

foreach (var parameter in constructor.Parameters)
{
var parameterDescription = new ParameterDescription(parameter.Type.ToDisplayString(), parameter.Name)
{
HasDefaultValue = parameter.HasExplicitDefaultValue
};

constructorDescription.Parameters.Add(parameterDescription);
}

return constructorDescription;
}

private void ProcessProperties(INamedTypeSymbol symbol)
{
if (this.currentType is null) return;

foreach (var member in symbol.GetMembers())
{
if (member is IPropertySymbol property && !this.PropertyAlreadyExists(property.Name))
{
var propertyDescription = this.CreatePropertyDescription(property);

this.currentType.AddMember(propertyDescription);
}
}
}

private bool ConstructorAlreadyExists(ConstructorDescription constructorDescription)
{
return this.currentType?.Constructors.Any(c => c.Parameters.Select(p => p.Type).SequenceEqual(constructorDescription.Parameters.Select(p => p.Type))) ?? false;
}

private bool PropertyAlreadyExists(string propertyName)
{
return this.currentType?.Properties.Any(p => p.Name == propertyName) ?? false;
}

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
};

this.EnsureMemberDefaultAccessModifier(propertyDescription);

return propertyDescription;
}
}
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);
}
}
Loading