private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
        {
            if (context.IsExcludedFromAnalysis())
            {
                return;
            }

            var methodDeclaration = context.Node as MethodDeclarationSyntax;

            if (methodDeclaration == null ||
                methodDeclaration.IsMissing)
            {
                return;
            }

            var method = context.ContainingSymbol as IMethodSymbol;

            if (method == null)
            {
                return;
            }

            if (ClrMethod.IsAttachedSetMethod(method, context.SemanticModel, context.CancellationToken, out IFieldSymbol setField))
            {
                CheckName(context, setField, method, methodDeclaration, "Set");
                return;
            }

            if (ClrMethod.IsAttachedGetMethod(method, context.SemanticModel, context.CancellationToken, out IFieldSymbol getField))
            {
                CheckName(context, getField, method, methodDeclaration, "Get");
            }
        }
Exemplo n.º 2
0
        private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
        {
            var methodDeclaration = context.Node as MethodDeclarationSyntax;

            if (methodDeclaration == null || methodDeclaration.IsMissing)
            {
                return;
            }

            var method = context.ContainingSymbol as IMethodSymbol;

            if (method == null)
            {
                return;
            }

            IFieldSymbol getField;

            if (ClrMethod.IsAttachedGetMethod(method, context.SemanticModel, context.CancellationToken, out getField))
            {
                ITypeSymbol registeredType;
                if (DependencyProperty.TryGetRegisteredType(getField, context.SemanticModel, context.CancellationToken, out registeredType))
                {
                    if (!method.ReturnType.IsSameType(registeredType))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, methodDeclaration.ReturnType.GetLocation(), "Return type", registeredType));
                    }
                }

                return;
            }

            IFieldSymbol setField;

            if (ClrMethod.IsAttachedSetMethod(method, context.SemanticModel, context.CancellationToken, out setField))
            {
                ITypeSymbol registeredType;
                if (DependencyProperty.TryGetRegisteredType(
                        setField,
                        context.SemanticModel,
                        context.CancellationToken,
                        out registeredType))
                {
                    if (!method.Parameters[1].Type.IsSameType(registeredType))
                    {
                        context.ReportDiagnostic(
                            Diagnostic.Create(
                                Descriptor,
                                methodDeclaration.ParameterList.Parameters[1].GetLocation(),
                                "Value type",
                                registeredType));
                    }
                }
            }
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        /// <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;
                }

                if (diagnostic.Id == WPF0004ClrMethodShouldMatchRegisteredName.DiagnosticId)
                {
                    var methodDeclaration = syntaxRoot.FindNode(diagnostic.Location.SourceSpan)
                                            .FirstAncestorOrSelf <MethodDeclarationSyntax>();

                    if (methodDeclaration == null || methodDeclaration.IsMissing)
                    {
                        continue;
                    }

                    var method = semanticModel.GetDeclaredSymbol(methodDeclaration) as IMethodSymbol;

                    if (method == null)
                    {
                        continue;
                    }

                    if (ClrMethod.IsAttachedSetMethod(
                            method,
                            semanticModel,
                            context.CancellationToken,
                            out IFieldSymbol backingField))
                    {
                        TryUpdateName(
                            context,
                            backingField,
                            semanticModel,
                            syntaxRoot,
                            token,
                            "Set",
                            diagnostic);

                        continue;
                    }

                    if (ClrMethod.IsAttachedGetMethod(
                            method,
                            semanticModel,
                            context.CancellationToken,
                            out backingField))
                    {
                        TryUpdateName(
                            context,
                            backingField,
                            semanticModel,
                            syntaxRoot,
                            token,
                            "Get",
                            diagnostic);
                    }
                }
                else if (diagnostic.Id == WPF0005PropertyChangedCallbackShouldMatchRegisteredName.DiagnosticId)
                {
                    var node     = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
                    var callback = node.FirstAncestorOrSelf <ArgumentSyntax>();
                    if (WPF0005PropertyChangedCallbackShouldMatchRegisteredName.TryGetIdentifierAndRegisteredName(
                            callback,
                            semanticModel,
                            context.CancellationToken,
                            out IdentifierNameSyntax _,
                            out string registeredName))
                    {
                        var newName = $"On{registeredName}Changed";
                        context.RegisterCodeFix(
                            CodeAction.Create(
                                $"Rename to: {newName}",
                                cancellationToken => RenameHelper.RenameSymbolAsync(context.Document, syntaxRoot, token, newName, cancellationToken),
                                this.GetType().FullName),
                            diagnostic);
                    }
                }
                else if (diagnostic.Id == WPF0006CoerceValueCallbackShouldMatchRegisteredName.DiagnosticId)
                {
                    var node     = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
                    var callback = node.FirstAncestorOrSelf <ArgumentSyntax>();
                    if (WPF0006CoerceValueCallbackShouldMatchRegisteredName.TryGetIdentifierAndRegisteredName(
                            callback,
                            semanticModel,
                            context.CancellationToken,
                            out IdentifierNameSyntax _,
                            out string registeredName))
                    {
                        var newName = $"Coerce{registeredName}";
                        context.RegisterCodeFix(
                            CodeAction.Create(
                                $"Rename to: {newName}",
                                cancellationToken => RenameHelper.RenameSymbolAsync(context.Document, syntaxRoot, token, newName, cancellationToken),
                                this.GetType().FullName),
                            diagnostic);
                    }
                }
                else if (diagnostic.Id == WPF0007ValidateValueCallbackCallbackShouldMatchRegisteredName.DiagnosticId)
                {
                    var node     = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
                    var callback = node.FirstAncestorOrSelf <ArgumentSyntax>();
                    if (WPF0007ValidateValueCallbackCallbackShouldMatchRegisteredName.TryGetIdentifierAndRegisteredName(
                            callback,
                            semanticModel,
                            context.CancellationToken,
                            out IdentifierNameSyntax _,
                            out string registeredName))
                    {
                        var newName = $"{registeredName}ValidateValue";
                        context.RegisterCodeFix(
                            CodeAction.Create(
                                $"Rename to: {newName}",
                                cancellationToken => RenameHelper.RenameSymbolAsync(context.Document, syntaxRoot, token, newName, cancellationToken),
                                this.GetType().FullName),
                            diagnostic);
                    }
                }
            }
        }