Skip to content

Commit

Permalink
Fix a couple inspections
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosch250 committed Jun 6, 2016
1 parent 3f7c3cd commit e09a073
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
32 changes: 29 additions & 3 deletions RetailCoder.VBE/Common/DeclarationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,40 @@ public static IEnumerable<Declaration> FindEventHandlers(this IEnumerable<Declar
&& declaration.IdentifierName.StartsWith(control.IdentifierName + "_"));
}

public static IEnumerable<Declaration> FindBuiltInEventHandlers(this IEnumerable<Declaration> declarations)
public static IEnumerable<Declaration> FindBuiltInEventHandlers(this List<Declaration> declarations)
{
var handlerNames = declarations.Where(declaration => declaration.IsBuiltIn && declaration.DeclarationType == DeclarationType.Event)
.Select(e => e.ParentDeclaration.IdentifierName + "_" + e.IdentifierName);

return declarations.Where(declaration => !declaration.IsBuiltIn
// class module built-in events
var classModuleHandlers = declarations.Where(item =>
item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
(item.IdentifierName == "Class_Initialize" || item.IdentifierName == "Class_Terminate"));

// user form built-in events
var userFormHandlers = declarations.Where(item =>
item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
item.QualifiedName.QualifiedModuleName.Component.Type == vbext_ComponentType.vbext_ct_MSForm &&
new[]
{
"UserForm_Activate", "UserForm_AddControl", "UserForm_BeforeDragOver", "UserForm_BeforeDropOrPaste",
"UserForm_Click", "UserForm_DblCIick", "UserForm_Deactivate", "UserForm_Error",
"UserForm_Initialize", "UserForm_KeyDown", "UserForm_KeyPress", "UserForm_KeyUp", "UserForm_Layout",
"UserForm_MouseDown", "UserForm_MouseMove", "UserForm_MouseUp", "UserForm_QueryClose",
"UserForm_RemoveControl", "UserForm_Resize", "UserForm_Scroll", "UserForm_Terminate",
"UserForm_Zoom"
}.Contains(item.IdentifierName));

var handlers = declarations.Where(declaration => !declaration.IsBuiltIn
&& declaration.DeclarationType == DeclarationType.Procedure
&& handlerNames.Contains(declaration.IdentifierName));
&& handlerNames.Contains(declaration.IdentifierName)).ToList();

handlers.AddRange(classModuleHandlers);
handlers.AddRange(userFormHandlers);

return handlers;
}

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions RetailCoder.VBE/Inspections/ParameterNotUsedInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using System.Linq;
using Microsoft.Vbe.Interop;
using Rubberduck.Common;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Refactorings.RemoveParameters;
using Rubberduck.UI;
using Rubberduck.UI.Refactorings;
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;

namespace Rubberduck.Inspections
{
Expand Down
2 changes: 2 additions & 0 deletions RetailCoder.VBE/Inspections/ProcedureNotUsedInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
handlers.AddRange(forms.SelectMany(form => declarations.FindFormEventHandlers(form)));
}

handlers.AddRange(declarations.FindBuiltInEventHandlers());

var items = declarations
.Where(item => !IsIgnoredDeclaration(declarations, item, handlers, classes, modules)
&& !item.IsInspectionDisabled(AnnotationName)).ToList();
Expand Down
26 changes: 26 additions & 0 deletions RubberduckTests/Inspections/ParameterNotUsedInspectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ public void ParameterUsed_DoesNotReturnResult()
Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void ParameterUsed_BuiltInEventHandlerParameter_DoesNotReturnResult()
{
const string inputCode =
@"Private Sub UserForm_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Control As MSForms.Control, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
End Sub";

//Arrange
var builder = new MockVbeBuilder();
VBComponent component;
var vbe = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_MSForm, out component, new Rubberduck.VBEditor.Selection());
var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

parser.Parse();
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }

var inspection = new ParameterNotUsedInspection(vbe.Object, parser.State, null);
var inspectionResults = inspection.GetInspectionResults();

Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void ParameterNotUsed_ReturnsResult_SomeParamsUsed()
Expand Down
30 changes: 30 additions & 0 deletions RubberduckTests/Inspections/ProcedureNotUsedInspectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,36 @@ public void ProcedureNotUsed_DoesNotReturnResult_EventImplementation()
Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void ProcedureNotUsed_DoesNotReturnResult_BuiltInEventImplementation()
{
//Input
const string inputCode =
@"Private Sub UserForm_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Control As MSForms.Control, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
End Sub";

//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("Form", vbext_ComponentType.vbext_ct_MSForm, inputCode)
.Build();
var vbe = builder.AddProject(project).Build();

var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

parser.Parse();
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }

var inspection = new ProcedureNotUsedInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void ProcedureNotUsed_NoResultForClassInitialize()
Expand Down

0 comments on commit e09a073

Please sign in to comment.