Author: spouliot
Date: 2007-12-26 13:09:49 -0500 (Wed, 26 Dec 2007)
New Revision: 91902

Added:
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/NamingUtils.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectCasingRule.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectPrefixRule.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectSuffixRule.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UsePreferredTermsRule.cs
Removed:
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/AttributeEndsWithAttributeSuffixRule.cs
Modified:
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/ChangeLog
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.mdp
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.xml.in
   trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Makefile.am
Log:
2007-12-26  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * AttributeEndsWithAttributeSuffixRule.cs: Deleted. The new rule
        UseCorrectSuffixTest is a superset of this rule.
        * Gendarme.Rules.Naming.mdp: Updated MonoDevelop project file.
        * Gendarme.Rules.Naming.xml.in: Updated descriptions.
        * Makefile.am: Updated Makefile for new rules/tests.
        * NamingUtils.cs: New. Common methods for naming rules.
        * UseCorrectCasingRule.cs: New. Rule to check correct casing. 
        From Daniel Abramov (GHOP).
        * UseCorrectPrefixRule.cs:New. Rule to check correct prefixes. 
        From Daniel Abramov (GHOP).
        * UseCorrectSuffixRule.cs:New. Rule to check correct suffixes. 
        From Daniel Abramov (GHOP).
        * UsePreferredTermsRule.cs:New. Rule to check use of preferred terms. 
        From Daniel Abramov (GHOP).



Deleted: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/AttributeEndsWithAttributeSuffixRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/AttributeEndsWithAttributeSuffixRule.cs
    2007-12-26 18:05:20 UTC (rev 91901)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/AttributeEndsWithAttributeSuffixRule.cs
    2007-12-26 18:09:49 UTC (rev 91902)
@@ -1,73 +0,0 @@
-//
-// Gendarme.Rules.Naming.AttributeEndsWithAttributeSuffixRule class
-//
-// Authors:
-//     Néstor Salceda <[EMAIL PROTECTED]>
-//
-//  (C) 2007 Néstor Salceda
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System;
-
-using Mono.Cecil;
-using Gendarme.Framework;
-
-namespace Gendarme.Rules.Naming {
-       public class AttributeEndsWithAttributeSuffixRule : ITypeRule {
-
-               //If the type is defined outside of the assembly I can't get a
-               //TypeDefinition, then I can't get the BaseType (only in 
TypeDefinition).
-               //Then, in order to avoid false positives, if we can't 
determine basetype,
-               //the method returns false.
-               private bool InheritsFromAttribute (TypeReference typeReference)
-               {
-                       TypeDefinition current = typeReference as 
TypeDefinition;
-                       if (current == null)
-                               return false;
-
-                       while (current.BaseType != null && 
current.BaseType.FullName != "System.Object") {
-                               if (current.BaseType.FullName == 
"System.Attribute")
-                                       return true;
-                               else {
-                                       current = current.BaseType as 
TypeDefinition;
-                                       if (current == null)
-                                               return false;
-                               }
-                       }
-                       return false;
-               }
-
-               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
-               {
-                       // rule applies only for type that inherits from 
System.Attribute
-                       if (!InheritsFromAttribute (typeDefinition))
-                               return runner.RuleSuccess;
-
-                       if (typeDefinition.Name.EndsWith ("Attribute"))
-                               return runner.RuleSuccess;
-
-                       Location location = new Location 
(typeDefinition.FullName, typeDefinition.Name, 0);
-                       Message message = new Message ("The class name doesn't 
end with Attribute Suffix", location, MessageType.Error);
-                       return new MessageCollection (message);
-               }
-       }
-}

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/ChangeLog  2007-12-26 
18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/ChangeLog  2007-12-26 
18:09:49 UTC (rev 91902)
@@ -1,3 +1,20 @@
+2007-12-26  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * AttributeEndsWithAttributeSuffixRule.cs: Deleted. The new rule
+       UseCorrectSuffixTest is a superset of this rule.
+       * Gendarme.Rules.Naming.mdp: Updated MonoDevelop project file.
+       * Gendarme.Rules.Naming.xml.in: Updated descriptions.
+       * Makefile.am: Updated Makefile for new rules/tests.
+       * NamingUtils.cs: New. Common methods for naming rules.
+       * UseCorrectCasingRule.cs: New. Rule to check correct casing. 
+       From Daniel Abramov (GHOP).
+       * UseCorrectPrefixRule.cs:New. Rule to check correct prefixes. 
+       From Daniel Abramov (GHOP).
+       * UseCorrectSuffixRule.cs:New. Rule to check correct suffixes. 
+       From Daniel Abramov (GHOP).
+       * UsePreferredTermsRule.cs:New. Rule to check use of preferred terms. 
+       From Daniel Abramov (GHOP).
+
 2007-12-20  Sebastien Pouliot  <[EMAIL PROTECTED]> 
 
        * EnumNotEndsWithEnumOrFlagsSuffixRule.cs,

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.mdp
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.mdp  
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.mdp  
2007-12-26 18:09:49 UTC (rev 91902)
@@ -7,19 +7,23 @@
       <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
     </Configuration>
     <Configuration name="Release" ctype="DotNetProjectConfiguration">
