Exemplo n.º 1
0
        private void CheckDeclarationKeywordOrder(Element element)
        {
            Param.AssertNotNull(element, "element");

            int accessModifierIndex = -1;
            int staticIndex = -1;
            int otherWordIndex = -1;

            int index = 0;

            for (Token token = element.FindFirstChildToken(); token != null; token = token.FindNextSiblingToken())
            {
                TokenType type = token.TokenType;
                if (type == TokenType.Private ||
                    type == TokenType.Public ||
                    type == TokenType.Protected ||
                    type == TokenType.Internal)
                {
                    if (accessModifierIndex == -1)
                    {
                        accessModifierIndex = index++;
                    }
                }
                else if (type == TokenType.Static)
                {
                    if (staticIndex == -1)
                    {
                        staticIndex = index++;
                    }
                }
                else
                {
                    if (otherWordIndex == -1)
                    {
                        otherWordIndex = index++;
                    }
                }
            }

            if (accessModifierIndex != -1)
            {
                if (staticIndex > -1 && staticIndex < accessModifierIndex)
                {
                    this.AddViolation(
                        element,
                        Rules.DeclarationKeywordsMustFollowOrder,
                        Strings.AccessModifier,
                        string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Static));
                }

                if (otherWordIndex > -1 && otherWordIndex < accessModifierIndex)
                {
                    this.AddViolation(
                        element,
                        Rules.DeclarationKeywordsMustFollowOrder,
                        Strings.AccessModifier,
                        string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Other));
                }
            }

            if (staticIndex > -1)
            {
                if (otherWordIndex > -1 && otherWordIndex < staticIndex)
                {
                    this.AddViolation(
                        element,
                        Rules.DeclarationKeywordsMustFollowOrder,
                        string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Static),
                        string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Other));
                }
            }

            // Check to make sure that 'protected' comes just before 'internal'.
            if (element.AccessModifierType == AccessModifierType.ProtectedInternal)
            {
                bool foundProtected = false;
                for (Token token = element.FindFirstChildToken(); token != null; token = token.FindNextSiblingToken())
                {
                    if (foundProtected)
                    {
                        if (token.TokenType == TokenType.Internal)
                        {
                            break;
                        }
                        else
                        {
                            this.AddViolation(element, Rules.ProtectedMustComeBeforeInternal);
                            break;
                        }
                    }
                    else
                    {
                        if (token.TokenType == TokenType.Protected)
                        {
                            foundProtected = true;
                        }
                        else if (token.TokenType == TokenType.Internal)
                        {
                            this.AddViolation(element, Rules.ProtectedMustComeBeforeInternal);
                            break;
                        }
                    }
                }
            }
        }