protected override void Run(IInvocationExpression element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var methodIdentifier = element.GetSolution().GetComponent <IMoqMethodIdentifier>();

            if (!methodIdentifier.IsMoqCallbackMethod(element))
            {
                return;
            }

            var expectedTypeParameters = element.TypeArguments;

            if (expectedTypeParameters.Count == 0)
            {
                return;
            }

            var pointer = element.InvokedExpression;
            TreeNodeCollection <ICSharpArgument>?arguments = null;

            while (pointer != null && arguments == null && pointer.FirstChild is IInvocationExpression methodInvocation)
            {
                arguments = GetArguments(element.GetSolution(), methodInvocation);
                pointer   = methodInvocation.InvokedExpression;
            }

            if (arguments == null)
            {
                return;
            }

            var actualTypesParameters = arguments.Value.Select(x => x.Value.Type()).ToArray();
            var rule = element.GetPsiModule().GetTypeConversionRule();

            if (actualTypesParameters.Length <= 0)
            {
                return;
            }

            if (expectedTypeParameters.Count != actualTypesParameters.Length)
            {
                AddWarning(element, consumer);
            }
            else
            {
                for (var i = 0; i < expectedTypeParameters.Count; i++)
                {
                    var actualParameterType   = actualTypesParameters[i];
                    var expectedParameterType = expectedTypeParameters[i];

                    if (!actualParameterType.Equals(expectedParameterType) &&
                        !actualParameterType.IsImplicitlyConvertibleTo(expectedParameterType, rule))
                    {
                        AddWarning(element, consumer);
                    }
                }
            }
        }
        public override MethodInvocation ProcessUsage(IReference reference)
        {
            var referenceExpression = reference.GetTreeNode() as IReferenceExpression;

            if (referenceExpression == null)
            {
                Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage"));
                return(null);
            }

            bool isExtensionMethod           = referenceExpression.IsExtensionMethod();
            IInvocationExpression invocation = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

            if (invocation == null)
            {
                Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage"));
                return(null);
            }

            ITreeNode element = GetArgument(invocation, isExtensionMethod);

            var   argument = element as ICSharpArgument;
            IType type     = argument != null?GetTypeOfValue(argument.Value) : GetTypeOfValue(element);

            if (type == null || !type.CanUseExplicitly(invocation))
            {
                Driver.AddConflict(ReferenceConflict.CreateError(
                                       reference, "Argument of {0} is not valid 'typeof' expression.", "usage"));
                return(null);
            }

            // we can rely on resolve result since method declaration is not yet changed.
            ResolveResultWithInfo resolveResult = reference.Resolve();
            ISubstitution         substitution  = resolveResult.Result.Substitution;
            var method = resolveResult.DeclaredElement as IMethod;

            if (method == null)
            {
                return(null);
            }

            if (argument != null)
            {
                invocation.RemoveArgument(argument);
                return(new MethodInvocation(reference, type, method, substitution));
            }

            CSharpElementFactory factory = CSharpElementFactory.GetInstance(invocation.GetPsiModule());
            IReferenceExpression newInvokedExpression =
                invocation.InvokedExpression.ReplaceBy(factory.CreateReferenceExpression("$0", Executer.Method));

            return(new MethodInvocation(newInvokedExpression.Reference, type, method, substitution));
        }
