private static void HandleDeclaration(SyntaxNodeAnalysisContext context) { var propertyDeclaration = context.Node as PropertyDeclarationSyntax; if (propertyDeclaration == null || propertyDeclaration.IsMissing) { return; } var property = context.ContainingSymbol as IPropertySymbol; if (property == null || !property.IsPotentialClrProperty()) { return; } ITypeSymbol registeredType; if (ClrProperty.TryGetRegisteredType(propertyDeclaration, context.SemanticModel, context.CancellationToken, out registeredType)) { if (!registeredType.IsSameType(property.Type)) { context.ReportDiagnostic(Diagnostic.Create(Descriptor, propertyDeclaration.Type.GetLocation(), property, registeredType)); } } }
private static void HandleAssignment(SyntaxNodeAnalysisContext context) { if (IsInObjectInitializer(context.Node) || IsInConstructor(context.Node)) { return; } var assignment = context.Node as AssignmentExpressionSyntax; if (assignment == null || assignment.IsMissing || context.SemanticModel == null) { return; } var property = context.SemanticModel.GetSymbolInfo(assignment.Left, context.CancellationToken).Symbol as IPropertySymbol; IFieldSymbol field; if (ClrProperty.TryGetSingleBackingField(property, context.SemanticModel, context.CancellationToken, out field)) { if (IsCalleePotentiallyCreatedInScope(assignment.Left as MemberAccessExpressionSyntax, context.SemanticModel, context.CancellationToken)) { return; } var propertyArg = DependencyProperty.CreateArgument(field, context.SemanticModel, context.Node.SpanStart); context.ReportDiagnostic(Diagnostic.Create(Descriptor, assignment.GetLocation(), propertyArg, assignment.Right)); } }
private static void HandleDeclaration(SyntaxNodeAnalysisContext context) { var propertyDeclaration = context.Node as PropertyDeclarationSyntax; if (propertyDeclaration == null || propertyDeclaration.IsMissing) { return; } var property = context.ContainingSymbol as IPropertySymbol; if (property == null || !property.IsPotentialClrProperty()) { return; } string registeredName; if (ClrProperty.TryGetRegisteredName(propertyDeclaration, context.SemanticModel, context.CancellationToken, out registeredName)) { if (registeredName != property.Name) { var identifier = propertyDeclaration.Identifier; context.ReportDiagnostic(Diagnostic.Create(Descriptor, identifier.GetLocation(), property.Name, registeredName)); } } }
internal static bool TryCreateProperty(AssignmentExpressionSyntax assignment, SemanticModel semanticModel, CancellationToken cancellationToken, out ArgumentSyntax result) { result = null; var property = semanticModel.GetSymbolInfo(assignment.Left, cancellationToken).Symbol as IPropertySymbol; if (ClrProperty.TryGetSingleBackingField(property, semanticModel, cancellationToken, out IFieldSymbol fieldSymbol)) { result = DependencyProperty.CreateArgument(fieldSymbol, semanticModel, assignment.SpanStart); } return(result != null); }
private static void HandleInvocation(SyntaxNodeAnalysisContext context) { if (context.IsExcludedFromAnalysis()) { return; } var invocation = context.Node as InvocationExpressionSyntax; if (invocation == null || context.SemanticModel == null) { return; } if (IsInObjectInitializer(context.Node) || IsInConstructor(context.Node)) { return; } if (!DependencyObject.TryGetSetValueArguments(invocation, context.SemanticModel, context.CancellationToken, out ArgumentSyntax property, out IFieldSymbol setField, out ArgumentSyntax value)) { return; } if (setField == null || setField.Type != KnownSymbol.DependencyProperty || setField == KnownSymbol.FrameworkElement.DataContextProperty) { return; } var clrProperty = context.ContainingProperty(); if (ClrProperty.IsDependencyPropertyAccessor(clrProperty, context.SemanticModel, context.CancellationToken)) { return; } var clrMethod = context.ContainingSymbol as IMethodSymbol; if (ClrMethod.IsAttachedSetMethod(clrMethod, context.SemanticModel, context.CancellationToken, out setField)) { return; } if (IsCalleePotentiallyCreatedInScope(invocation.Expression as MemberAccessExpressionSyntax, context.SemanticModel, context.CancellationToken)) { return; } context.ReportDiagnostic(Diagnostic.Create(Descriptor, invocation.GetLocation(), property, value)); }
private static void HandleDeclaration(SyntaxNodeAnalysisContext context) { if (context.IsExcludedFromAnalysis()) { return; } var propertyDeclaration = context.Node as PropertyDeclarationSyntax; if (propertyDeclaration == null || propertyDeclaration.IsMissing) { return; } if (ClrProperty.TryGetBackingFields( propertyDeclaration, context.SemanticModel, context.CancellationToken, out IFieldSymbol getter, out IFieldSymbol setter)) { if (DependencyProperty.TryGetDependencyPropertyKeyField( getter, context.SemanticModel, context.CancellationToken, out IFieldSymbol keyField)) { getter = keyField; } if (ReferenceEquals(getter, setter)) { return; } context.ReportDiagnostic( Diagnostic.Create( Descriptor, propertyDeclaration.GetLocation(), context.ContainingSymbol.Name)); } }
/// <inheritdoc/> public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var syntaxRoot = await document.GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false); var semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); foreach (var diagnostic in context.Diagnostics) { var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); if (string.IsNullOrEmpty(token.ValueText)) { continue; } var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan); if (node == null || node.IsMissing) { continue; } var property = semanticModel.SemanticModelFor(node) .GetDeclaredSymbol(node, context.CancellationToken) as IPropertySymbol; string registeredName; if (ClrProperty.TryGetRegisteredName( property, semanticModel, context.CancellationToken, out registeredName)) { context.RegisterCodeFix( CodeAction.Create( $"Rename to: {registeredName}.", _ => ApplyFixAsync(context, token, registeredName), this.GetType().Name), diagnostic); } } }