Exemplo n.º 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) }
                });
            }
Exemplo n.º 2
0
            public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
            {
                base.VisitIfElseStatement(ifElseStatement);
                Expression rightSide;
                var        leftSide = ConvertIfStatementToNullCoalescingExpressionAction.CheckNode(ifElseStatement, out rightSide);

                if (leftSide == null)
                {
                    return;
                }
                if (ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexExpression(leftSide) ||
                    ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexExpression(rightSide))
                {
                    return;
                }
                var previousNode = ifElseStatement.GetPrevSibling(sibling => sibling is Statement) as VariableDeclarationStatement;

                if (previousNode == null || ConvertIfStatementToConditionalTernaryExpressionIssue.IsComplexExpression(previousNode))
                {
                    return;
                }

                AddIssue(new CodeIssue(
                             ifElseStatement.IfToken,
                             ctx.TranslateString("Convert to '??' expresssion")
                             )
                {
                    IssueMarker = IssueMarker.DottedLine, ActionProvider = { typeof(ConvertIfStatementToNullCoalescingExpressionAction) }
                });
            }