Ejemplo n.º 1
0
        /// <summary>
        /// Determine whether a binary expression with a string expression is suitable for replacement.
        /// </summary>
        /// <param name="left">A node representing a string expression.</param>
        /// <param name="right">A node to be tested.</param>
        /// <param name="operatorToken">The operator separating the nodes.</param>
        /// <returns></returns>
        static ShouldReplaceResult ShouldReplaceString(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax left, ExpressionSyntax right, SyntaxToken operatorToken)
        {
            var result = new ShouldReplaceResult();

            result.ShouldReplace = false;

            // str == null or str != null
            if (IsNullSyntax(nodeContext, right))
            {
                result.IsNullTest    = true;
                result.ShouldReplace = true;
            }
            // str == "" or str != ""
            // str == string.Empty or str != string.Empty
            else if (IsEmptySyntax(nodeContext, right))
            {
                result.IsEmptyTest   = true;
                result.ShouldReplace = true;
            }

            if (result.ShouldReplace)
            {
                result.IdentifierNode = left;

                if (operatorToken.IsKind(SyntaxKind.ExclamationEqualsToken))
                {
                    result.IsNegative = true;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether a binary expression with a string length expression is suitable for replacement.
        /// </summary>
        /// <param name="left">A node representing a string length expression.</param>
        /// <param name="right">A node to be tested.</param>
        /// <param name="operatorToken">The operator separating the nodes.</param>
        /// <returns></returns>
        static ShouldReplaceResult ShouldReplaceStringLength(MemberAccessExpressionSyntax left, ExpressionSyntax right, SyntaxToken operatorToken)
        {
            const string zeroLiteral = "0";
            const string oneLiteral  = "1";

            var result = new ShouldReplaceResult();

            result.ShouldReplace = false;

            // str.Length == 0 or str.Length <= 0
            if (operatorToken.IsKind(SyntaxKind.EqualsEqualsToken, SyntaxKind.LessThanEqualsToken) && string.Equals(zeroLiteral, right.ToString()))
            {
                result.IsEmptyTest   = true;
                result.ShouldReplace = true;
            }
            // str.Length < 1
            else if (operatorToken.IsKind(SyntaxKind.LessThanToken) && string.Equals(oneLiteral, right.ToString()))
            {
                result.IsEmptyTest   = true;
                result.ShouldReplace = true;
            }
            // str.Length != 0 or str.Length > 0
            else if (operatorToken.IsKind(SyntaxKind.ExclamationEqualsToken, SyntaxKind.GreaterThanToken) && string.Equals(zeroLiteral, right.ToString()))
            {
                result.IsEmptyTest   = true;
                result.IsNegative    = true;
                result.ShouldReplace = true;
            }
            // str.Length >= 1
            else if (operatorToken.IsKind(SyntaxKind.GreaterThanEqualsToken) && string.Equals(oneLiteral, right.ToString()))
            {
                result.IsEmptyTest   = true;
                result.IsNegative    = true;
                result.ShouldReplace = true;
            }

            if (result.ShouldReplace)
            {
                result.IdentifierNode = left.Expression;
            }

            return(result);
        }
        /// <summary>
        /// Determines whether a binary expression with a string length expression is suitable for replacement.
        /// </summary>
        /// <param name="left">A node representing a string length expression.</param>
        /// <param name="right">A node to be tested.</param>
        /// <param name="operatorToken">The operator separating the nodes.</param>
        /// <returns></returns>
        static ShouldReplaceResult ShouldReplaceStringLength(MemberAccessExpressionSyntax left, ExpressionSyntax right, SyntaxToken operatorToken)
        {
            const string zeroLiteral = "0";
            const string oneLiteral = "1";

            var result = new ShouldReplaceResult();
            result.ShouldReplace = false;

            // str.Length == 0 or str.Length <= 0
            if (operatorToken.IsKind(SyntaxKind.EqualsEqualsToken, SyntaxKind.LessThanEqualsToken) && string.Equals(zeroLiteral, right.ToString()))
            {
                result.IsEmptyTest = true;
                result.ShouldReplace = true;
            }
            // str.Length < 1
            else if (operatorToken.IsKind(SyntaxKind.LessThanToken) && string.Equals(oneLiteral, right.ToString()))
            {
                result.IsEmptyTest = true;
                result.ShouldReplace = true;
            }
            // str.Length != 0 or str.Length > 0
            else if (operatorToken.IsKind(SyntaxKind.ExclamationEqualsToken, SyntaxKind.GreaterThanToken) && string.Equals(zeroLiteral, right.ToString()))
            {
                result.IsEmptyTest = true;
                result.IsNegative = true;
                result.ShouldReplace = true;
            }
            // str.Length >= 1
            else if (operatorToken.IsKind(SyntaxKind.GreaterThanEqualsToken) && string.Equals(oneLiteral, right.ToString()))
            {
                result.IsEmptyTest = true;
                result.IsNegative = true;
                result.ShouldReplace = true;
            }

            if (result.ShouldReplace)
            {
                result.IdentifierNode = left.Expression;
            }

            return result;

        }
        /// <summary>
        /// Determine whether a binary expression with a string expression is suitable for replacement.
        /// </summary>
        /// <param name="left">A node representing a string expression.</param>
        /// <param name="right">A node to be tested.</param>
        /// <param name="operatorToken">The operator separating the nodes.</param>
        /// <returns></returns>
        static ShouldReplaceResult ShouldReplaceString(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax left, ExpressionSyntax right, SyntaxToken operatorToken)
        {
            var result = new ShouldReplaceResult();
            result.ShouldReplace = false;

            // str == null or str != null
            if (IsNullSyntax(nodeContext, right))
            {
                result.IsNullTest = true;
                result.ShouldReplace = true;
            }
            // str == "" or str != ""
            // str == string.Empty or str != string.Empty
            else if (IsEmptySyntax(nodeContext, right))
            {
                result.IsEmptyTest = true;
                result.ShouldReplace = true;
            }

            if (result.ShouldReplace)
            {
                result.IdentifierNode = left;

                if (operatorToken.IsKind(SyntaxKind.ExclamationEqualsToken))
                {
                    result.IsNegative = true;
                }
            }

            return result;
        }