Skip to content

Commit b26b8f4

Browse files
committed
Make ParseTreeValueVisitor.VisitChildren pure
Also turns the ParseTreeValueVisitor in UnreachableCaseInspection into a constructor injected field.
1 parent ab922fd commit b26b8f4

File tree

6 files changed

+154
-140
lines changed

6 files changed

+154
-140
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/ParseTreeValueVisitor.cs

Lines changed: 127 additions & 56 deletions
Large diffs are not rendered by default.

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/ParseTreeValueVisitorFactory.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/UnreachableCaseInspection.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection
116116
public sealed class UnreachableCaseInspection : InspectionBase, IParseTreeInspection
117117
{
118118
private readonly IUnreachableCaseInspectorFactory _unreachableCaseInspectorFactory;
119-
private readonly IParseTreeValueVisitorFactory _parseTreeValueVisitorFactory;
120-
private readonly UnreachableCaseInspectionListener _listener;
119+
private readonly IParseTreeValueVisitor _parseTreeValueVisitor;
120+
private readonly IInspectionListener<VBAParser.SelectCaseStmtContext> _listener;
121121

122122
public enum CaseInspectionResultType
123123
{
@@ -128,11 +128,11 @@ public enum CaseInspectionResultType
128128
CaseElse
129129
}
130130

131-
public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderProvider, IUnreachableCaseInspectionFactoryProvider factoryProvider)
131+
public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderProvider, IUnreachableCaseInspectionFactoryProvider factoryProvider, IParseTreeValueVisitor parseTreeValueVisitor)
132132
: base(declarationFinderProvider)
133133
{
134134
_unreachableCaseInspectorFactory = factoryProvider.CreateIUnreachableInspectorFactory();
135-
_parseTreeValueVisitorFactory = factoryProvider.CreateParseTreeValueVisitorFactory();
135+
_parseTreeValueVisitor = parseTreeValueVisitor;
136136
_listener = new UnreachableCaseInspectionListener();
137137
}
138138

@@ -142,38 +142,36 @@ public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderPro
142142

143143
protected override IEnumerable<IInspectionResult> DoGetInspectionResults(DeclarationFinder finder)
144144
{
145-
var parseTreeValueVisitor = CreateParseTreeValueVisitor(GetIdentifierReferenceForContextFunction(finder));
146145
var selectCaseInspector = _unreachableCaseInspectorFactory.Create(GetVariableTypeNameFunction(finder));
147146

148147
return finder.UserDeclarations(DeclarationType.Module)
149148
.Where(module => module != null)
150-
.SelectMany(module => DoGetInspectionResults(module.QualifiedModuleName, finder, parseTreeValueVisitor, selectCaseInspector))
149+
.SelectMany(module => DoGetInspectionResults(module.QualifiedModuleName, finder, selectCaseInspector))
151150
.ToList();
152151
}
153152

154153
protected override IEnumerable<IInspectionResult> DoGetInspectionResults(QualifiedModuleName module, DeclarationFinder finder)
155154
{
156-
var parseTreeValueVisitor = CreateParseTreeValueVisitor(GetIdentifierReferenceForContextFunction(finder));
157155
var selectCaseInspector = _unreachableCaseInspectorFactory.Create(GetVariableTypeNameFunction(finder));
158-
return DoGetInspectionResults(module, finder, parseTreeValueVisitor, selectCaseInspector);
156+
return DoGetInspectionResults(module, finder, selectCaseInspector);
159157
}
160158

161-
private IEnumerable<IInspectionResult> DoGetInspectionResults(QualifiedModuleName module, DeclarationFinder finder, IParseTreeValueVisitor parseTreeValueVisitor, IUnreachableCaseInspector selectCaseInspector)
159+
private IEnumerable<IInspectionResult> DoGetInspectionResults(QualifiedModuleName module, DeclarationFinder finder, IUnreachableCaseInspector selectCaseInspector)
162160
{
163161
var qualifiedSelectCaseStmts = _listener.Contexts(module)
164162
// ignore filtering here to make the search space smaller
165163
.Where(result => !result.IsIgnoringInspectionResultFor(finder, AnnotationName));
166164

167165
return qualifiedSelectCaseStmts
168-
.SelectMany(context => ResultsForContext(context, parseTreeValueVisitor, selectCaseInspector))
166+
.SelectMany(context => ResultsForContext(context, selectCaseInspector, finder))
169167
.ToList();
170168
}
171169

172-
private IEnumerable<IInspectionResult> ResultsForContext(QualifiedContext<VBAParser.SelectCaseStmtContext> qualifiedSelectCaseStmt, IParseTreeValueVisitor parseTreeValueVisitor, IUnreachableCaseInspector selectCaseInspector)
170+
private IEnumerable<IInspectionResult> ResultsForContext(QualifiedContext<VBAParser.SelectCaseStmtContext> qualifiedSelectCaseStmt, IUnreachableCaseInspector selectCaseInspector, DeclarationFinder finder)
173171
{
174172
var module = qualifiedSelectCaseStmt.ModuleName;
175173
var selectStmt = qualifiedSelectCaseStmt.Context;
176-
var contextValues = parseTreeValueVisitor.VisitChildren(module, selectStmt);
174+
var contextValues = _parseTreeValueVisitor.VisitChildren(module, selectStmt, finder);
177175

178176
var results = selectCaseInspector.InspectForUnreachableCases(module, selectStmt, contextValues);
179177

@@ -214,11 +212,6 @@ private IInspectionResult CreateInspectionResult(QualifiedContext<VBAParser.Sele
214212
new QualifiedContext<ParserRuleContext>(selectStmt.ModuleName, unreachableBlock));
215213
}
216214

