Example #1
0
            public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
            {
                base.VisitIfElseStatement(ifElseStatement);
                var match = ifPattern.Match(ifElseStatement);

                if (match.Success)
                {
                    var cond1 = match.Get <Expression>("condition").Single();
                    var cond2 = match.Get <Expression>("condition2").Single();
                    if (!AlUtil.AreConditionsEqual(cond1, cond2))
                    {
                        return;
                    }
                    AddIssue(new CodeIssue(
                                 ifElseStatement.IfToken,
                                 ctx.TranslateString("Statement can be simplified to 'while' statement"),
                                 ctx.TranslateString("Replace with 'while'"),
                                 script => {
                        script.Replace(
                            ifElseStatement,
                            new WhileStatement(
                                cond1.Clone(),
                                match.Get <Statement>("EmbeddedStatement").Single().Clone()
                                )
                            );
                    }
                                 )
                    {
                        IssueMarker = IssueMarker.DottedLine
                    });
                }
            }
            public override void VisitInvocationExpression(InvocationExpression invocationExpression)
            {
                base.VisitInvocationExpression(invocationExpression);
                var rr = ctx.Resolve(invocationExpression) as InvocationResolveResult;

                if (rr == null || rr.Member.Name != "Equals" || !rr.Member.ReturnType.IsKnownType(KnownTypeCode.Boolean))
                {
                    return;
                }

                if (rr.Member.IsStatic)
                {
                    if (rr.Member.Parameters.Count != 2)
                    {
                        return;
                    }
                    if (AlUtil.AreConditionsEqual(invocationExpression.Arguments.FirstOrDefault(), invocationExpression.Arguments.Last()))
                    {
                        if ((invocationExpression.Parent is UnaryOperatorExpression) && ((UnaryOperatorExpression)invocationExpression.Parent).Operator == UnaryOperatorType.Not)
                        {
                            AddIssue(invocationExpression.Parent, invocationExpression.Parent, false);
                        }
                        else
                        {
                            AddIssue(invocationExpression, invocationExpression, true);
                        }
                    }
                }
                else
                {
                    if (rr.Member.Parameters.Count != 1)
                    {
                        return;
                    }
                    var target = invocationExpression.Target as MemberReferenceExpression;
                    if (target == null)
                    {
                        return;
                    }
                    if (AlUtil.AreConditionsEqual(invocationExpression.Arguments.FirstOrDefault(), target.Target))
                    {
                        if ((invocationExpression.Parent is UnaryOperatorExpression) && ((UnaryOperatorExpression)invocationExpression.Parent).Operator == UnaryOperatorType.Not)
                        {
                            AddIssue(invocationExpression.Parent, invocationExpression.Parent, false);
                        }
                        else
                        {
                            AddIssue(invocationExpression, invocationExpression, true);
                        }
                    }
                }
            }