diff --git a/CleanCode/src/CleanCode/CleanCode.csproj b/CleanCode/src/CleanCode/CleanCode.csproj index 3e43a4d..1d59634 100644 --- a/CleanCode/src/CleanCode/CleanCode.csproj +++ b/CleanCode/src/CleanCode/CleanCode.csproj @@ -164,6 +164,7 @@ + @@ -172,18 +173,23 @@ + + + Code + Code + Code diff --git a/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigCheckVb.cs b/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigCheckVb.cs new file mode 100644 index 0000000..e27105e --- /dev/null +++ b/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigCheckVb.cs @@ -0,0 +1,31 @@ +using CleanCode.Resources; +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; + +namespace CleanCode.Features.ClassTooBig +{ + [ElementProblemAnalyzer(typeof(IClassDeclaration), HighlightingTypes = new [] + { + typeof(ClassTooBigHighlighting) + })] + public class ClassTooBigCheckVb : ElementProblemAnalyzer + { + protected override void Run(IClassDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + var maxLength = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumMethodsInClass); + + var statementCount = element.CountChildren(); + if (statementCount > maxLength) + { + var declarationIdentifier = element.Name; + var documentRange = declarationIdentifier.GetDocumentRange(); + var highlighting = new ClassTooBigHighlighting(Warnings.ClassTooBig, documentRange); + consumer.AddHighlighting(highlighting); + } + } + } +} \ No newline at end of file diff --git a/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigHighlighting.cs b/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigHighlighting.cs index 5a06983..d11544e 100644 --- a/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigHighlighting.cs @@ -3,14 +3,15 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(ClassTooBigHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Class too big", "This class contains too many methods", Severity.SUGGESTION, false)] namespace CleanCode.Features.ClassTooBig -{ - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] +{ + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class ClassTooBigHighlighting : IHighlighting { internal const string SeverityID = "ClassTooBig"; diff --git a/CleanCode/src/CleanCode/Features/HollowNames/HollowNamesCheckVb.cs b/CleanCode/src/CleanCode/Features/HollowNames/HollowNamesCheckVb.cs new file mode 100644 index 0000000..64e3668 --- /dev/null +++ b/CleanCode/src/CleanCode/Features/HollowNames/HollowNamesCheckVb.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CleanCode.Resources; +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; + +namespace CleanCode.Features.HollowNames +{ + [ElementProblemAnalyzer(typeof(IClassDeclaration), + HighlightingTypes = new [] + { + typeof(HollowTypeNameHighlighting) + })] + public class HollowNamesCheckVb : ElementProblemAnalyzer + { + protected override void Run(IClassDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + var suffixes = GetSuffixes(data.SettingsStore); + + var match = GetFirstMatchOrDefault(element.DeclaredName, suffixes); + if (match != null) + AddHighlighting(match, consumer, element); + } + + private IEnumerable GetSuffixes(IContextBoundSettingsStore dataSettingsStore) + { + var suffixes = dataSettingsStore.GetValue((CleanCodeSettings s) => s.MeaninglessClassNameSuffixes); + return suffixes.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); + } + + private static string GetFirstMatchOrDefault(string declaredName, IEnumerable suffixes) + { + return suffixes.FirstOrDefault(declaredName.EndsWith); + } + + private void AddHighlighting(string bannedSuffix, IHighlightingConsumer consumer, IClassDeclaration typeExpression) + { + var identifier = typeExpression.Name; + var documentRange = identifier.GetDocumentRange(); + var highlighting = new HollowTypeNameHighlighting(string.Format(Warnings.HollowTypeName, bannedSuffix), documentRange); + consumer.AddHighlighting(highlighting); + } + } +} \ No newline at end of file diff --git a/CleanCode/src/CleanCode/Features/HollowNames/HollowTypeNameHighlighting.cs b/CleanCode/src/CleanCode/Features/HollowNames/HollowTypeNameHighlighting.cs index adbc538..4741142 100644 --- a/CleanCode/src/CleanCode/Features/HollowNames/HollowTypeNameHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/HollowNames/HollowTypeNameHighlighting.cs @@ -3,6 +3,7 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(HollowTypeNameHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Hollow type name", "This type has a name that doesn't express its intent.", @@ -10,7 +11,7 @@ namespace CleanCode.Features.HollowNames { - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class HollowTypeNameHighlighting : IHighlighting { internal const string SeverityID = "HollowTypeName"; diff --git a/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulCheckVb.cs b/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulCheckVb.cs new file mode 100644 index 0000000..a9fbbde --- /dev/null +++ b/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulCheckVb.cs @@ -0,0 +1,33 @@ +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; + +namespace CleanCode.Features.MethodNameNotMeaningful +{ + [ElementProblemAnalyzer(typeof(IMethodDeclaration), HighlightingTypes = new [] + { + typeof(MethodNameNotMeaningfulHighlighting) + })] + public class MethodNameNotMeaningfulCheckVb : ElementProblemAnalyzer + { + protected override void Run(IMethodDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + if (element.Name == null) + return; + + var minimumMethodNameLength = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MinimumMeaningfulMethodNameLength); + + var name = element.Name.GetText(); + var methodNameLength = name.Length; + if (methodNameLength < minimumMethodNameLength) + { + var documentRange = element.GetNameDocumentRange(); + var highlighting = new MethodNameNotMeaningfulHighlighting(documentRange); + consumer.AddHighlighting(highlighting); + } + } + } +} \ No newline at end of file diff --git a/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulHighlighting.cs b/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulHighlighting.cs index 5eb9160..7db196a 100644 --- a/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/MethodNameNotMeaningful/MethodNameNotMeaningfulHighlighting.cs @@ -4,6 +4,7 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(MethodNameNotMeaningfulHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Method name not meaningful", @@ -12,7 +13,7 @@ namespace CleanCode.Features.MethodNameNotMeaningful { - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class MethodNameNotMeaningfulHighlighting : IHighlighting { internal const string SeverityID = "MethodNameNotMeaningful"; diff --git a/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongCheckVb.cs b/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongCheckVb.cs new file mode 100644 index 0000000..a5a6346 --- /dev/null +++ b/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongCheckVb.cs @@ -0,0 +1,36 @@ +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; + +namespace CleanCode.Features.MethodTooLong +{ + [ElementProblemAnalyzer(typeof(IMethodDeclaration), HighlightingTypes = new [] + { + typeof(MethodTooLongHighlighting) + })] + public class MethodTooLongCheckVb : ElementProblemAnalyzer + { + protected override void Run(IMethodDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + var maxStatements = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumMethodStatements); + var maxDeclarations = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumDeclarationsInMethod); + + var statementCount = element.CountChildren(); + if (statementCount <= maxStatements) + { + // Only look in the method body for declarations, otherwise we see + // parameters + type parameters. We can ignore arrow expressions, as + // they must be a single expression and won't have declarations + var declarationCount = element.Block?.CountChildren() ?? 0; + if (declarationCount <= maxDeclarations) + return; + } + + var highlighting = new MethodTooLongHighlighting(element.GetNameDocumentRange()); + consumer.AddHighlighting(highlighting); + } + } +} \ No newline at end of file diff --git a/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongHighlighting.cs b/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongHighlighting.cs index 3d1e0c6..3c27658 100644 --- a/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/MethodTooLong/MethodTooLongHighlighting.cs @@ -4,6 +4,7 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(MethodTooLongHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Method too long", "The method is bigger than it should be.", @@ -11,7 +12,7 @@ namespace CleanCode.Features.MethodTooLong { - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class MethodTooLongHighlighting : IHighlighting { internal const string SeverityID = "MethodTooLong"; diff --git a/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesCheckVb.cs b/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesCheckVb.cs new file mode 100644 index 0000000..6608fa8 --- /dev/null +++ b/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesCheckVb.cs @@ -0,0 +1,34 @@ +using System.Linq; +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; +using JetBrains.ReSharper.Psi.Util; + +namespace CleanCode.Features.TooManyDependencies +{ + [ElementProblemAnalyzer(typeof(IConstructorDeclaration), HighlightingTypes = new [] + { + typeof(TooManyDependenciesHighlighting) + })] + public class TooManyDependenciesCheckVb : ElementProblemAnalyzer + { + protected override void Run(IConstructorDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + var maxDependencies = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumConstructorDependencies); + + var dependencies = element.ParameterDeclarations.Select( + declaration => declaration.DeclaredElement != null && + declaration.DeclaredElement.Type.IsInterfaceType()); + + var dependenciesCount = dependencies.Count(); + if (dependenciesCount > maxDependencies) + { + var highlighting = new TooManyDependenciesHighlighting(element.GetNameDocumentRange()); + consumer.AddHighlighting(highlighting); + } + } + } +} \ No newline at end of file diff --git a/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesHighlighting.cs b/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesHighlighting.cs index 4b4492d..96931fd 100644 --- a/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/TooManyDependencies/TooManyDependenciesHighlighting.cs @@ -4,6 +4,7 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(TooManyDependenciesHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Too many dependencies", "Too many dependencies passed into constructor.", @@ -11,7 +12,7 @@ namespace CleanCode.Features.TooManyDependencies { - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class TooManyDependenciesHighlighting : IHighlighting { internal const string SeverityID = "TooManyDependencies"; diff --git a/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyArgumentsHighlighting.cs b/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyArgumentsHighlighting.cs index 2075a63..043a093 100644 --- a/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyArgumentsHighlighting.cs +++ b/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyArgumentsHighlighting.cs @@ -3,6 +3,7 @@ using JetBrains.DocumentModel; using JetBrains.ReSharper.Feature.Services.Daemon; using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.VB; [assembly: RegisterConfigurableSeverity(TooManyArgumentsHighlighting.SeverityID, null, CleanCodeHighlightingGroupIds.CleanCode, "Too many arguments", "Too many arguments passed to a method.", @@ -10,7 +11,7 @@ namespace CleanCode.Features.TooManyMethodArguments { - [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)] + [ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)] public class TooManyArgumentsHighlighting : IHighlighting { internal const string SeverityID = "TooManyArguments"; diff --git a/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyMethodArgumentsCheckVb.cs b/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyMethodArgumentsCheckVb.cs new file mode 100644 index 0000000..63386ca --- /dev/null +++ b/CleanCode/src/CleanCode/Features/TooManyMethodArguments/TooManyMethodArgumentsCheckVb.cs @@ -0,0 +1,30 @@ +using CleanCode.Resources; +using CleanCode.Settings; +using JetBrains.Application.Settings; +using JetBrains.ReSharper.Daemon.Stages.Dispatcher; +using JetBrains.ReSharper.Feature.Services.Daemon; +using JetBrains.ReSharper.Psi.VB.Tree; +using JetBrains.ReSharper.Psi.Tree; + +namespace CleanCode.Features.TooManyMethodArguments +{ + [ElementProblemAnalyzer(typeof(IMethodDeclaration), HighlightingTypes = new [] + { + typeof(TooManyArgumentsHighlighting) + })] + public class TooManyMethodArgumentsCheckVb : ElementProblemAnalyzer + { + protected override void Run(IMethodDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) + { + var maxParameters = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumMethodParameters); + var parameterDeclarations = element.ParameterDeclarations; + + if (parameterDeclarations.Count > maxParameters) + { + var highlighting = new TooManyArgumentsHighlighting(Warnings.TooManyMethodArguments, + element.GetNameDocumentRange()); + consumer.AddHighlighting(highlighting); + } + } + } +} \ No newline at end of file