/// <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(Variable variable, CsElement 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.Name); // Check whether the name starts with a lower-case letter. if (variable.Name.Length > index && char.IsLower(variable.Name, index)) { // Check for hungarian notation. this.CheckHungarian(variable.Name, index, variable.Location.LineNumber, element, validPrefixes); // Check casing on the variable. if ((variable.Modifiers & VariableModifiers.Const) == VariableModifiers.Const) { // Constants must start with an upper-case letter. this.AddViolation(element, variable.Location.LineNumber, Rules.ConstFieldNamesMustBeginWithUpperCaseLetter, variable.Name); } } else if ((variable.Modifiers & VariableModifiers.Const) == 0 && char.IsUpper(variable.Name, index)) { // We check for IsUpper again to support languages that dont have upper or lower case like Chinese. // Method variables must start with a lower-case letter. this.AddViolation(element, variable.Location.LineNumber, Rules.FieldNamesMustBeginWithLowerCaseLetter, variable.Name); } }
/// <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 non-private must start with an upper-case letter. if (field.Readonly && field.AccessModifier != AccessModifierType.Private) { this.AddViolation(field, field.LineNumber, Rules.NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter, field.Declaration.Name); } // Readonly static fields must start with an upper-case letter. if (field.Readonly && field.Static) { this.AddViolation(field, field.LineNumber, Rules.StaticReadonlyFieldsMustBeginWithUpperCaseLetter, field.Declaration.Name); } } else if (char.IsUpper(field.Declaration.Name, index)) { // We check for IsUpper here as some languages don't have Upper/Lower case liek Chinese. // Constants must always start with an upper-case letter. if (field.Const) { return; } // Readonly non-private fields must start with an upper-case letter. if (field.Readonly && field.AccessModifier != AccessModifierType.Private) { return; } // Readonly static fields must start with an upper-case letter. if (field.Readonly && field.Static) { return; } // Public, internal or protected-internal fields also must always start with an upper-case letter. if (field.AccessModifier == AccessModifierType.Public) { return; } if (field.AccessModifier == AccessModifierType.Internal) { return; } if (field.AccessModifier == AccessModifierType.ProtectedInternal) { return; } this.AddViolation(field, field.LineNumber, Rules.FieldNamesMustBeginWithLowerCaseLetter, field.Declaration.Name); } }