Skip to content

Commit ef70aab

Browse files
committed
Pass the location of the target field
1 parent e481f48 commit ef70aab

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Diagnostics/Analyzers/UseGeneratedDependencyPropertyOnManualPropertyAnalyzer.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,15 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
392392
return;
393393
}
394394

395+
// Find the parent field for the operation (we're guaranteed to only fine one)
396+
if (context.Operation.Syntax.FirstAncestor<FieldDeclarationSyntax>()?.GetLocation() is not Location fieldLocation)
397+
{
398+
return;
399+
}
400+
395401
fieldFlags.PropertyName = propertyName;
396402
fieldFlags.PropertyType = propertyTypeSymbol;
397-
fieldFlags.IsInitializerValid = true;
403+
fieldFlags.FieldLocation = fieldLocation;
398404
}, OperationKind.FieldInitializer);
399405

400406
// Finally, we can consume this information when we finish processing the symbol
@@ -421,14 +427,27 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
421427
continue;
422428
}
423429

424-
// Validate the combination of accessors and target field, and warn if that's the case
425-
if (fieldFlags.PropertyName == pair.Key.Name &&
426-
SymbolEqualityComparer.Default.Equals(fieldFlags.PropertyType, pair.Key.Type) &&
427-
fieldFlags.IsInitializerValid)
430+
// We only support rewriting when the property name matches the field being initialized.
431+
// Note that the property name here is the literal being passed for the 'name' parameter.
432+
if (fieldFlags.PropertyName != pair.Key.Name)
433+
{
434+
continue;
435+
}
436+
437+
// Make sure that the 'propertyType' value matches the actual type of the property.
438+
// We are intentionally not handling combinations of nullable value types here.
439+
if (!SymbolEqualityComparer.Default.Equals(fieldFlags.PropertyType, pair.Key.Type))
440+
{
441+
return;
442+
}
443+
444+
// Finally, check whether the field was valid (if so, we will have a valid location)
445+
if (fieldFlags.FieldLocation is Location fieldLocation)
428446
{
429447
context.ReportDiagnostic(Diagnostic.Create(
430448
UseGeneratedDependencyPropertyForManualProperty,
431449
pair.Key.Locations.FirstOrDefault(),
450+
[fieldLocation],
432451
pair.Key));
433452
}
434453
}
@@ -448,7 +467,7 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
448467
{
449468
fieldFlags.PropertyName = null;
450469
fieldFlags.PropertyType = null;
451-
fieldFlags.IsInitializerValid = false;
470+
fieldFlags.FieldLocation = null;
452471

453472
fieldFlagsStack.Push(fieldFlags);
454473
}
@@ -518,8 +537,8 @@ private sealed class FieldFlags
518537
public ITypeSymbol? PropertyType;
519538

520539
/// <summary>
521-
/// Whether the initializer is valid.
540+
/// The location of the target field being initialized.
522541
/// </summary>
523-
public bool IsInitializerValid;
542+
public Location? FieldLocation;
524543
}
525544
}

0 commit comments

Comments
 (0)