/// <summary>
        /// Fixes the error.
        /// </summary>
        /// <param name="violationContext">Context for the violation.</param>
        /// <param name="token">The token that is missing a prefix.</param>
        private void FixPrefixLocalCallsWithThisViolation(ViolationContext violationContext, Token token)
        {
            Param.Ignore(violationContext);
            Param.AssertNotNull(token, "token");

            CsDocument document = token.Document;

            // Find the item that is going to get replaced.
            CodeUnit          itemToReplace = token;
            LiteralExpression parent        = token.Parent as LiteralExpression;

            if (parent != null)
            {
                itemToReplace = parent;
            }

            // Record the location of this item.
            CodeUnitLocationMarker marker = document.GetCodeUnitLocationMarker(itemToReplace);

            // If the token is not already wrapped by a literal expression, create one.
            LiteralExpression rightHandSide = parent;

            if (rightHandSide == null)
            {
                rightHandSide = document.CreateLiteralExpression(token);
            }

            // Create the left-hand side and the expression.
            LiteralExpression      leftHandSide = document.CreateLiteralExpression(document.CreateThisToken());
            MemberAccessExpression expression   = document.CreateMemberAccessExpression(leftHandSide, rightHandSide);

            // Insert the member access expression in the same location as the previous literal.
            document.Insert(expression, marker);
        }
Beispiel #2
0
        /// <summary>
        /// Checks a string to determine whether it is using an incorrect empty string notation.
        /// </summary>
        /// <param name="text">The string to check.</param>
        /// <param name="parentElement">The parent element.</param>
        private void CheckEmptyString(Token text, Element parentElement)
        {
            Param.AssertNotNull(text, "text");
            Param.AssertNotNull(parentElement, "parentElement");

            Debug.Assert(text.TokenType == TokenType.String, "The token must be a string.");

            if (string.Equals(text.Text, "\"\"", StringComparison.Ordinal) ||
                string.Equals(text.Text, "@\"\"", StringComparison.Ordinal))
            {
                // Look at the previous token. If it is the 'case' keyword, then do not throw this
                // exception. It is illegal to write case: String.Empty and instead case: "" must be written.
                Token previousToken = text.FindPreviousToken();
                if (previousToken == null || (previousToken.TokenType != TokenType.Case && !IsConstVariableDeclaration(previousToken)))
                {
                    this.Violation(
                        Rules.UseStringEmptyForEmptyStrings,
                        new ViolationContext(parentElement, text.LineNumber),
                        (c, o) =>
                    {
                        // Fix the violation.
                        CsDocument document = text.Document;

                        // Create a member access expression which represents "string.Empty".
                        var stringEmpty = document.CreateMemberAccessExpression(document.CreateLiteralExpression("string"), document.CreateLiteralExpression("Empty"));

                        // The parent of the token should be a literal expression.
                        if (text.Parent != null && text.Parent.Is(ExpressionType.Literal))
                        {
                            document.Replace(text.Parent, stringEmpty);
                        }
                        else
                        {
                            Debug.Fail("Unhandled situation");
                        }
                    });
                }
            }
        }