Skip to content

Commit

Permalink
Handle modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Nov 18, 2024
1 parent ca14ed0 commit 5fbaaf3
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions ReactiveGenerator/ReactiveGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static bool HasReactivePropertiesInHierarchy(INamedTypeSymbol typeSymbol
return typeSymbol.BaseType != null && HasReactivePropertiesInHierarchy(typeSymbol.BaseType);
}

private static string GenerateClassSource(
private static string GenerateClassSource(
Compilation compilation,
INamedTypeSymbol classSymbol,
List<IPropertySymbol> allProperties,
Expand Down Expand Up @@ -245,6 +245,7 @@ private static string GenerateClassSource(
if (typeProperties.Any())
sb.AppendLine();


// Generate backing fields and properties
foreach (var property in typeProperties)
{
Expand All @@ -253,21 +254,36 @@ private static string GenerateClassSource(
var backingFieldName = GetBackingFieldName(propertyName);
var eventArgsFieldName = GetEventArgsFieldName(propertyName);

// Backing field should be private regardless of property accessibility
sb.AppendLine($" private {propertyType} {backingFieldName};");
sb.AppendLine();

// Get property accessors' modifiers
var getterAccessibility = GetAccessorAccessibility(property.GetMethod);

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 262 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.
var setterAccessibility = GetAccessorAccessibility(property.SetMethod);

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.

Check warning on line 263 in ReactiveGenerator/ReactiveGenerator.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Possible null reference argument for parameter 'accessor' in 'string ReactiveGenerator.GetAccessorAccessibility(IMethodSymbol accessor)'.
var propertyAccessibility = property.DeclaredAccessibility.ToString().ToLowerInvariant();

sb.AppendLine($" {propertyAccessibility} partial {propertyType} {propertyName}");
sb.AppendLine(" {");
sb.AppendLine($" get => {backingFieldName};");
sb.AppendLine(" set");
sb.AppendLine(" {");
sb.AppendLine($" if (!Equals({backingFieldName}, value))");
sb.AppendLine(" {");
sb.AppendLine($" {backingFieldName} = value;");
sb.AppendLine($" OnPropertyChanged({eventArgsFieldName});");
sb.AppendLine(" }");
sb.AppendLine(" }");

// Generate getter
var getterModifier = getterAccessibility != propertyAccessibility ? $"{getterAccessibility} " : "";
sb.AppendLine($" {getterModifier}get => {backingFieldName};");

// Generate setter if it exists
if (property.SetMethod != null)
{
var setterModifier = setterAccessibility != propertyAccessibility ? $"{setterAccessibility} " : "";
sb.AppendLine($" {setterModifier}set");
sb.AppendLine(" {");
sb.AppendLine($" if (!Equals({backingFieldName}, value))");
sb.AppendLine(" {");
sb.AppendLine($" {backingFieldName} = value;");
sb.AppendLine($" OnPropertyChanged({eventArgsFieldName});");
sb.AppendLine(" }");
sb.AppendLine(" }");
}

sb.AppendLine(" }");
sb.AppendLine();
}
Expand All @@ -281,7 +297,23 @@ private static string GenerateClassSource(

return sb.ToString();
}


private static string GetAccessorAccessibility(IMethodSymbol accessor)
{
if (accessor == null)
return "private";

return accessor.DeclaredAccessibility switch
{
Accessibility.Private => "private",
Accessibility.Protected => "protected",
Accessibility.Internal => "internal",
Accessibility.ProtectedOrInternal => "protected internal",
Accessibility.ProtectedAndInternal => "private protected",
_ => accessor.DeclaredAccessibility.ToString().ToLowerInvariant()
};
}

private static string GetBackingFieldName(string propertyName)
{
return "_" + char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1);
Expand All @@ -300,4 +332,4 @@ sealed class ReactiveAttribute : Attribute
public ReactiveAttribute() { }
}";
}
}
}

0 comments on commit 5fbaaf3

Please sign in to comment.