Skip to content

Commit 4e2796b

Browse files
committed
Minor code refactoring
1 parent 5abba05 commit 4e2796b

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -473,38 +473,6 @@ public void OnExecuteForPropertyArgs(GeneratorExecutionContext context, IReadOnl
473473
return;
474474
}
475475

476-
static FieldDeclarationSyntax CreateFieldDeclaration(INamedTypeSymbol type, string propertyName)
477-
{
478-
// Create a static field with a cached property changed/changing argument for a specified property.
479-
// This code produces a field declaration as follows:
480-
//
481-
// [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
482-
// [global::System.Obsolete("This field is not intended to be referenced directly by user code")]
483-
// public static readonly <ARG_TYPE> <PROPERTY_NAME><ARG_TYPE> = new("<PROPERTY_NAME>");
484-
return
485-
FieldDeclaration(
486-
VariableDeclaration(IdentifierName(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)))
487-
.AddVariables(
488-
VariableDeclarator(Identifier($"{propertyName}{type.Name}"))
489-
.WithInitializer(EqualsValueClause(
490-
ImplicitObjectCreationExpression()
491-
.AddArgumentListArguments(Argument(
492-
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(propertyName))))))))
493-
.AddModifiers(
494-
Token(SyntaxKind.PublicKeyword),
495-
Token(SyntaxKind.StaticKeyword),
496-
Token(SyntaxKind.ReadOnlyKeyword))
497-
.AddAttributeLists(
498-
AttributeList(SingletonSeparatedList(
499-
Attribute(IdentifierName("global::System.ComponentModel.EditorBrowsable")).AddArgumentListArguments(
500-
AttributeArgument(ParseExpression("global::System.ComponentModel.EditorBrowsableState.Never"))))),
501-
AttributeList(SingletonSeparatedList(
502-
Attribute(IdentifierName("global::System.Obsolete")).AddArgumentListArguments(
503-
AttributeArgument(LiteralExpression(
504-
SyntaxKind.StringLiteralExpression,
505-
Literal("This field is not intended to be referenced directly by user code")))))));
506-
}
507-
508476
INamedTypeSymbol
509477
propertyChangedEventArgsSymbol = context.Compilation.GetTypeByMetadataName(typeof(PropertyChangedEventArgs).FullName)!,
510478
propertyChangingEventArgsSymbol = context.Compilation.GetTypeByMetadataName(typeof(PropertyChangingEventArgs).FullName)!;
@@ -563,5 +531,44 @@ static FieldDeclarationSyntax CreateFieldDeclaration(INamedTypeSymbol type, stri
563531
// Add the partial type
564532
context.AddSource($"[{typeof(ObservablePropertyAttribute).Name}]_[__KnownINotifyPropertyChangedOrChangingArgs].cs", SourceText.From(source, Encoding.UTF8));
565533
}
534+
535+
/// <summary>
536+
/// Creates a field declaration for a cached property change name.
537+
/// </summary>
538+
/// <param name="type">The type of cached property change argument (either <see cref="PropertyChangedEventArgs"/> or <see cref="PropertyChangingEventArgs"/>).</param>
539+
/// <param name="propertyName">The name of the cached property name.</param>
540+
/// <returns>A <see cref="FieldDeclarationSyntax"/> instance for the input cached property name.</returns>
541+
[Pure]
542+
private static FieldDeclarationSyntax CreateFieldDeclaration(INamedTypeSymbol type, string propertyName)
543+
{
544+
// Create a static field with a cached property changed/changing argument for a specified property.
545+
// This code produces a field declaration as follows:
546+
//
547+
// [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
548+
// [global::System.Obsolete("This field is not intended to be referenced directly by user code")]
549+
// public static readonly <ARG_TYPE> <PROPERTY_NAME><ARG_TYPE> = new("<PROPERTY_NAME>");
550+
return
551+
FieldDeclaration(
552+
VariableDeclaration(IdentifierName(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)))
553+
.AddVariables(
554+
VariableDeclarator(Identifier($"{propertyName}{type.Name}"))
555+
.WithInitializer(EqualsValueClause(
556+
ImplicitObjectCreationExpression()
557+
.AddArgumentListArguments(Argument(
558+
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(propertyName))))))))
559+
.AddModifiers(
560+
Token(SyntaxKind.PublicKeyword),
561+
Token(SyntaxKind.StaticKeyword),
562+
Token(SyntaxKind.ReadOnlyKeyword))
563+
.AddAttributeLists(
564+
AttributeList(SingletonSeparatedList(
565+
Attribute(IdentifierName("global::System.ComponentModel.EditorBrowsable")).AddArgumentListArguments(
566+
AttributeArgument(ParseExpression("global::System.ComponentModel.EditorBrowsableState.Never"))))),
567+
AttributeList(SingletonSeparatedList(
568+
Attribute(IdentifierName("global::System.Obsolete")).AddArgumentListArguments(
569+
AttributeArgument(LiteralExpression(
570+
SyntaxKind.StringLiteralExpression,
571+
Literal("This field is not intended to be referenced directly by user code")))))));
572+
}
566573
}
567574
}

0 commit comments

Comments
 (0)