Skip to content

Commit e09a073

Browse files
committed
Fix a couple inspections
1 parent 3f7c3cd commit e09a073

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,40 @@ public static IEnumerable<Declaration> FindEventHandlers(this IEnumerable<Declar
220220
&& declaration.IdentifierName.StartsWith(control.IdentifierName + "_"));
221221
}
222222

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

228-
return declarations.Where(declaration => !declaration.IsBuiltIn
228+
// class module built-in events
229+
var classModuleHandlers = declarations.Where(item =>
230+
item.DeclarationType == DeclarationType.Procedure &&
231+
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
232+
(item.IdentifierName == "Class_Initialize" || item.IdentifierName == "Class_Terminate"));
233+
234+
// user form built-in events
235+
var userFormHandlers = declarations.Where(item =>
236+
item.DeclarationType == DeclarationType.Procedure &&
237+
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
238+
item.QualifiedName.QualifiedModuleName.Component.Type == vbext_ComponentType.vbext_ct_MSForm &&
239+
new[]
240+
{
241+
"UserForm_Activate", "UserForm_AddControl", "UserForm_BeforeDragOver", "UserForm_BeforeDropOrPaste",
242+
"UserForm_Click", "UserForm_DblCIick", "UserForm_Deactivate", "UserForm_Error",
243+
"UserForm_Initialize", "UserForm_KeyDown", "UserForm_KeyPress", "UserForm_KeyUp", "UserForm_Layout",
244+
"UserForm_MouseDown", "UserForm_MouseMove", "UserForm_MouseUp", "UserForm_QueryClose",
245+
"UserForm_RemoveControl", "UserForm_Resize", "UserForm_Scroll", "UserForm_Terminate",
246+
"UserForm_Zoom"
247+
}.Contains(item.IdentifierName));
248+
249+
var handlers = declarations.Where(declaration => !declaration.IsBuiltIn
229250
&& declaration.DeclarationType == DeclarationType.Procedure
230-
&& handlerNames.Contains(declaration.IdentifierName));
251+
&& handlerNames.Contains(declaration.IdentifierName)).ToList();
252+
253+
handlers.AddRange(classModuleHandlers);
254+
handlers.AddRange(userFormHandlers);
255+
256+
return handlers;
231257
}
232258

233259
/// <summary>

RetailCoder.VBE/Inspections/ParameterNotUsedInspection.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
using System.Linq;
33
using Microsoft.Vbe.Interop;
44
using Rubberduck.Common;
5-
using Rubberduck.Parsing.Grammar;
65
using Rubberduck.Parsing.Symbols;
76
using Rubberduck.Parsing.VBA;
87
using Rubberduck.Refactorings.RemoveParameters;
98
using Rubberduck.UI;
109
using Rubberduck.UI.Refactorings;
11-
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
1210

1311
namespace Rubberduck.Inspections
1412
{

RetailCoder.VBE/Inspections/ProcedureNotUsedInspection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
4242
handlers.AddRange(forms.SelectMany(form => declarations.FindFormEventHandlers(form)));
4343
}
4444

45+
handlers.AddRange(declarations.FindBuiltInEventHandlers());
46+
4547
var items = declarations
4648
.Where(item => !IsIgnoredDeclaration(declarations, item, handlers, classes, modules)
4749
&& !item.IsInspectionDisabled(AnnotationName)).ToList();

RubberduckTests/Inspections/ParameterNotUsedInspectionTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ public void ParameterUsed_DoesNotReturnResult()
9292
Assert.AreEqual(0, inspectionResults.Count());
9393
}
9494

95+
[TestMethod]
96+
[TestCategory("Inspections")]
97+
public void ParameterUsed_BuiltInEventHandlerParameter_DoesNotReturnResult()
98+
{
99+
const string inputCode =
100+
@"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)
101+
102+
End Sub";
103+
104+
//Arrange
105+
var builder = new MockVbeBuilder();
106+
VBComponent component;
107+
var vbe = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_MSForm, out component, new Rubberduck.VBEditor.Selection());
108+
var mockHost = new Mock<IHostApplication>();
109+
mockHost.SetupAllProperties();
110+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
111+
112+
parser.Parse();
113+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
114+
115+
var inspection = new ParameterNotUsedInspection(vbe.Object, parser.State, null);
116+
var inspectionResults = inspection.GetInspectionResults();
117+
118+
Assert.AreEqual(0, inspectionResults.Count());
119+
}
120+
95121
[TestMethod]
96122
[TestCategory("Inspections")]
97123
public void ParameterNotUsed_ReturnsResult_SomeParamsUsed()

RubberduckTests/Inspections/ProcedureNotUsedInspectionTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,36 @@ Private Sub abc_Foo(ByVal arg1 As Integer, ByVal arg2 As String)
194194
Assert.AreEqual(0, inspectionResults.Count());
195195
}
196196

197+
[TestMethod]
198+
[TestCategory("Inspections")]
199+
public void ProcedureNotUsed_DoesNotReturnResult_BuiltInEventImplementation()
200+
{
201+
//Input
202+
const string inputCode =
203+
@"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)
204+
205+
End Sub";
206+
207+
//Arrange
208+
var builder = new MockVbeBuilder();
209+
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
210+
.AddComponent("Form", vbext_ComponentType.vbext_ct_MSForm, inputCode)
211+
.Build();
212+
var vbe = builder.AddProject(project).Build();
213+
214+
var mockHost = new Mock<IHostApplication>();
215+
mockHost.SetupAllProperties();
216+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
217+
218+
parser.Parse();
219+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
220+
221+
var inspection = new ProcedureNotUsedInspection(parser.State);
222+
var inspectionResults = inspection.GetInspectionResults();
223+
224+
Assert.AreEqual(0, inspectionResults.Count());
225+
}
226+
197227
[TestMethod]
198228
[TestCategory("Inspections")]
199229
public void ProcedureNotUsed_NoResultForClassInitialize()

0 commit comments

Comments
 (0)