217-
public IParseTreeValueVisitor CreateParseTreeValueVisitor(Func<QualifiedModuleName, ParserRuleContext, (bool success, IdentifierReference idRef)> func)
218-
{
219-
return _parseTreeValueVisitorFactory.Create(func);
220-
}
221-
222215
private Func<QualifiedModuleName, ParserRuleContext,(bool success, IdentifierReference reference)> GetIdentifierReferenceForContextFunction(DeclarationFinder finder)
223216
{
224217
return (module, context) => GetIdentifierReferenceForContext(module, context, finder);
@@ -287,7 +280,7 @@ private string GetBaseTypeForDeclaration(Declaration declaration)
287280
return localDeclaration is null ? declaration.AsTypeName : localDeclaration.AsTypeName;
288281
}
289282

290-
public class UnreachableCaseInspectionListener : InspectionListenerBase<VBAParser.SelectCaseStmtContext>
283+
private class UnreachableCaseInspectionListener : InspectionListenerBase<VBAParser.SelectCaseStmtContext>
291284
{
292285
public override void EnterSelectCaseStmt([NotNull] VBAParser.SelectCaseStmtContext context)
293286
{

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/UnreachableCaseInspectionFactoryProvider.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ public interface IUnreachableCaseInspectionFactoryProvider
55
{
66
IParseTreeValueFactory CreateIParseTreeValueFactory();
77
IUnreachableCaseInspectorFactory CreateIUnreachableInspectorFactory();
8-
IParseTreeValueVisitorFactory CreateParseTreeValueVisitorFactory();
98
}
109

1110
public class UnreachableCaseInspectionFactoryProvider : IUnreachableCaseInspectionFactoryProvider
@@ -19,10 +18,5 @@ public IUnreachableCaseInspectorFactory CreateIUnreachableInspectorFactory()
1918
{
2019
return new UnreachableCaseInspectorFactory(CreateIParseTreeValueFactory());
2120
}
22-
23-
public IParseTreeValueVisitorFactory CreateParseTreeValueVisitorFactory()
24-
{
25-
return new ParseTreeValueVisitorFactory(CreateIParseTreeValueFactory());
26-
}
2721
}
2822
}

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,6 @@ private void RegisterUnreachableCaseFactories(IWindsorContainer container)
385385
container.Register(Component.For<IUnreachableCaseInspectorFactory>()
386386
.ImplementedBy<UnreachableCaseInspectorFactory>()
387387
.LifestyleSingleton());
388-
container.Register(Component.For<IParseTreeValueVisitorFactory>()
389-
.ImplementedBy<ParseTreeValueVisitorFactory>()
390-
.LifestyleSingleton());
391388
container.Register(Component.For<IUnreachableCaseInspectionFactoryProvider>()
392389
.ImplementedBy<UnreachableCaseInspectionFactoryProvider>()
393390
.LifestyleSingleton());

RubberduckTests/Inspections/UnreachableCase/UnreachableCaseInspectionTests.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
using System.Collections.Generic;
1414
using System.Linq;
1515
using System.Threading;
16-
using Antlr4.Runtime.Tree;
1716
using Moq;
18-
using Rubberduck.Parsing.VBA.Parsing;
1917
using Rubberduck.VBEditor;
2018
using Rubberduck.VBEditor.Extensions;
2119