Exemple #3
0
        private MethodSignature GetMethodSignature(IInvocationExpression invocationExpression, IMethod invokedMethod, bool isCoroutine)
        {
            var predefinedType = myPredefinedTypeCache.GetOrCreatePredefinedType(invocationExpression.GetPsiModule());
            var returnType     = isCoroutine ? predefinedType.IEnumerator : predefinedType.Void;

            if (invokedMethod.ShortName == "StartCoroutine")
            {
                var arguments = invocationExpression.ArgumentList.Arguments;
                if (arguments.Count == 2)
                {
                    var argumentValue = arguments[1].Value;
                    if (argumentValue != null)
                    {
                        var argumentType = argumentValue.Type();
                        var argumentName = invokedMethod.Parameters.FirstOrDefault()?.ShortName ?? "value";
                        return(new MethodSignature(returnType, false, new[] { argumentType }, new[] { argumentName }));
                    }
                }
            }

            return(new MethodSignature(returnType, false));
        }
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression != null)
            {
                IReferenceExpression referenceExpressionNode = invokedExpression as IReferenceExpression;

                if (referenceExpressionNode != null)
                {
                    IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;
                    if (referenceExpression != null)
                    {
                        ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;
                        if (qualifierExpression is IBaseExpression)
                        {
                            string methodName = referenceExpressionNode.NameIdentifier.Name;

                            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode<ICSharpTypeDeclaration>(true);

                            if (typeDeclaration != null)
                            {
                                foreach (ICSharpTypeMemberDeclaration memberDeclaration in typeDeclaration.MemberDeclarations)
                                {
                                    if (memberDeclaration.DeclaredName == methodName)
                                    {
                                        IMethodDeclaration methodDeclaration = memberDeclaration as IMethodDeclaration;
                                        if (methodDeclaration != null)
                                        {
                                            isOverride = methodDeclaration.IsOverride;
                                            isNew = methodDeclaration.IsNew();
                                            break;
                                        }
                                    }
                                }

                                if (isOverride || isNew || methodName.Equals("Equals", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return;
                                }

                                using (WriteLockCookie.Create(true))
                                {
                                    // swap the base to this
                                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                                    referenceExpression.SetQualifierExpression(expression);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression != null)
            {
                IReferenceExpression referenceExpressionNode = invokedExpression as IReferenceExpression;

                if (referenceExpressionNode != null)
                {
                    IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;
                    if (referenceExpression != null)
                    {
                        ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;
                        if (qualifierExpression is IBaseExpression)
                        {
                            string methodName = referenceExpressionNode.NameIdentifier.Name;

                            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode <ICSharpTypeDeclaration>(true);

                            if (typeDeclaration != null)
                            {
                                foreach (ICSharpTypeMemberDeclaration memberDeclaration in typeDeclaration.MemberDeclarations)
                                {
                                    if (memberDeclaration.DeclaredName == methodName)
                                    {
                                        IMethodDeclaration methodDeclaration = memberDeclaration as IMethodDeclaration;
                                        if (methodDeclaration != null)
                                        {
                                            isOverride = methodDeclaration.IsOverride;
                                            isNew      = methodDeclaration.IsNew();
                                            break;
                                        }
                                    }
                                }

                                if (isOverride || isNew)
                                {
                                    return;
                                }

                                using (WriteLockCookie.Create(true))
                                {
                                    // swap the base to this
                                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                                    referenceExpression.SetQualifierExpression(expression);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression == null)
            {
                return;
            }

            IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;

            if (referenceExpression == null)
            {
                return;
            }

            ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;

            if (!(qualifierExpression is IBaseExpression))
            {
                return;
            }

            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode <ICSharpTypeDeclaration>(true);

            if (typeDeclaration == null)
            {
                return;
            }

            ITypeElement typeDeclaredElement = typeDeclaration.DeclaredElement;

            if (typeDeclaredElement == null)
            {
                return;
            }

            IDeclaredElement referenceDeclaredElement = referenceExpression.Reference.Resolve().DeclaredElement;

            if (referenceDeclaredElement == null)
            {
                return;
            }

            foreach (var member in typeDeclaredElement.GetAllClassMembers(referenceDeclaredElement.ShortName))
            {
                if (!member.Equals(referenceDeclaredElement))
                {
                    continue;
                }

                using (WriteLockCookie.Create(true))
                {
                    // swap the base to this
                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                    referenceExpression.SetQualifierExpression(expression);
                }
            }
        }