-      <Output directory="./bin/Release" assembly="Gendarme.Rules.Naming" />
+      <Output directory="bin/Release" assembly="Gendarme.Rules.Naming" />
       <Build debugmode="False" target="Library" />
       <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_2_0" />
       <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
     </Configuration>
   </Configurations>
   <Contents>
-    <File name="./UsePluralNameInEnumFlagsRule.cs" subtype="Code" 
buildaction="Compile" />
-    <File name="./UseSingularNameInEnumsUnlessAreFlagsRule.cs" subtype="Code" 
buildaction="Compile" />
-    <File name="./AttributeEndsWithAttributeSuffixRule.cs" subtype="Code" 
buildaction="Compile" />
-    <File name="./EnumNotEndsWithEnumOrFlagsSuffixRule.cs" subtype="Code" 
buildaction="Compile" />
-    <File name="./Gendarme.Rules.Naming.xml.in" subtype="Code" 
buildaction="Nothing" />
-    <File name="./DetectNonAlphaNumericsInTypeNamesRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="UsePluralNameInEnumFlagsRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="UseSingularNameInEnumsUnlessAreFlagsRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="EnumNotEndsWithEnumOrFlagsSuffixRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="Gendarme.Rules.Naming.xml.in" subtype="Code" 
buildaction="Nothing" />
+    <File name="DetectNonAlphaNumericsInTypeNamesRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="UseCorrectCasingRule.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="UseCorrectPrefixRule.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="UseCorrectSuffixRule.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="UsePreferredTermsRule.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="NamingUtils.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Project" localcopy="True" refto="framework" />

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.xml.in
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.xml.in   
    2007-12-26 18:05:20 UTC (rev 91901)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Gendarme.Rules.Naming.xml.in   
    2007-12-26 18:09:49 UTC (rev 91902)
@@ -1,32 +1,50 @@
 <rules>
-       <rule   Name="AttributeEndsWithAttributeSuffixRule"
-               
Type="Gendarme.Rules.Naming.AttributeEndsWithAttributeSuffixRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
-               
Uri="http://groups.google.com/group/Gendarme/web/rules-documentation#AttributeEndsWithAttributeSuffixRule";>
-               <problem>The type '{0}' is an attribute and by convention the 
attributes should end with the Attribute suffix.</problem>
-               <solution>You should rename the type and append the Attribute 
suffix.</solution>
+       <rule   Name="DetectNonAlphaNumericsInTypeNamesRule" 
+               
Type="Gendarme.Rules.Naming.DetectNonAlphaNumericsInTypeNamesRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               
Uri="http://groups.google.com/group/gendarme/web/rules-documentation#DetectNonAlphaNumericsInTypeNamesRule";>
+               <problem>The type or method name '{0}' contains underscore in 
its name.</problem>
+               <solution>Remove the underscore from name.</solution>
        </rule>
        <rule   Name="EnumNotEndsWithEnumOrFlagsSuffixRule"
                
