Exemplo n.º 1
0
        /// <summary>
        /// Checks the prefix for a variable defined within a method or property.
        /// </summary>
        /// <param name="variable">The variable to check.</param>
        /// <param name="element">The element that contains the variable.</param>
        /// <param name="validPrefixes">A list of valid prefixes that should not be considered hungarian.</param>
        private void CheckMethodVariablePrefix(
            IVariable variable, Element element, Dictionary <string, string> validPrefixes)
        {
            Param.AssertNotNull(variable, "variable");
            Param.AssertNotNull(element, "element");
            Param.Ignore(validPrefixes);

            // Skip past any prefixes in the name.
            int index = NamingRules.MovePastPrefix(variable.VariableName);

            // Do not check for name casing on fields and events. These are checked elsewhere since they are elements, not simple variables,
            // and therefor fall under different rules.
            bool checkCasing = element.ElementType != ElementType.Field && element.ElementType != ElementType.Event;

            // Check whether the name starts with a lower-case letter.
            if (variable.VariableName.Length > index && char.IsLower(variable.VariableName, index))
            {
                // Check for hungarian notation.
                this.CheckHungarian(variable.VariableName, index, variable.Location.LineNumber, element, validPrefixes);

                // Check casing on the variable.
                if (checkCasing && (variable.VariableModifiers & VariableModifiers.Const) == VariableModifiers.Const)
                {
                    // Constants must start with an upper-case letter.
                    this.AddViolation(element, variable.Location.LineNumber, Rules.ConstFieldNamesMustBeginWithUpperCaseLetter, variable.VariableName);
                }
            }
            else if (checkCasing && (variable.VariableModifiers & VariableModifiers.Const) == 0)
            {
                // Non-constant method variables must start with a lower-case letter.
                this.AddViolation(element, variable.Location.LineNumber, Rules.FieldNamesMustBeginWithLowerCaseLetter, variable.VariableName);
            }
        }
Exemplo n.º 2
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.Name);

            // Check whether the name starts with a lower-case letter.
            if (char.IsLower(field.Name, index))
            {
                // Check for hungarian notation.
                this.CheckHungarian(field.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.Name);
                }
                else if (field.AccessModifierType == AccessModifierType.Public ||
                         field.AccessModifierType == AccessModifierType.Internal ||
                         field.AccessModifierType == AccessModifierType.ProtectedInternal)
                {
                    // Public or internal fields must start with an upper-case letter.
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.AccessibleFieldsMustBeginWithUpperCaseLetter,
                        field.Name);
                }

                // Readonly fields which are not declared private must start with an upper-case letter.
                if (field.Readonly && field.AccessModifierType != AccessModifierType.Private)
                {
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter,
                        field.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.AccessModifierType != AccessModifierType.Public &&
                    field.AccessModifierType != AccessModifierType.Internal &&
                    field.AccessModifierType != AccessModifierType.ProtectedInternal)
                {
                    this.AddViolation(
                        field,
                        field.LineNumber,
                        Rules.FieldNamesMustBeginWithLowerCaseLetter,
                        field.Name);
                }
            }
        }