@@ -2462,8 +2460,9 @@ End Sub
24622460
IEnumerable<IInspectionResult> actualResults;
24632461
using (var state = MockParser.CreateAndParse(vbe.Object))
24642462
{
2465-
var factoryProvider = SpecialValueDeclarationEvaluatorFactoryProvider(TestGetValuedDeclaration);
2466-
var inspection = new UnreachableCaseInspection(state, factoryProvider);
2463+
var factoryProvider = FactoryProvider;
2464+
var parseTreeVisitor = TestParseTreeValueVisitor(TestGetValuedDeclaration);
2465+
var inspection = new UnreachableCaseInspection(state, factoryProvider, parseTreeVisitor);
24672466

24682467
WalkTrees(inspection, state);
24692468
actualResults = inspection.GetInspectionResults(CancellationToken.None);
@@ -2514,8 +2513,9 @@ End Sub
25142513
IEnumerable<IInspectionResult> actualResults;
25152514
using (var state = MockParser.CreateAndParse(vbe.Object))
25162515
{
2517-
var factoryProvider = SpecialValueDeclarationEvaluatorFactoryProvider(TestGetValuedDeclaration);
2518-
var inspection = new UnreachableCaseInspection(state, factoryProvider);
2516+
var factoryProvider = FactoryProvider;
2517+
var parseTreeVisitor = TestParseTreeValueVisitor(TestGetValuedDeclaration);
2518+
var inspection = new UnreachableCaseInspection(state, factoryProvider, parseTreeVisitor);
25192519

25202520
WalkTrees(inspection, state);
25212521
actualResults = inspection.GetInspectionResults(CancellationToken.None);
@@ -2530,27 +2530,6 @@ End Sub
25302530
private IUnreachableCaseInspectionFactoryProvider FactoryProvider => _factoryProvider ?? (_factoryProvider = new UnreachableCaseInspectionFactoryProvider());
25312531

25322532
private IUnreachableCaseInspectorFactory UnreachableCaseInspectorFactory => FactoryProvider.CreateIUnreachableInspectorFactory();
2533-
private IParseTreeValueVisitorFactory ParseTreeValueVisitorFactory => FactoryProvider.CreateParseTreeValueVisitorFactory();
2534-
2535-
private IUnreachableCaseInspectionFactoryProvider SpecialValueDeclarationEvaluatorFactoryProvider(Func<Declaration, (bool, string, string)> valueDeclarationEvaluator)
2536-
{
2537-
var baseFactoryProvider = FactoryProvider;
2538-
var factoryProviderMock = new Mock<IUnreachableCaseInspectionFactoryProvider>();
2539-
factoryProviderMock.Setup(m => m.CreateIParseTreeValueFactory()).Returns(() => baseFactoryProvider.CreateIParseTreeValueFactory());
2540-
factoryProviderMock.Setup(m => m.CreateIUnreachableInspectorFactory()).Returns(() => baseFactoryProvider.CreateIUnreachableInspectorFactory());
2541-
factoryProviderMock.Setup(m => m.CreateParseTreeValueVisitorFactory()).Returns(() =>
2542-
SpecialValueDeclarationEvaluatorParseTreeValueVisitorFactory(baseFactoryProvider.CreateIParseTreeValueFactory(), valueDeclarationEvaluator));
2543-
return factoryProviderMock.Object;
2544-
}
2545-
2546-
private IParseTreeValueVisitorFactory SpecialValueDeclarationEvaluatorParseTreeValueVisitorFactory(IParseTreeValueFactory valueFactory, Func<Declaration, (bool, string, string)> valueDeclarationEvaluator)
2547-
{
2548-
var factoryMock = new Mock<IParseTreeValueVisitorFactory>();
2549-
factoryMock.Setup(m => m.Create(It.IsAny<Func<QualifiedModuleName, ParserRuleContext, (bool success, IdentifierReference idRef)>>()))
2550-
.Returns<Func<QualifiedModuleName, ParserRuleContext, (bool success, IdentifierReference idRef)>>((identifierReferenceRetriever) =>
2551-
new ParseTreeValueVisitor(valueFactory, identifierReferenceRetriever, valueDeclarationEvaluator));
2552-
return factoryMock.Object;
2553-
}
25542533

25552534
private static Dictionary<string, (string, string)> _vbConstConversions = new Dictionary<string, (string, string)>()
25562535
{
@@ -2637,17 +2616,24 @@ private IParseTreeVisitorResults GetParseTreeValueResults(string inputCode, out
26372616
var (parseTreeModule, moduleParseTree) = state.ParseTrees
26382617
.First(pt => pt.Value is ParserRuleContext);
26392618
selectStmt = ((ParserRuleContext)moduleParseTree).GetDescendent<VBAParser.SelectCaseStmtContext>();
2640-
var visitor = ParseTreeValueVisitorFactory.Create((module, context) => UnreachableCaseInspection.GetIdentifierReferenceForContext(module, context, finder));
2641-
valueResults = visitor.VisitChildren(parseTreeModule, selectStmt);
2619+
var visitor = TestParseTreeValueVisitor();
2620+
valueResults = visitor.VisitChildren(parseTreeModule, selectStmt, finder);
26422621
contextModule = parseTreeModule;
26432622
}
26442623
return valueResults;
26452624
}
26462625

2626+
private ParseTreeValueVisitor TestParseTreeValueVisitor(Func<Declaration, (bool, string, string)> valueDeclarationEvaluator = null)
2627+
{
2628+
var valueFactory = new ParseTreeValueFactory();
2629+
return new ParseTreeValueVisitor(valueFactory, valueDeclarationEvaluator);
2630+
}
2631+
26472632
protected override IInspection InspectionUnderTest(RubberduckParserState state)
26482633
{
26492634
var factoryProvider = new UnreachableCaseInspectionFactoryProvider();
2650-
return new UnreachableCaseInspection(state, factoryProvider);
2635+
var parseTeeValueVisitor = new ParseTreeValueVisitor(factoryProvider.CreateIParseTreeValueFactory());
2636+
return new UnreachableCaseInspection(state, factoryProvider, parseTeeValueVisitor);
26512637
}
26522638
}
26532639
}

0 commit comments

Comments
 (0)