Esempio n. 1
0
        /// <summary>
        /// Determines whether the given bracket is the only thing on its line or whether it shares the line.
        /// </summary>
        /// <param name="bracketNode">The bracket to check.</param>
        /// <param name="allowTrailingCharacters">Indicates whether a semicolon, comma or closing parenthesis after the
        /// bracket is allowed.</param>
        /// <returns>Returns true if the bracket shares the line with something else.</returns>
        private static bool BracketSharesLine(Token bracketNode, bool allowTrailingCharacters)
        {
            Param.AssertNotNull(bracketNode, "bracketNode");
            Param.Ignore(allowTrailingCharacters);

            // Look forward.
            bool sharesLine = false;

            // Find the next non-whitespace or comment item.
            LexicalElement nextItem = null;

            for (LexicalElement item = bracketNode.FindNextLexicalElement(); item != null; item = item.FindNextLexicalElement())
            {
                if (item.LexicalElementType == LexicalElementType.EndOfLine)
                {
                    break;
                }
                else if (item.LexicalElementType != LexicalElementType.WhiteSpace &&
                         item.LexicalElementType != LexicalElementType.Comment)
                {
                    nextItem = item;
                    break;
                }
            }

            if (nextItem != null)
            {
                if (!allowTrailingCharacters ||
                    (!nextItem.Is(TokenType.Semicolon) &&
                     !nextItem.Is(TokenType.Comma) &&
                     !nextItem.Is(TokenType.CloseParenthesis) &&
                     !nextItem.Is(TokenType.CloseSquareBracket)))
                {
                    sharesLine = true;
                }
            }

            if (!sharesLine)
            {
                // Look backwards.
                for (LexicalElement item = bracketNode.FindPreviousLexicalElement(); item != null; item = item.FindPreviousLexicalElement())
                {
                    if (item.LexicalElementType == LexicalElementType.EndOfLine)
                    {
                        break;
                    }
                    else if (item.LexicalElementType != LexicalElementType.WhiteSpace && !item.Is(CommentType.SingleLineComment))
                    {
                        sharesLine = true;
                        break;
                    }
                }
            }

            return(sharesLine);
        }
        /// <summary>
        /// Checks a single-line comment to see if it is empty.
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <param name="parentElement">The parent element.</param>
        private void CheckSingleLineComment(Comment comment, Element parentElement)
        {
            Param.AssertNotNull(comment, "comment");
            Param.AssertNotNull(parentElement, "parentElement");

            // This is a single line comment.
            int  slashCount = 0;
            bool found      = false;

            // Loop through the characters in the comment text.
            for (int character = 0; character < comment.Text.Length; ++character)
            {
                // See if we've found the slashes at the beginning of the comment
                if (slashCount == 2)
                {
                    // Check whether there's anything here other than whitespace.
                    // If so, then this comment is ok.
                    if (comment.Text[character] != ' ' &&
                        comment.Text[character] != '\t' &&
                        comment.Text[character] != '\r' &&
                        comment.Text[character] != '\n')
                    {
                        found = true;
                        break;
                    }
                }
                else if (comment.Text[character] == '/')
                {
                    ++slashCount;
                }
            }

            // Check whether the comment contained any text.
            if (!found)
            {
                // This is only allowed if this comment is sandwiched in between two other comments.
                bool isComment = false;
                int  lines     = 0;
                for (LexicalElement item = comment.FindPreviousLexicalElement(); item != null; item = item.FindPreviousLexicalElement())
                {
                    if (item.Text == "\n")
                    {
                        ++lines;
                        if (lines > 1)
                        {
                            break;
                        }
                    }
                    else if (item.Is(CommentType.SingleLineComment))
                    {
                        isComment = true;
                        break;
                    }
                    else if (item.LexicalElementType != LexicalElementType.WhiteSpace)
                    {
                        break;
                    }
                }

                if (!isComment)
                {
                    this.AddViolation(parentElement, comment.LineNumber, Rules.CommentsMustContainText);
                }
                else
                {
                    isComment = false;
                    lines     = 0;
                    for (LexicalElement item = comment.FindNextLexicalElement(); item != null; item = item.FindNextLexicalElement())
                    {
                        if (item.Text == "\n")
                        {
                            ++lines;
                            if (lines > 1)
                            {
                                break;
                            }
                        }
                        else if (item.Is(CommentType.SingleLineComment))
                        {
                            isComment = true;
                            break;
                        }
                        else if (item.LexicalElementType != LexicalElementType.WhiteSpace)
                        {
                            break;
                        }
                    }

                    if (!isComment)
                    {
                        this.AddViolation(parentElement, comment.LineNumber, Rules.CommentsMustContainText);
                    }
                }
            }
        }