Type="Gendarme.Rules.Naming.EnumNotEndsWithEnumOrFlagsSuffixRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
                
Uri="http://groups.google.com/group/Gendarme/web/rules-documentation#EnumNotEndsWithEnumOrFlagsSuffixRule";>
-               <problem>The type '{0}' is an enumeration and by convention the 
enums shoudn't end with the suffix Enum or Flags.</problem>
-               <solution>You should remove the Enum or Flags suffix.</solution>
+               <problem>The type '{0}' is an enumeration and by convention the 
enums shoud not end with the Enum or Flags suffix.</problem>
+               <solution>Remove the Enum or Flags suffix in enum 
name.</solution>
        </rule>
+       <rule   Name="UseCorrectCasingRule"
+               Type="Gendarme.Rules.Naming.UseCorrectCasingRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               Uri="">
+               <problem>The '{0}' identifier violates .NET naming 
conventions.</problem>
+               <solution>Make all type and method names pascal-cased (like 
MyClass), and all parameter names must be camel-cased (like 
myParameter).</solution>
+       </rule>
+       <rule   Name="UseCorrectPrefixRule"
+               Type="Gendarme.Rules.Naming.UseCorrectPrefixRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               Uri="">
+               <problem>The type '{0}' starts with an incorrect prefix or does 
not start with the required one. All interface names should start with the 'I' 
letter, followed by another capital letter. All type names, in contrast, should 
not have any specific prefix.</problem>
+               <solution>Rename the type and prefix it correctly.</solution>
+       </rule>
+       <rule   Name="UseCorrectSuffixRule"
+               Type="Gendarme.Rules.Naming.UseCorrectSuffixRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               Uri="">
+               <problem>The type '{0}' does not end with a correct suffix. 
That usually happens when you define a custom attribute or exception and forget 
appending suffixes like 'Attribute' or 'Exception' to the type name.</problem>
+               <solution>Rename the type and append the correct 
suffix.</solution>
+       </rule>
        <rule   Name="UsePluralNameInEnumFlagsRule"
                Type="Gendarme.Rules.Naming.UsePluralNameInEnumFlagsRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
                
Uri="http://groups.google.com/group/Gendarme/web/rules-documentation#UsePluralNameInEnumFlagsRule";>
                <problem>The type '{0}' is a flags enumeration and by 
convention the flag enums should have a plural name.</problem>
-               <solution>You should convert from singular to plural.</solution>
+               <solution>Convert enum name from singular to plural.</solution>
        </rule>
+       <rule   Name="UsePreferredTermsRule"
+               Type="Gendarme.Rules.Naming.UsePreferredTermsRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               Uri="">
+               <problem>The '{0}' identifier contains an obsolete 
term.</problem>
+               <solution>Replace an obsolete term with the preferred 
one.</solution>
+       </rule>
        <rule   Name="UseSingularNameInEnumsUnlessAreFlagsRule"
                
Type="Gendarme.Rules.Naming.UseSingularNameInEnumsUnlessAreFlagsRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
                
Uri="http://groups.google.com/group/Gendarme/web/rules-documentation#UseSingularNameInEnumsUnlessAreFlagsRule";>
                <problem>The type '{0}' is an enumeration and by convention the 
enums should have a singular name.</problem>
-               <solution>You should convert to plural to singular.</solution>
+               <solution>Convert enum name from plural to singular.</solution>
        </rule>
-       <rule   Name="DetectNonAlphaNumericsInTypeNamesRule" 
-               
Type="Gendarme.Rules.Naming.DetectNonAlphaNumericsInTypeNamesRule, 
Gendarme.Rules.Naming, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
-               
Uri="http://groups.google.com/group/gendarme/web/rules-documentation#DetectNonAlphaNumericsInTypeNamesRule";>
-               <problem>The type or method name '{0}' contains underscore in 
its name.</problem>
-               <solution>Remove the underscore from name.</solution>
-       </rule>
 </rules>

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Makefile.am        
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/Makefile.am        
2007-12-26 18:09:49 UTC (rev 91902)
@@ -9,9 +9,10 @@
 
 naming_rules_sources_in = ../../AssemblyInfo.cs.in
 naming_rules_generated_sources = $(naming_rules_sources_in:.in=)
