Пример #1
0
        private void PrivateFieldNameMustStartWithUnderscoreFollowedByLowerCase(Field field)
        {
            if (field.Generated)
            {
                return;
            }

            if (field.Const)
            {
                return;
            }

            if (IsReadonlyStatic(field))
            {
                return;
            }

            if (ViolatesFieldNameRule(field.Declaration.Name))
            {
                AddViolation(
                    field,
                    "PrivateFieldNameMustStartWithUnderscoreFollowedByLowerCase",
                    new object[0]);
            }
        }
Пример #2
0
 private void CheckFieldPrefix(Field field, Dictionary<string, string> validPrefixes)
 {
     int index = MovePastPrefix(field.Declaration.Name);
     if (char.IsLower(field.Declaration.Name, index))
     {
         this.CheckHungarian(field.Declaration.Name, index, field.LineNumber, field, validPrefixes);
         if (field.Const)
         {
             base.AddViolation(field, field.LineNumber, Microsoft.StyleCop.CSharp.Rules.ConstFieldNamesMustBeginWithUpperCaseLetter, new object[] { field.Declaration.Name });
         }
         else if (((field.AccessModifier == AccessModifierType.Public) || (field.AccessModifier == AccessModifierType.Internal)) || (field.AccessModifier == AccessModifierType.ProtectedInternal))
         {
             base.AddViolation(field, field.LineNumber, Microsoft.StyleCop.CSharp.Rules.AccessibleFieldsMustBeginWithUpperCaseLetter, new object[] { field.Declaration.Name });
         }
         if (field.Readonly && (field.Declaration.AccessModifierType != AccessModifierType.Private))
         {
             base.AddViolation(field, field.LineNumber, Microsoft.StyleCop.CSharp.Rules.NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter, new object[] { field.Declaration.Name });
         }
     }
     else if (((!field.Const && !field.Readonly) && ((field.AccessModifier != AccessModifierType.Public) && (field.AccessModifier != AccessModifierType.Internal))) && (field.AccessModifier != AccessModifierType.ProtectedInternal))
     {
         base.AddViolation(field, field.LineNumber, Microsoft.StyleCop.CSharp.Rules.FieldNamesMustBeginWithLowerCaseLetter, new object[] { field.Declaration.Name });
     }
 }
        /// <summary>
        /// Parses and returns a field.
        /// </summary>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="elementReference">A reference to the element being created.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <param name="generated">Indicates whether the code is marked as generated code.</param>
        /// <param name="xmlHeader">The element's documentation header.</param>
        /// <param name="attributes">The attributes on the element.</param>
        /// <returns>Returns the element.</returns>
        private Field ParseField(
            CsElement parent, 
            Reference<ICodePart> elementReference,
            bool unsafeCode, 
            bool generated, 
            XmlHeader xmlHeader, 
            ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(parent, "parent");
            Param.AssertNotNull(elementReference, "elementReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(xmlHeader);
            Param.Ignore(attributes);

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;
            Dictionary<CsTokenType, CsToken> modifiers = this.GetElementModifiers(elementReference, ref accessModifier, FieldModifiers);

            unsafeCode |= modifiers.ContainsKey(CsTokenType.Unsafe);

            // Get the field type.
            TypeToken fieldType = this.GetTypeToken(elementReference, unsafeCode, true);
            Node<CsToken> fieldTypeNode = this.tokens.InsertLast(fieldType);

            // Get all of the variable declarators.
            IList<VariableDeclaratorExpression> declarators = this.ParseFieldDeclarators(elementReference, unsafeCode, fieldType);

            if (declarators.Count == 0)
            {
                throw this.CreateSyntaxException();
            }

            VariableDeclarationExpression declarationExpression = new VariableDeclarationExpression(
                new CsTokenList(this.tokens, declarators[0].Tokens.First, this.tokens.Last),
                new LiteralExpression(this.tokens, fieldTypeNode),
                declarators);

            // Create the field.
            Node<CsToken> firstTokenNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next;
            CsTokenList declarationTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);
            Declaration declaration = new Declaration(
                declarationTokens, declarators[0].Identifier.Text, ElementType.Field, accessModifier, modifiers);

            Field field = new Field(
                this.document, parent, xmlHeader, attributes, declaration, fieldType, unsafeCode, generated);
            elementReference.Target = field;

            // Get the trailing semicolon.
            this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, elementReference));

            // Create the variable declaration statement and add it to the field.
            field.VariableDeclarationStatement = new VariableDeclarationStatement(
                new CsTokenList(this.tokens, declarators[0].Tokens.First, this.tokens.Last),
                field.Const,
                declarationExpression);

            return field;
        }
