Skip to content

Commit

Permalink
Merge pull request rubberduck-vba#2575 from retailcoder/next
Browse files Browse the repository at this point in the history
use meaningful names tweaks
  • Loading branch information
retailcoder committed Jan 24, 2017
2 parents c8e45f6 + c9dd5fc commit 693b349
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 563 deletions.
40 changes: 33 additions & 7 deletions RetailCoder.VBE/Inspections/UseMeaningfulNameInspection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Common;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Resources;
using Rubberduck.Inspections.Results;
Expand All @@ -27,22 +28,47 @@ public UseMeaningfulNameInspection(IMessageBox messageBox, RubberduckParserState
public override string Description { get { return InspectionsUI.UseMeaningfulNameInspectionName; } }
public override CodeInspectionType InspectionType { get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; } }

private static readonly DeclarationType[] IgnoreDeclarationTypes =
{
DeclarationType.ModuleOption,
DeclarationType.BracketedExpression,
DeclarationType.LibraryFunction,
DeclarationType.LibraryProcedure,
};

public override IEnumerable<InspectionResultBase> GetInspectionResults()
{
var settings = _settings.Load(new CodeInspectionSettings()) ?? new CodeInspectionSettings();
var whitelistedNames = settings.WhitelistedIdentifiers.Select(s => s.Identifier).ToList();
var whitelistedNames = settings.WhitelistedIdentifiers.Select(s => s.Identifier).ToArray();

var handlers = Declarations.FindBuiltInEventHandlers();

var issues = UserDeclarations
.Where(declaration => declaration.DeclarationType != DeclarationType.ModuleOption &&
!whitelistedNames.Contains(declaration.IdentifierName) &&
(declaration.IdentifierName.Length < 3 ||
char.IsDigit(declaration.IdentifierName.Last()) ||
!declaration.IdentifierName.Any(c =>
"aeiouy".Any(a => string.Compare(a.ToString(), c.ToString(), StringComparison.OrdinalIgnoreCase) == 0))))
.Where(declaration =>
!IgnoreDeclarationTypes.Contains(declaration.DeclarationType) &&
(declaration.ParentDeclaration == null ||
!IgnoreDeclarationTypes.Contains(declaration.ParentDeclaration.DeclarationType) &&
!handlers.Contains(declaration.ParentDeclaration)) &&
!whitelistedNames.Contains(declaration.IdentifierName) &&
IsBadIdentifier(declaration.IdentifierName))
.Select(issue => new IdentifierNameInspectionResult(this, issue, State, _messageBox, _settings))
.ToList();

return issues;
}

private static bool IsBadIdentifier(string identifier)
{
return identifier.Length < 3 ||
char.IsDigit(identifier.Last()) ||
!HasVowels(identifier);
}

private static bool HasVowels(string identifier)
{
const string vowels = "aeiouyàâäéèêëïîöôùûü";
return identifier.Any(character => vowels.Any(vowel =>
string.Compare(vowel.ToString(), character.ToString(), StringComparison.OrdinalIgnoreCase) == 0));
}
}
}
2 changes: 1 addition & 1 deletion Rubberduck.Parsing/Symbols/DeclarationSymbolsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class DeclarationSymbolsListener : VBAParserBaseListener
{
try
{
if (coclass.Key.Count != _qualifiedName.Component.Properties.Count)
if (_qualifiedName.Component == null || coclass.Key.Count != _qualifiedName.Component.Properties.Count)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ End Sub
}

[TestMethod]
[DeploymentItem(@"TestFiles\")]
[TestCategory("Inspections")]
public void ImplicitActiveSheetReference_Ignored_DoesNotReportRange()
{
Expand Down Expand Up @@ -87,8 +88,8 @@ End Sub
Assert.AreEqual(0, inspectionResults.Count());
}

[DeploymentItem(@"TestFiles\")]
[TestMethod]
[DeploymentItem(@"TestFiles\")]
public void ImplicitActiveSheetReference_IgnoreQuickFixWorks()
{
const string inputCode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace RubberduckTests.Inspections
public class ImplicitActiveWorkbookReferenceInspectionTests
{
[TestMethod]
[DeploymentItem(@"TestFiles\")]
[TestCategory("Inspections")]
public void ImplicitActiveWorkbookReference_ReportsWorksheets()
{
Expand Down Expand Up @@ -51,6 +52,7 @@ Sub foo()
}

[TestMethod]
[DeploymentItem(@"TestFiles\")]
[TestCategory("Inspections")]
public void ImplicitActiveWorkbookReference_Ignored_DoesNotReportRange()
{
Expand All @@ -75,6 +77,7 @@ Sub foo()
mockHost.SetupAllProperties();

var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
parser.State.AddTestLibrary("Excel.1.8.xml");

parser.Parse(new CancellationTokenSource());
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
Expand Down
2 changes: 0 additions & 2 deletions RubberduckTests/RubberduckTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@
<Compile Include="Settings\InspectionSettingsTests.cs" />
<Compile Include="Settings\GeneralSettingsTests.cs" />
<Compile Include="Settings\IndenterSettingsTests.cs" />
<Compile Include="Settings\SettingsControlTests.cs" />
<Compile Include="Settings\TodoSettingsTests.cs" />
<Compile Include="Settings\UnitTestSettingsTests.cs" />
<Compile Include="SmartIndenter\EndOfLineCommentTests.cs" />
Expand All @@ -197,7 +196,6 @@
<Compile Include="SourceControl\BranchesViewModelTests.cs" />
<Compile Include="SourceControl\ChangesViewModelTests.cs" />
<Compile Include="SourceControl\SettingsViewModelTests.cs" />
<Compile Include="SourceControl\SourceControlTests.cs" />
<Compile Include="SourceControl\SourceControlViewModelTests.cs" />
<Compile Include="SourceControl\UnsyncedCommitsViewModelTests.cs" />
<Compile Include="StringExtensionsTests.cs" />
Expand Down

0 comments on commit 693b349

Please sign in to comment.