Exemple #1
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            // lambda
            var lambda = context.GetNode <LambdaExpression> ();

            if (lambda != null && lambda.ArrowToken.Contains(context.Location))
            {
                if (ContainsLocalReferences(context, lambda, lambda.Body))
                {
                    yield break;
                }

                bool           noReturn = false;
                BlockStatement body;
                if (lambda.Body is BlockStatement)
                {
                    body = (BlockStatement)lambda.Body.Clone();
                }
                else
                {
                    body = new BlockStatement();

                    var type = LambdaHelper.GetLambdaReturnType(context, lambda);
                    if (type == null || type.ReflectionName == "System.Void")
                    {
                        noReturn = true;
                        body.Add(new ExpressionStatement((Expression)lambda.Body.Clone()));
                    }
                    else
                    {
                        body.Add(new ReturnStatement((Expression)lambda.Body.Clone()));
                    }
                }
                var method = GetMethod(context, (LambdaResolveResult)context.Resolve(lambda), body, noReturn);
                yield return(GetAction(context, lambda, method));
            }

            // anonymous method
            var anonymousMethod = context.GetNode <AnonymousMethodExpression> ();

            if (anonymousMethod != null && anonymousMethod.DelegateToken.Contains(context.Location))
            {
                if (ContainsLocalReferences(context, anonymousMethod, anonymousMethod.Body))
                {
                    yield break;
                }

                var method = GetMethod(context, (LambdaResolveResult)context.Resolve(anonymousMethod),
                                       (BlockStatement)anonymousMethod.Body.Clone());
                yield return(GetAction(context, anonymousMethod, method));
            }
        }
Exemple #2
0
        static bool RequireReturnStatement(RefactoringContext context, LambdaExpression lambda)
        {
            var type = LambdaHelper.GetLambdaReturnType(context, lambda);

            return(type != null && type.ReflectionName != "System.Void");
        }