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

Suppress the compiler-generated members of record types in source generator. #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static string GetName(this IMethodSymbol symbol)
}
public static string GetReturnTypeString(this IMethodSymbol symbol)
{
if(symbol.MethodKind is MethodKind.Constructor
or MethodKind.SharedConstructor
if (symbol.MethodKind is MethodKind.Constructor
or MethodKind.SharedConstructor
or MethodKind.StaticConstructor)
{ return ""; }
return symbol.ReturnsVoid ? " : void" : $" : {symbol.ReturnType.GetTypeName()}";
Expand All @@ -29,7 +29,7 @@ public static string GetModifiersString(this IMethodSymbol symbol)
var modifiers = string.Join(" ",
new[]
{
symbol.ContainingType.TypeKind is not TypeKind.Interface
symbol.ContainingType.TypeKind is not TypeKind.Interface
&& symbol.IsAbstract ? "{abstract}" : "",
symbol.IsStatic ? "{static}" : "",
symbol.IsSealed ? "<<sealed>>" : "",
Expand All @@ -49,4 +49,21 @@ public static string GetParametersString(this IMethodSymbol symbol)
return string.Join(", ", symbol.Parameters
.Select(param => $"{param.Name} : {param.Type.GetTypeName()}"));
}

/// <summary>
/// Determine if the given method is the sole explicit constructor for a
/// record type.
/// </summary>
/// <param name="method">The method to inspect.</param>
/// <returns>True if there are no other explicit constructors in the method's type.</returns>
public static bool IsSoleRecordConstructor(this IMethodSymbol method)
{
var containingType = method.ContainingType;
var explicitConstructors = containingType.GetMembers()
.OfType<IMethodSymbol>()
.Where(m => !m.IsImplicitlyDeclared && m.MethodKind is MethodKind.Constructor or MethodKind.SharedConstructor or MethodKind.StaticConstructor);

return containingType.IsRecord
&& explicitConstructors.Count() == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ private void ProcessMembersSymbol(IImmutableSet<INamedTypeSymbol> symbols)
}
break;
case IPropertySymbol propertySymbol:
SetPropertyDeclaration(propertySymbol);
SetPropertyAssociation(propertySymbol, symbols);
// Skip compiler-generated properties.
if (!propertySymbol.IsImplicitlyDeclared)
{
SetPropertyDeclaration(propertySymbol);
SetPropertyAssociation(propertySymbol, symbols);
}
break;
case IMethodSymbol methodSymbol:
if (methodSymbol.MethodKind is not MethodKind.PropertyGet
if (!methodSymbol.IsSoleRecordConstructor() // Only include constructor when there is more than one.
&& methodSymbol.MethodKind is not MethodKind.PropertyGet
and not MethodKind.PropertySet
and not MethodKind.EventAdd
and not MethodKind.EventRemove
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
@startuml Item
class Item <<record>> {
+ Item(Name : string, Value : double)
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
+ Name : string <<get>> <<set>>
+ Value : double <<get>> <<set>>
+ <<override>> ToString() : string
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
+ {static} operator !=(left : Item?, right : Item?) : bool
+ {static} operator ==(left : Item?, right : Item?) : bool
+ <<override>> GetHashCode() : int
+ <<override>> Equals(obj : object?) : bool
+ <<virtual>> Equals(other : Item?) : bool
# Item(original : Item)
+ Deconstruct(Name : string, Value : double) : void
}
@enduml
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
@startuml Parameters
class Parameters <<record>> {
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
+ <<readonly>> X : int <<get>>
+ <<readonly>> Y : int <<get>>
+ Parameters(x : int, y : int)
+ Area() : int
+ <<override>> ToString() : string
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
+ {static} operator !=(left : Parameters?, right : Parameters?) : bool
+ {static} operator ==(left : Parameters?, right : Parameters?) : bool
+ <<override>> GetHashCode() : int
+ <<override>> Equals(obj : object?) : bool
+ <<virtual>> Equals(other : Parameters?) : bool
# Parameters(original : Parameters)
}
"IEquatable`1" "<Parameters>" <|.. Parameters
@enduml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
@startuml RecordA
class RecordA <<record>> {
+ RecordA(Name : string, Value : int)
# <<readonly>> <<virtual>> EqualityContract : Type <<get>>
+ Name : string <<get>> <<set>>
+ Value : int <<get>> <<set>>
+ <<override>> ToString() : string
# <<virtual>> PrintMembers(builder : StringBuilder) : bool
+ {static} operator !=(left : RecordA?, right : RecordA?) : bool
+ {static} operator ==(left : RecordA?, right : RecordA?) : bool
+ <<override>> GetHashCode() : int
+ <<override>> Equals(obj : object?) : bool
+ <<virtual>> Equals(other : RecordA?) : bool
# RecordA(original : RecordA)
+ Deconstruct(Name : string, Value : int) : void
}
@enduml
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
@startuml RecordStruct
struct RecordStruct <<sealed>> <<record>> {
+ RecordStruct(X : float, Y : float, Z : float)
+ X : float <<get>> <<set>>
+ Y : float <<get>> <<set>>
+ Z : float <<get>> <<set>>
+ <<readonly>> <<override>> ToString() : string
- <<readonly>> PrintMembers(builder : StringBuilder) : bool
+ {static} operator !=(left : RecordStruct, right : RecordStruct) : bool
+ {static} operator ==(left : RecordStruct, right : RecordStruct) : bool
+ <<readonly>> <<override>> GetHashCode() : int
+ <<readonly>> <<override>> Equals(obj : object) : bool
+ <<readonly>> Equals(other : RecordStruct) : bool
+ <<readonly>> Deconstruct(X : float, Y : float, Z : float) : void
+ RecordStruct()
}
@enduml