-naming_rules_sources = AttributeEndsWithAttributeSuffixRule.cs  
EnumNotEndsWithEnumOrFlagsSuffixRule.cs \
+naming_rules_sources = EnumNotEndsWithEnumOrFlagsSuffixRule.cs \
        UsePluralNameInEnumFlagsRule.cs 
UseSingularNameInEnumsUnlessAreFlagsRule.cs \
-       DetectNonAlphaNumericsInTypeNamesRule.cs
+       DetectNonAlphaNumericsInTypeNamesRule.cs UseCorrectCasingRule.cs 
UseCorrectPrefixRule.cs UseCorrectSuffixRule.cs \
+       UsePreferredTermsRule.cs NamingUtils.cs
 
 naming_rules_build_sources = $(addprefix $(srcdir)/, $(naming_rules_sources))
 naming_rules_build_sources += $(naming_rules_generated_sources)
@@ -34,9 +35,10 @@
                rm -f $(DESTDIR)$(pkglibdir)/`basename $$ASM`; \
        done;
 
-naming_test_sources = AttributeEndsWithAttributeSuffixTest.cs 
EnumNotEndsWithEnumOrFlagsSuffixTest.cs \
+naming_test_sources = EnumNotEndsWithEnumOrFlagsSuffixTest.cs \
        UsePluralNameInEnumFlagsTest.cs 
UseSingularNameInEnumsUnlessAreFlagsTest.cs \
-       DetectNonAlphaNumericsInTypeNamesTest.cs
+       DetectNonAlphaNumericsInTypeNamesTest.cs UseCorrectCasingTest.cs 
UseCorrectPrefixTest.cs UseCorrectSuffixTest.cs \
+       UsePreferredTermsTest.cs
 
 naming_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(naming_test_sources))
 
@@ -47,7 +49,7 @@
 test: Test.Rules.Naming.dll
 
 run-test: test