Пример #4
0
 private Field ParseField(CsElement parent, bool unsafeCode, bool generated, XmlHeader xmlHeader, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes)
 {
     Microsoft.StyleCop.Node<CsToken> last = this.tokens.Last;
     AccessModifierType @private = AccessModifierType.Private;
     Dictionary<CsTokenType, CsToken> elementModifiers = this.GetElementModifiers(ref @private, FieldModifiers);
     unsafeCode |= elementModifiers.ContainsKey(CsTokenType.Unsafe);
     TypeToken typeToken = this.GetTypeToken(unsafeCode, true);
     Microsoft.StyleCop.Node<CsToken> tokenNode = this.tokens.InsertLast(typeToken);
     IList<VariableDeclaratorExpression> declarators = this.ParseFieldDeclarators(unsafeCode, typeToken);
     if (declarators.Count == 0)
     {
         throw this.CreateSyntaxException();
     }
     VariableDeclarationExpression expression = new VariableDeclarationExpression(new CsTokenList(this.tokens, declarators[0].Tokens.First, this.tokens.Last), new LiteralExpression(this.tokens, tokenNode), declarators);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = (last == null) ? this.tokens.First : last.Next;
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     Declaration declaration = new Declaration(tokens, declarators[0].Identifier.Text, ElementType.Field, @private, elementModifiers);
     Field field = new Field(this.document, parent, xmlHeader, attributes, declaration, typeToken, unsafeCode, generated);
     this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon));
     field.VariableDeclarationStatement = new VariableDeclarationStatement(new CsTokenList(this.tokens, declarators[0].Tokens.First, this.tokens.Last), field.Const, expression);
     return field;
 }
Пример #5
0
        /// <summary>
        /// Checks a field for compliance with naming prefix rules.
        /// </summary>
        /// <param name="field">The field element.</param>
        /// <param name="validPrefixes">A list of valid prefixes that should not be considered hungarian.</param>
        private void CheckFieldPrefix(Field field, Dictionary<string, string> validPrefixes)
        {
            Param.AssertNotNull(field, "field");
            Param.Ignore(validPrefixes);

            // Skip past any prefixes in the name.
            int index = NamingRules.MovePastPrefix(field.Declaration.Name);

            // Check whether the name starts with a lower-case letter.
            if (char.IsLower(field.Declaration.Name, index))
            {
                // Check for hungarian notation.
                this.CheckHungarian(field.Declaration.Name, index, field.LineNumber, field, validPrefixes);

                // Check casing on the field.
                if (field.Const)
                {
                    // Const fields must start with an upper-case letter.
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.ConstFieldNamesMustBeginWithUpperCaseLetter,
                        field.Declaration.Name);
                }
                else if (field.AccessModifier == AccessModifierType.Public ||
                    field.AccessModifier == AccessModifierType.Internal ||
                    field.AccessModifier == AccessModifierType.ProtectedInternal)
                {
                    // Public or internal fields must start with an upper-case letter.
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.AccessibleFieldsMustBeginWithUpperCaseLetter,
                        field.Declaration.Name);
                }

                // Readonly fields which are not declared private must start with an upper-case letter.
                if (field.Readonly && field.Declaration.AccessModifierType != AccessModifierType.Private)
                {
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter,
                        field.Declaration.Name);
                }
            }
            else
            {
                // Constants must always start with an upper-case letter,
                // while readonly fields may start with either an upper-case or
                // a lower-case letter. Public or internal fields
                // also must always start with an upper-case letter.
                if (!field.Const &&
                    !field.Readonly &&
                    field.AccessModifier != AccessModifierType.Public &&
                    field.AccessModifier != AccessModifierType.Internal &&
                    field.AccessModifier != AccessModifierType.ProtectedInternal)
                {
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.FieldNamesMustBeginWithLowerCaseLetter,
                        field.Declaration.Name);
                }
            }
        }
Пример #6
0
 private void VisitField(Field field)
 {
     PrivateFieldNameMustStartWithUnderscoreFollowedByLowerCase(field);
 }
Пример #7
0
 private static bool IsReadonlyStatic(Field field)
 {
     return field.Readonly && field.Declaration.ContainsModifier(new[] { CsTokenType.Static });
 }