Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB.NET support for several rules #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CleanCode/src/CleanCode/CleanCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<Compile Include="CleanCodeHighlightingGroupIds.cs" />
<Compile Include="Features\ChainedReferences\MaximumChainedReferencesHighlighting.cs" />
<Compile Include="Features\ChainedReferences\ChainedReferencesCheck.cs" />
<Compile Include="Features\ClassTooBig\ClassTooBigCheckVb.cs" />
<Compile Include="Features\ClassTooBig\ClassTooBigHighlighting.cs" />
<Compile Include="Features\ExcessiveIndentation\ExcessiveIndentationCheck.cs" />
<Compile Include="Features\ComplexExpression\ComplexConditionExpressionHighlighting.cs" />
Expand All @@ -172,18 +173,23 @@
<Compile Include="Features\ExtensionMethods.cs" />
<Compile Include="Features\FlagArguments\FlagArgumentsHighlighting.cs" />
<Compile Include="Features\FlagArguments\FlagArgumentsCheck.cs" />
<Compile Include="Features\HollowNames\HollowNamesCheckVb.cs" />
<Compile Include="Features\HollowNames\HollowTypeNameHighlighting.cs" />
<Compile Include="Features\HollowNames\HollowNamesCheck.cs" />
<Compile Include="Features\MethodNameNotMeaningful\MethodNameNotMeaningfulCheckVb.cs" />
<Compile Include="Features\MethodNameNotMeaningful\MethodNameNotMeaningfulHighlighting.cs" />
<Compile Include="Features\MethodNameNotMeaningful\MethodNameNotMeaningfulCheck.cs" />
<Compile Include="Features\MethodTooLong\MethodTooLongCheckVb.cs" />
<Compile Include="Features\MethodTooLong\MethodTooLongHighlighting.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Features\ClassTooBig\ClassTooBigCheck.cs" />
<Compile Include="Features\TooManyDependencies\TooManyDependenciesCheckVb.cs" />
<Compile Include="Features\TooManyDependencies\TooManyDependenciesHighlighting.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Features\TooManyDependencies\TooManyDependenciesCheck.cs" />
<Compile Include="Features\TooManyMethodArguments\TooManyMethodArgumentsCheckVb.cs" />
<Compile Include="Features\TooManyMethodArguments\TooManyMethodArgumentsCheck.cs" />
<Compile Include="Features\TooManyMethodArguments\TooManyArgumentsHighlighting.cs">
<SubType>Code</SubType>
Expand Down
31 changes: 31 additions & 0 deletions CleanCode/src/CleanCode/Features/ClassTooBig/ClassTooBigCheckVb.cs
Original file line number Diff line number Diff line change
@@ -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<IClassDeclaration>
{
protected override void Run(IClassDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
{
var maxLength = data.SettingsStore.GetValue((CleanCodeSettings s) => s.MaximumMethodsInClass);

var statementCount = element.CountChildren<IMethodDeclaration>();
if (statementCount > maxLength)
{
var declarationIdentifier = element.Name;
var documentRange = declarationIdentifier.GetDocumentRange();
var highlighting = new ClassTooBigHighlighting(Warnings.ClassTooBig, documentRange);
consumer.AddHighlighting(highlighting);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
49 changes: 49 additions & 0 deletions CleanCode/src/CleanCode/Features/HollowNames/HollowNamesCheckVb.cs
Original file line number Diff line number Diff line change
@@ -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<IClassDeclaration>
{
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<string> GetSuffixes(IContextBoundSettingsStore dataSettingsStore)
{
var suffixes = dataSettingsStore.GetValue((CleanCodeSettings s) => s.MeaninglessClassNameSuffixes);
return suffixes.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
}

private static string GetFirstMatchOrDefault(string declaredName, IEnumerable<string> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(HollowTypeNameHighlighting.SeverityID, null,
CleanCodeHighlightingGroupIds.CleanCode, "Hollow type name", "This type has a name that doesn't express its intent.",
Severity.SUGGESTION, false)]

namespace CleanCode.Features.HollowNames
{
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)]
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)]
public class HollowTypeNameHighlighting : IHighlighting
{
internal const string SeverityID = "HollowTypeName";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IMethodDeclaration>
{
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IMethodDeclaration>
{
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<IStatement>();
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<IDeclaration>() ?? 0;
if (declarationCount <= maxDeclarations)
return;
}

var highlighting = new MethodTooLongHighlighting(element.GetNameDocumentRange());
consumer.AddHighlighting(highlighting);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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.",
Severity.SUGGESTION, false)]

namespace CleanCode.Features.MethodTooLong
{
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)]
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)]
public class MethodTooLongHighlighting : IHighlighting
{
internal const string SeverityID = "MethodTooLong";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IConstructorDeclaration>
{
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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.",
Severity.WARNING, false)]

namespace CleanCode.Features.TooManyDependencies
{
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)]
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)]
public class TooManyDependenciesHighlighting : IHighlighting
{
internal const string SeverityID = "TooManyDependencies";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(TooManyArgumentsHighlighting.SeverityID, null,
CleanCodeHighlightingGroupIds.CleanCode, "Too many arguments", "Too many arguments passed to a method.",
Severity.WARNING, false)]

namespace CleanCode.Features.TooManyMethodArguments
{
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name)]
[ConfigurableSeverityHighlighting(SeverityID, CSharpLanguage.Name + "," + VBLanguage.Name)]
public class TooManyArgumentsHighlighting : IHighlighting
{
internal const string SeverityID = "TooManyArguments";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IMethodDeclaration>
{
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);
}
}
}
}