Exemplo n.º 1
0
        /// <summary>
        /// Checks the placement of curly brackets on the current element.
        /// </summary>
        /// <param name="element">The element to check.</param>
        /// <param name="allowAllOnOneLine">Indicates whether the brackets are allowed to be all on one line.</param>
        private void CheckElementBracketPlacement(Element element, bool allowAllOnOneLine)
        {
            Param.AssertNotNull(element, "element");
            Param.Ignore(allowAllOnOneLine);

            // Get the absolute index of the last token from the element declaration.
            Token lastDeclarationToken = element.LastDeclarationToken;

            if (lastDeclarationToken != null)
            {
                // Find the opening curly bracket within this element.
                for (Token token = lastDeclarationToken.FindNextDescendentTokenOf(element); token != null; token = token.FindNextDescendentTokenOf(element))
                {
                    if (token.Is(OperatorType.Equals))
                    {
                        // If we have found an equals sign in the element declaration before finding any opening curly bracket
                        // then this is not a bracketed element. This could be something like the following examples:
                        // int[] x = new int[] { 1, 2, 3 };
                        // event EventHandler x = (sender, e) => { };
                        // In both of these cases, it's allowed to put the statement on a single line.
                        allowAllOnOneLine = true;
                    }
                    else if (token.TokenType == TokenType.OpenCurlyBracket)
                    {
                        OpenCurlyBracketToken openCurlyBracket = (OpenCurlyBracketToken)token;
                        this.CheckBracketPlacement(element, element, null, openCurlyBracket, allowAllOnOneLine);

                        if (allowAllOnOneLine && element.ElementType == ElementType.Accessor)
                        {
                            // If this accessor is all one one line, any other accessors in this
                            // property must also be all on one line.
                            this.CheckSiblingAccessors(element, openCurlyBracket);
                        }

                        break;
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks an accessor and its siblings. If the access is all on one line, its siblings
        /// must also be all on one line.
        /// </summary>
        /// <param name="accessor">The accessor to check.</param>
        /// <param name="openingBracket">The opening bracket within the accessor.</param>
        private void CheckSiblingAccessors(Element accessor, OpenCurlyBracketToken openingBracket)
        {
            Param.AssertNotNull(accessor, "accessor");
            Param.AssertNotNull(openingBracket, "openingBracket");

            if (openingBracket.MatchingBracket != null &&
                openingBracket.LineNumber == openingBracket.MatchingBracket.LineNumber)
            {
                // Check the siblings of this accessor.
                Element property = accessor.Parent as Element;
                if (property != null)
                {
                    for (Element sibling = property.FindFirstChildElement(); sibling != null; sibling = sibling.FindNextSiblingElement())
                    {
                        if (sibling != accessor)
                        {
                            // Check this sibling to make sure it is all on one line.
                            openingBracket = sibling.FindFirstChild <OpenCurlyBracketToken>();
                            if (openingBracket != null && openingBracket.MatchingBracket != null)
                            {
                                if (openingBracket.LineNumber != openingBracket.MatchingBracket.LineNumber)
                                {
                                    this.AddViolation(
                                        accessor,
                                        accessor.LineNumber,
                                        Rules.AllAccessorsMustBeMultiLineOrSingleLine,
                                        property.FriendlyTypeText);

                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks the curly brackets under the given expression.
        /// </summary>
        /// <param name="expression">The expression being visited.</param>
        /// <param name="parentExpression">The parent expression, if any.</param>
        /// <param name="parentStatement">The parent statement, if any.</param>
        /// <param name="parentElement">The parent element, if any.</param>
        /// <returns>Returns true to continue, or false to stop the walker.</returns>
        private bool VisitExpression(
            Expression expression,
            Expression parentExpression,
            Statement parentStatement,
            Element parentElement)
        {
            Param.AssertNotNull(expression, "expression");
            Param.Ignore(parentExpression);
            Param.Ignore(parentStatement);
            Param.AssertNotNull(parentElement, "parentElement");

            switch (expression.ExpressionType)
            {
            case ExpressionType.AnonymousMethod:
            case ExpressionType.Lambda:
                // An anonymous method or lambda expression is allowed to be all on a single line as long as the entire
                // expression is on one line.
                OpenCurlyBracketToken openBracket = expression.FindFirstChild <OpenCurlyBracketToken>();

                if (openBracket != null)
                {
                    bool allowAllOnOneLine = expression.Location.StartPoint.LineNumber == expression.Location.EndPoint.LineNumber;

                    this.CheckBracketPlacement(
                        expression,
                        parentElement,
                        parentStatement,
                        openBracket,
                        allowAllOnOneLine);
                }

                break;

            case ExpressionType.ArrayInitializer:
            case ExpressionType.ObjectInitializer:
            case ExpressionType.CollectionInitializer:
                // If this object or collection initializer is a direct child of another
                // expression, then use the token list of the parent expression.
                // This ensures that the initializer cannot only exist all on
                // one single line if it is also on the same line as the rest of the
                // parent expression. If this is not a direct child of aanother expression,
                // this restriction does not apply.
                CodeUnit   openBracketParent = expression;
                Expression expressionParent  = expression.Parent as Expression;
                if (expressionParent != null)
                {
                    openBracketParent = expressionParent;
                }

                var openCurlyBracket = expression.FindFirstChild <OpenCurlyBracketToken>();
                if (openCurlyBracket != null)
                {
                    this.CheckBracketPlacement(
                        openBracketParent,
                        parentElement,
                        parentStatement,
                        openCurlyBracket,
                        true);
                }

                break;

            default:
                break;
            }

            return(true);
        }