コード例 #1
0
            public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
            {
                base.VisitIfElseStatement(ifElseStatement);

                if (ifElseStatement.Parent is IfElseStatement)
                {
                    return;
                }
                Expression c, e1, e2;
                AstNode    rs;

                if (!ConvertIfStatementToReturnStatementAction.GetMatch(ifElseStatement, out c, out e1, out e2, out rs))
                {
                    return;
                }
                if (ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexCondition(c) ||
                    ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexExpression(e1) ||
                    ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexExpression(e2))
                {
                    return;
                }
                AddIssue(new CodeIssue(
                             ifElseStatement.IfToken,
                             ctx.TranslateString("Convert to 'return' statement")
                             )
                {
                    IssueMarker = IssueMarker.DottedLine, ActionProvider = { typeof(ConvertIfStatementToReturnStatementAction) }
                });
            }
        protected override CodeAction GetAction(RefactoringContext context, IfElseStatement ifElseStatement)
        {
            if (!ifElseStatement.IfToken.Contains(context.Location))
            {
                return(null);
            }

            Expression c, e1, e2;
            AstNode    rs;

            if (!ConvertIfStatementToReturnStatementAction.GetMatch(ifElseStatement, out c, out e1, out e2, out rs))
            {
                return(null);
            }
            return(new CodeAction(
                       context.TranslateString("Replace with 'return'"),
                       script => {
                script.Replace(ifElseStatement, new ReturnStatement(
                                   CreateCondition(c, e1, e2)
                                   ));
                if (rs != null)
                {
                    script.Remove(rs);
                }
            },
                       ifElseStatement
                       ));
        }