-       MONO_PATH=../../bin/:$(CECIL_PATH):$(MONO_PATH) nunit-console 
Test.Rules.Naming.dll
+       MONO_PATH=../../bin/:$(CECIL_PATH):$(MONO_PATH) nunit-console2 
Test.Rules.Naming.dll
 
 self-test: $(naming_rules_SCRIPTS)
        mono --debug ../../bin/gendarme.exe $(naming_rules_SCRIPTS)

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/NamingUtils.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/NamingUtils.cs     
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/NamingUtils.cs     
2007-12-26 18:09:49 UTC (rev 91902)
@@ -0,0 +1,81 @@
+//
+// Gendarme.Rules.Naming.NamingUtils
+//
+// Authors:
+//     Daniel Abramov <[EMAIL PROTECTED]>
+//     Sebastien Pouliot  <[EMAIL PROTECTED]>
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
+// Copyright (c) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+
+using Mono.Cecil;
+
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Naming {
+
+       internal static class NamingUtils {
+
+               private static bool HasAttribute (CustomAttributeCollection 
cac, string fullname)
+               {
+                       foreach (CustomAttribute custom in cac) {
+                               if (custom.Constructor.DeclaringType.FullName 
== fullname)
+                                       return true;
+                       }
+                       return false;
+               }
+
+               private static bool IsGeneratedByCompiler (TypeReference type)
+               {
+                       return HasAttribute (type.CustomAttributes, 
"System.Runtime.CompilerServices.CompilerGeneratedAttribute");
+               }
+
+               public static bool IsNameGeneratedByCompiler (TypeDefinition 
type)
+               {
+                       // <Module> isn't tagged as generated by the compiler 
but still must be ignored
+                       if (type.Name == Constants.ModuleType)
+                               return true;
+
+                       if (type.IsNested)
+                               return IsGeneratedByCompiler (type) || 
IsGeneratedByCompiler (type.DeclaringType);
+
+                       return IsGeneratedByCompiler (type);
+               }
+
+               public static bool IsNameGeneratedByCompiler (MethodDefinition 
method)
+               {
+                       if (method.IsConstructor)
+                               return true;
+                       if (method.IsRuntimeSpecialName)
+                               return true;
+                       if (method.IsSpecialName)
+                               return true;
+                       if (method.IsCompilerControlled)
+                               return true;
+
+                       return HasAttribute (method.CustomAttributes, 
"System.Runtime.CompilerServices.CompilerGeneratedAttribute");
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectCasingRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectCasingRule.cs    
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectCasingRule.cs    
2007-12-26 18:09:49 UTC (rev 91902)
@@ -0,0 +1,147 @@
+//
+// UseCorrectCasingRule class
+//
+// Authors:
+//     Daniel Abramov <[EMAIL PROTECTED]>
+//
+//     (C) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Mono.Cecil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Naming {
+
+       public class UseCorrectCasingRule : ITypeRule, IMethodRule {
+
+               // check if name is PascalCased
+               private static bool IsPascalCase (string name)
+               {
+                       if (name == null || name.Length == 0) {
+                               return true;
+                       }
+                       return Char.IsUpper (name [0]);
+               }
+
+               // convert name to PascalCase
+               private static string PascalCase (string name)
+               {
+                       if (name == null || name.Length == 0) {
+                               return name;
+                       }
+                       if (name.StartsWith ("_"))
+                               RemoveLeadingUnderlines (ref name);
+                       return Char.ToUpper (name [0]) + name.Substring (1);
+               }
+
+               // check if name is camelCased
+               private static bool IsCamelCase (string name)
+               {
+                       if (name == null || name.Length == 0) {
+                               return true;
+                       }
+                       return Char.IsLower (name [0]);
+               }
+
+               // convert name to camelCase
+               private static string CamelCase (string name)
+               {
+                       if (name == null || name.Length == 0) {
+                               return name;
+                       }
+                       if (name.StartsWith ("_"))
+                               RemoveLeadingUnderlines (ref name);
+                       return Char.ToLower (name [0]) + name.Substring (1);
+               }
+
+               private static void RemoveLeadingUnderlines (ref string s)
+               {
+                       int idx = 0;
+                       while ((s [idx] == '_') && (s.Length > 1))
+                               idx++;
+                       s = s.Substring (idx);
+               }
+
+
+               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
+               {
+                       // types should all be PascalCased
+                       if (IsPascalCase (typeDefinition.Name))
+                               return runner.RuleSuccess;
+
+                       if (NamingUtils.IsNameGeneratedByCompiler 
(typeDefinition))
+                               return runner.RuleSuccess;
+
+                       Location location = new Location 
(typeDefinition.FullName, typeDefinition.Name, 0);
+                       string errorMessage = string.Format ("By existing 
naming conventions, the type names should all be pascal-cased (e.g. MyClass). 
Rename '{0}' type to '{1}'.",
+                                                            
typeDefinition.Name, PascalCase (typeDefinition.Name));
+                       return new MessageCollection (new Message 
(errorMessage, location, MessageType.Error));
+               }
+
+               public MessageCollection CheckMethod (MethodDefinition 
methodDefinition, Runner runner)
+               {
+                       string name = methodDefinition.Name;
+                       MethodSemanticsAttributes attrs = 
methodDefinition.SemanticsAttributes;
+                       MethodSemanticsAttributes mask = 
MethodSemanticsAttributes.Getter | MethodSemanticsAttributes.Setter
+                               | MethodSemanticsAttributes.AddOn | 
MethodSemanticsAttributes.RemoveOn;
+                       if ((attrs & mask) != 0) {
+                               // it's something special
+                               int underscore = methodDefinition.Name.IndexOf 
('_');
+                               if (underscore != -1)
+                                       name = name.Substring (underscore + 1);
+                       } else if (NamingUtils.IsNameGeneratedByCompiler 
(methodDefinition)) {
+                               return runner.RuleSuccess;
+                       }
+
+                       MessageCollection messages = new MessageCollection ();
+                       Location location = new Location 
(methodDefinition.DeclaringType.FullName, methodDefinition.Name, 0);
+
+                       // like types, methods/props should all be PascalCased, 
too
+                       if (!IsPascalCase (name)) {
+                               string errorMessage = string.Format ("By 
existing naming conventions, all the method and property names should all be 
pascal-cased (e.g. MyOperation). Rename '{0}' to '{1}'.",
+                                                                    name, 
PascalCase (name));
+                               messages.Add (new Message (errorMessage, 
location, MessageType.Error));
+                       }
+                       // check parameters
+                       List<string> parameterNames = new List<string> ();
+                       foreach (ParameterDefinition paramDefinition in 
methodDefinition.Parameters) {
+                               if (!parameterNames.Contains 
(paramDefinition.Name)) // somewhy they duplicate sometimes
+                                       parameterNames.Add 
(paramDefinition.Name);
+                       }
+                       foreach (string param in parameterNames) {
+                               // params should all be camelCased
+                               if (!IsCamelCase (param)) {
+                                       string errorMessage = string.Format 
("By existing naming conventions, the parameter names should all be camel-cased 
(e.g. myParameter). Rename '{0}' parameter to '{1}'.",
+                                                                               
param, CamelCase (param));
+                                       messages.Add (new Message 
(errorMessage, location, MessageType.Error));
+                               }
+                       }
+                       if (messages.Count == 0)
+                               return runner.RuleSuccess;
+
+                       return messages;
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectPrefixRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectPrefixRule.cs    
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectPrefixRule.cs    
2007-12-26 18:09:49 UTC (rev 91902)
@@ -0,0 +1,70 @@
+//
+// Gendarme.Rules.Naming.UseCorrectPrefixRule class
+//
+// Authors:
+//      Daniel Abramov <[EMAIL PROTECTED]>
+//
+//  (C) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+using Mono.Cecil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Naming {
+
+       public class UseCorrectPrefixRule : ITypeRule {
+
+               private static bool IsCorrectTypeName (string name)
+               {
+                       if (name.Length < 3)
+                               return true;
+                       bool bad = name [0] == 'C' && char.IsUpper (name [1]) 
&& char.IsLower (name [2]);
+                       return !bad;
+               }
+
+               private static bool IsCorrectInterfaceName (string name)
+               {
+                       if (name.Length < 3)
+                               return false;
+                       return name [0] == 'I' && char.IsUpper (name [1]);
+               }
+
+               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
+               {
+                       Location location = new Location 
(typeDefinition.FullName, typeDefinition.Name, 0);
+                       if (typeDefinition.IsInterface) {
+                               if (!IsCorrectInterfaceName 
(typeDefinition.Name)) { // interfaces should look like 'ISomething'
+                                       Message message = new Message 
(string.Format ("The '{0}' interface name doesn't have the required 'I' prefix. 
Acoording to existing naming conventions, all interface names should begin with 
the 'I' letter followed by another capital letter.", typeDefinition.Name), 
location, MessageType.Error);
+                                       return new MessageCollection (message);
+                               }
+                       } else {
+                               if (!IsCorrectTypeName (typeDefinition.Name)) { 
// class should _not_ look like 'CSomething"
+                                       Message message = new Message 
(string.Format ("The '{0}' type name starts with 'C' prefix but, according to 
existing naming conventions, type names should not have any specific prefix.", 
typeDefinition.Name), location, MessageType.Error);
+                                       return new MessageCollection (message);
+                               }
+                       }
+                       return runner.RuleSuccess;
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectSuffixRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectSuffixRule.cs    
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UseCorrectSuffixRule.cs    
2007-12-26 18:09:49 UTC (rev 91902)
@@ -0,0 +1,138 @@
+//
+// Gendarme.Rules.Naming.UseCorrectSuffixRule class
+//
+// Authors:
+//     Néstor Salceda <[EMAIL PROTECTED]>
+//      Daniel Abramov <[EMAIL PROTECTED]>
+//
+//  (C) 2007 Néstor Salceda
+//  (C) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Mono.Cecil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Naming {
+
+       public class UseCorrectSuffixRule : ITypeRule {
+
+               // keys are base class names, values are arrays of possible 
suffixes
+               private Dictionary<string, string []> definedSuffixes =
+                       new Dictionary<string, string []> ();
+
+               public UseCorrectSuffixRule ()
+               {
+                       definedSuffixes.Add ("System.Attribute", new string [] 
{ "Attribute" });
+                       definedSuffixes.Add ("System.EventArgs", new string [] 
{ "EventArgs" });
+                       definedSuffixes.Add ("System.Exception", new string [] 
{ "Exception" });
+                       definedSuffixes.Add ("System.Collections.Queue", new 
string [] { "Collection", "Queue" });
+                       definedSuffixes.Add ("System.Collections.Stack", new 
string [] { "Collection", "Stack" });
+                       definedSuffixes.Add ("System.Data.DataSet", new string 
[] { "DataSet" });
+                       definedSuffixes.Add ("System.Data.DataTable", new 
string [] { "DataTable", "Collection" });
+                       definedSuffixes.Add ("System.IO.Stream", new string [] 
{ "Stream" });
+                       definedSuffixes.Add ("System.Security.IPermission", new 
string [] { "Permission" });
+                       definedSuffixes.Add 
("System.Security.Policy.IMembershipCondition", new string [] { "Condition" });
+                       definedSuffixes.Add ("System.Collections.IDictionary", 
new string [] { "Dictionary" });
+                       definedSuffixes.Add 
("System.Collections.Generic.IDictionary", new string [] { "Dictionary" });
+                       definedSuffixes.Add ("System.Collections.ICollection", 
new string [] { "Collection" });
+                       definedSuffixes.Add 
("System.Collections.Generic.ICollection", new string [] { "Collection" });
+                       definedSuffixes.Add ("System.Collections.IEnumerable", 
new string [] { "Collection" });
+               }
+
+               private string [] GetSuffixes (string baseTypeName)
+               {
+                       if (definedSuffixes.ContainsKey (baseTypeName)) {
+                               return definedSuffixes [baseTypeName];
+                       } else {
+                               return new string [] { };
+                       }
+               }
+
+               // checks if type name ends with an approriate suffix
+               // returns array of proposed suffixes via out suffixes 
parameter or empty list (if none)
+               private bool HasRequiredSuffix (TypeDefinition type, out 
List<string> suffixes)
+               {
+                       suffixes = new List<string> ();
+                       TypeDefinition current = type;
+
+                       while (current != null && current.BaseType != null) {
+                               // if we have any suffixes defined by base 
type, we select them
+                               if (definedSuffixes.ContainsKey 
(current.BaseType.FullName)) {
+                                       suffixes.AddRange (GetSuffixes 
(current.BaseType.FullName));
+                               } else {
+                                       // if no suffix for base type is found, 
we start looking through interfaces
+                                       foreach (TypeReference iface in 
current.Interfaces)
+                                               if (definedSuffixes.ContainsKey 
(iface.FullName))
+                                                       suffixes.AddRange 
(GetSuffixes (iface.FullName));
+                               }
+                               if (suffixes.Count > 0) {
+                                       // if any suffixes found
+                                       // check whether type name ends with 
any of these suffixes
+                                       return suffixes.Exists (delegate 
(string suffix) { return type.Name.EndsWith (suffix); });
+                               } else {
+                                       current = current.BaseType as 
TypeDefinition; // inspect base type
+                               }
+                       }
+                       // by default, return true
+                       return true;
+               }
+
+               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
+               {
+                       List<string> proposedSuffixes;
+                       if (HasRequiredSuffix (typeDefinition, out 
proposedSuffixes))
+                               return runner.RuleSuccess;
+
+                       RemoveDuplicates (ref proposedSuffixes);
+                       // there must be some suffixes defined, but type name 
doesn't end with any of them
+                       Location location = new Location 
(typeDefinition.FullName, typeDefinition.Name, 0);
+                       string messageText;
+                       if (proposedSuffixes.Count > 0) {
+                               string joinedSuffixes = proposedSuffixes [0];
+                               if (proposedSuffixes.Count == 1) {
+                                       messageText = string.Format ("The class 
name does not end with '{0}' suffix. Append it to the type name.", 
proposedSuffixes [0]);
+                               } else {
+                                       foreach (string suffix in 
proposedSuffixes)
+                                               joinedSuffixes += ", " + suffix;
+                                       messageText = string.Format ("The class 
name does not end with one of the following suffixes: {0}. Append any of them 
to the type name.", joinedSuffixes);
+                               }
+                       } else {
+                               messageText = "The class name does not end with 
the correct suffix. However Gendarme could not determine what suffix should it 
end with. Contact the author of the rule to fix this bug.";
+                       }
+                       Message message = new Message (messageText, location, 
MessageType.Error);
+                       return new MessageCollection (message);
+               }
+
+               private static void RemoveDuplicates (ref List<string> list)
+               {
+                       List<string> newList = new List<string> ();
+                       foreach (string s in list) {
+                               if (!newList.Contains (s))
+                                       newList.Add (s);
+                       }
+                       list = newList;
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UsePreferredTermsRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UsePreferredTermsRule.cs   
2007-12-26 18:05:20 UTC (rev 91901)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Naming/UsePreferredTermsRule.cs   
2007-12-26 18:09:49 UTC (rev 91902)
@@ -0,0 +1,94 @@
+//
+// Gendarme.Rules.Naming.UsePreferredTermsRule class
+//
+// Authors:
+//     Daniel Abramov <[EMAIL PROTECTED]>
+//
+//     (C) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Mono.Cecil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Naming {
+
+       public class UsePreferredTermsRule : ITypeRule, IMethodRule {
+
+               // keys are obsolete terms, values are preferred ones
+               private Dictionary<string, string> preferredTerms = 
+                       new Dictionary<string, string> ();
+               
+               public UsePreferredTermsRule ()
+               {
+                       // list is based on the FxCop naming rule (as the whole 
rule is inspired by it)
+                       // 
http://www.gotdotnet.com/Team/FxCop/Docs/Rules/Naming/UsePreferredTerms.html
+                       preferredTerms.Add ("ComPlus", "EnterpriseServices");
+                       preferredTerms.Add ("Cancelled", "Canceled");
+                       preferredTerms.Add ("Indices", "Indexes");
+                       preferredTerms.Add ("LogIn", "LogOn");
+                       preferredTerms.Add ("LogOut", "LogOff");
+                       preferredTerms.Add ("SignOn", "SignIn");
+                       preferredTerms.Add ("SignOff", "SignOut");
+                       preferredTerms.Add ("Writeable", "Writable");           
        
+               }
+               
+               // common function checking any identifier
+               private MessageCollection CheckIdentifier (string identifier, 
Location location, Runner runner)
+               {
+                       Dictionary<string, string> foundTerms = new 
Dictionary<string, string> ();
+                       // scan for any obsolete terms
+                       foreach (KeyValuePair<string, string> pair in 
preferredTerms) {
+                               if (identifier.IndexOf (pair.Key, 
StringComparison.InvariantCultureIgnoreCase) != -1) {
+                                       foundTerms.Add (pair.Key, pair.Value);
+                               }
+                       }
+                       if (foundTerms.Count == 0)
+                               return runner.RuleSuccess;
+                       
+                       // form our messages
+                       MessageCollection messages = new MessageCollection ();
+                       foreach (KeyValuePair<string, string> pair in 
foundTerms) {
+                               string errorMessage = string.Format (
+                                       "Obsolete term '{0}' is used in the 
identifier. Replace it with the preferred term '{1}'.",
+                                       pair.Key, pair.Value);
+                               Message message = new Message (errorMessage, 
location, MessageType.Error);
+                               messages.Add (message);
+                       }
+                       return messages;
+               }
+
+               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
+               {
+                       Location location = new Location 
(typeDefinition.FullName, typeDefinition.Name, 0);
+                       return CheckIdentifier (typeDefinition.Name, location, 
runner);
+               }
+
+               public MessageCollection CheckMethod (MethodDefinition 
methodDefinition, Runner runner)
+               {
+                       Location location = new Location 
(methodDefinition.DeclaringType.FullName, methodDefinition.Name, 0);
+                       return CheckIdentifier (methodDefinition.Name, 
location, runner);
+               }
+       }
+}

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to