Skip to content

Commit 213c876

Browse files
committed
Adjust signature for TryGetInfo method
1 parent df6c49e commit 213c876

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Immutable;
66
using System.ComponentModel;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Globalization;
89
using System.Linq;
910
using System.Threading;
@@ -33,13 +34,15 @@ internal static class Execute
3334
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
3435
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current run.</param>
3536
/// <param name="token">The cancellation token for the current operation.</param>
37+
/// <param name="propertyInfo">The resulting <see cref="PropertyInfo"/> value, if successfully retrieved.</param>
3638
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
3739
/// <returns>The resulting <see cref="PropertyInfo"/> instance for <paramref name="fieldSymbol"/>, if successful.</returns>
38-
public static PropertyInfo? TryGetInfo(
40+
public static bool TryGetInfo(
3941
FieldDeclarationSyntax fieldSyntax,
4042
IFieldSymbol fieldSymbol,
4143
SemanticModel semanticModel,
4244
CancellationToken token,
45+
[NotNullWhen(true)] out PropertyInfo? propertyInfo,
4346
out ImmutableArray<Diagnostic> diagnostics)
4447
{
4548
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
@@ -53,9 +56,10 @@ internal static class Execute
5356
fieldSymbol.ContainingType,
5457
fieldSymbol.Name);
5558

59+
propertyInfo = null;
5660
diagnostics = builder.ToImmutable();
5761

58-
return null;
62+
return false;
5963
}
6064

6165
// Get the property type and name
@@ -72,12 +76,13 @@ internal static class Execute
7276
fieldSymbol.ContainingType,
7377
fieldSymbol.Name);
7478

79+
propertyInfo = null;
7580
diagnostics = builder.ToImmutable();
7681

7782
// If the generated property would collide, skip generating it entirely. This makes sure that
7883
// users only get the helpful diagnostic about the collision, and not the normal compiler error
7984
// about a definition for "Property" already existing on the target type, which might be confusing.
80-
return null;
85+
return false;
8186
}
8287

8388
// Check for special cases that are explicitly not allowed
@@ -89,9 +94,10 @@ internal static class Execute
8994
fieldSymbol.ContainingType,
9095
fieldSymbol.Name);
9196

97+
propertyInfo = null;
9298
diagnostics = builder.ToImmutable();
9399

94-
return null;
100+
return false;
95101
}
96102

97103
ImmutableArray<string>.Builder propertyChangedNames = ImmutableArray.CreateBuilder<string>();
@@ -238,9 +244,7 @@ internal static class Execute
238244
fieldSymbol.Name);
239245
}
240246

241-
diagnostics = builder.ToImmutable();
242-
243-
return new(
247+
propertyInfo = new PropertyInfo(
244248
typeNameWithNullabilityAnnotations,
245249
fieldName,
246250
propertyName,
@@ -250,6 +254,10 @@ internal static class Execute
250254
notifyRecipients,
251255
notifyDataErrorInfo,
252256
forwardedAttributes.ToImmutable());
257+
258+
diagnostics = builder.ToImmutable();
259+
260+
return true;
253261
}
254262

255263
/// <summary>

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4040
FieldDeclarationSyntax fieldDeclaration = (FieldDeclarationSyntax)context.TargetNode.Parent!.Parent!;
4141
IFieldSymbol fieldSymbol = (IFieldSymbol)context.TargetSymbol;
4242

43-
// Produce the incremental models
43+
// Get the hierarchy info for the target symbol, and try to gather the property info
4444
HierarchyInfo hierarchy = HierarchyInfo.From(fieldSymbol.ContainingType);
45-
PropertyInfo? propertyInfo = Execute.TryGetInfo(fieldDeclaration, fieldSymbol, context.SemanticModel, token, out ImmutableArray<Diagnostic> diagnostics);
45+
46+
_ = Execute.TryGetInfo(fieldDeclaration, fieldSymbol, context.SemanticModel, token, out PropertyInfo? propertyInfo, out ImmutableArray<Diagnostic> diagnostics);
4647

4748
return (Hierarchy: hierarchy, new Result<PropertyInfo?>(propertyInfo, diagnostics));
4849
})

0 commit comments

Comments
 (0)