@@ -117,100 +117,95 @@ namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection
117
117
public sealed class UnreachableCaseInspection : InspectionBase , IParseTreeInspection
118
118
{
119
119
private readonly IUnreachableCaseInspectorFactory _unreachableCaseInspectorFactory ;
120
- private readonly IParseTreeValueFactory _valueFactory ;
120
+ private readonly IParseTreeValueVisitorFactory _parseTreeValueVisitorFactory ;
121
+ private readonly UnreachableCaseInspectionListener _listener ;
121
122
122
- private enum CaseInspectionResult { Unreachable , InherentlyUnreachable , MismatchType , Overflow , CaseElse } ;
123
-
124
- private static readonly Dictionary < CaseInspectionResult , string > ResultMessages = new Dictionary < CaseInspectionResult , string > ( )
123
+ public enum CaseInspectionResultType
125
124
{
126
- [ CaseInspectionResult . Unreachable ] = InspectionResults . UnreachableCaseInspection_Unreachable ,
127
- [ CaseInspectionResult . InherentlyUnreachable ] = InspectionResults . UnreachableCaseInspection_InherentlyUnreachable ,
128
- [ CaseInspectionResult . MismatchType ] = InspectionResults . UnreachableCaseInspection_TypeMismatch ,
129
- [ CaseInspectionResult . Overflow ] = InspectionResults . UnreachableCaseInspection_Overflow ,
130
- [ CaseInspectionResult . CaseElse ] = InspectionResults . UnreachableCaseInspection_CaseElse
131
- } ;
125
+ Unreachable ,
126
+ InherentlyUnreachable ,
127
+ MismatchType ,
128
+ Overflow ,
129
+ CaseElse
130
+ }
132
131
133
- public UnreachableCaseInspection ( IDeclarationFinderProvider declarationFinderProvider )
132
+ public UnreachableCaseInspection ( IDeclarationFinderProvider declarationFinderProvider , IUnreachableCaseInspectionFactoryProvider factoryProvider )
134
133
: base ( declarationFinderProvider )
135
134
{
136
- var factoryProvider = new UnreachableCaseInspectionFactoryProvider ( ) ;
137
-
138
135
_unreachableCaseInspectorFactory = factoryProvider . CreateIUnreachableInspectorFactory ( ) ;
139
- _valueFactory = factoryProvider . CreateIParseTreeValueFactory ( ) ;
136
+ _parseTreeValueVisitorFactory = factoryProvider . CreateParseTreeValueVisitorFactory ( ) ;
137
+ _listener = new UnreachableCaseInspectionListener ( ) ;
140
138
}
141
139
142
140
public CodeKind TargetKindOfCode => CodeKind . CodePaneCode ;
143
141
144
- public IInspectionListener Listener { get ; } =
145
- new UnreachableCaseInspectionListener ( ) ;
146
-
147
- private ParseTreeVisitorResults ValueResults { get ; } = new ParseTreeVisitorResults ( ) ;
142
+ public IInspectionListener Listener => _listener ;
148
143
149
144
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
150
145
{
151
146
var finder = DeclarationFinderProvider . DeclarationFinder ;
147
+ var enumStmts = _listener . EnumerationStmtContexts ( ) ;
148
+ var parseTreeValueVisitor = CreateParseTreeValueVisitor ( enumStmts , GetIdentifierReferenceForContext ) ;
152
149
153
150
return finder . UserDeclarations ( DeclarationType . Module )
154
151
. Where ( module => module != null )
155
- . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder ) )
152
+ . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder , parseTreeValueVisitor ) )
156
153
. ToList ( ) ;
157
154
}
158
155
159
156
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module )
160
157
{
161
158
var finder = DeclarationFinderProvider . DeclarationFinder ;
162
- return DoGetInspectionResults ( module , finder ) ;
159
+ var enumStmts = _listener . EnumerationStmtContexts ( ) ;
160
+ var parseTreeValueVisitor = CreateParseTreeValueVisitor ( enumStmts , GetIdentifierReferenceForContext ) ;
161
+ return DoGetInspectionResults ( module , finder , parseTreeValueVisitor ) ;
163
162
}
164
163
165
- private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder )
164
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder , IParseTreeValueVisitor parseTreeValueVisitor )
166
165
{
167
166
var qualifiedSelectCaseStmts = Listener . Contexts ( module )
168
167
// ignore filtering here to make the search space smaller
169
168
. Where ( result => ! result . IsIgnoringInspectionResultFor ( finder , AnnotationName ) ) ;
170
169
171
- ParseTreeValueVisitor . OnValueResultCreated += ValueResults . OnNewValueResult ;
172
-
173
170
return qualifiedSelectCaseStmts
174
- . SelectMany ( ResultsForContext )
171
+ . SelectMany ( context => ResultsForContext ( context , finder , parseTreeValueVisitor ) )
175
172
. ToList ( ) ;
176
173
}
177
174
178
- private IEnumerable < IInspectionResult > ResultsForContext ( QualifiedContext < ParserRuleContext > qualifiedSelectCaseStmt )
175
+ private IEnumerable < IInspectionResult > ResultsForContext ( QualifiedContext < ParserRuleContext > qualifiedSelectCaseStmt , DeclarationFinder finder , IParseTreeValueVisitor parseTreeValueVisitor )
179
176
{
180
- qualifiedSelectCaseStmt . Context . Accept ( ParseTreeValueVisitor ) ;
181
- var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( ( VBAParser . SelectCaseStmtContext ) qualifiedSelectCaseStmt . Context , ValueResults , _valueFactory , GetVariableTypeName ) ;
177
+ var contextValues = qualifiedSelectCaseStmt . Context . Accept ( parseTreeValueVisitor ) ;
178
+ var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( ( VBAParser . SelectCaseStmtContext ) qualifiedSelectCaseStmt . Context , contextValues , GetVariableTypeName ) ;
182
179
183
- selectCaseInspector . InspectForUnreachableCases ( ) ;
180
+ var results = selectCaseInspector . InspectForUnreachableCases ( ) ;
184
181
185
- return selectCaseInspector
186
- . UnreachableCases
187
- . Select ( uc => CreateInspectionResult ( qualifiedSelectCaseStmt , uc , ResultMessages [ CaseInspectionResult . Unreachable ] ) )
188
- . Concat ( selectCaseInspector
189
- . MismatchTypeCases
190
- . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . MismatchType ] ) ) )
191
- . Concat ( selectCaseInspector
192
- . OverflowCases
193
- . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . Overflow ] ) ) )
194
- . Concat ( selectCaseInspector
195
- . InherentlyUnreachableCases
196
- . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . InherentlyUnreachable ] ) ) )
197
- . Concat ( selectCaseInspector
198
- . UnreachableCaseElseCases
199
- . Select ( ce => CreateInspectionResult ( qualifiedSelectCaseStmt , ce , ResultMessages [ CaseInspectionResult . CaseElse ] ) ) )
182
+ return results
183
+ . Select ( resultTpl => CreateInspectionResult ( qualifiedSelectCaseStmt , resultTpl . context , resultTpl . resultType ) )
200
184
. ToList ( ) ;
201
185
}
202
186
203
- private IParseTreeValueVisitor _parseTreeValueVisitor ;
204
- public IParseTreeValueVisitor ParseTreeValueVisitor
187
+ private IInspectionResult CreateInspectionResult ( QualifiedContext < ParserRuleContext > selectStmt , ParserRuleContext unreachableBlock , CaseInspectionResultType resultType )
205
188
{
206
- get
189
+ return CreateInspectionResult ( selectStmt , unreachableBlock , ResultMessage ( resultType ) ) ;
190
+ }
191
+
192
+ //This cannot be a dictionary because the strings have to change after a change in the selected language.
193
+ private static string ResultMessage ( CaseInspectionResultType resultType )
194
+ {
195
+ switch ( resultType )
207
196
{
208
- if ( _parseTreeValueVisitor is null )
209
- {
210
- var listener = ( UnreachableCaseInspectionListener ) Listener ;
211
- _parseTreeValueVisitor = CreateParseTreeValueVisitor ( _valueFactory , listener . EnumerationStmtContexts ( ) , GetIdentifierReferenceForContext ) ;
212
- }
213
- return _parseTreeValueVisitor ;
197
+ case CaseInspectionResultType . Unreachable :
198
+ return InspectionResults . UnreachableCaseInspection_Unreachable ;
199
+ case CaseInspectionResultType . InherentlyUnreachable :
200
+ return InspectionResults . UnreachableCaseInspection_InherentlyUnreachable ;
201
+ case CaseInspectionResultType . MismatchType :
202
+ return InspectionResults . UnreachableCaseInspection_TypeMismatch ;
203
+ case CaseInspectionResultType . Overflow :
204
+ return InspectionResults . UnreachableCaseInspection_Overflow ;
205
+ case CaseInspectionResultType . CaseElse :
206
+ return InspectionResults . UnreachableCaseInspection_CaseElse ;
207
+ default :
208
+ throw new ArgumentOutOfRangeException ( nameof ( resultType ) , resultType , null ) ;
214
209
}
215
210
}
216
211
@@ -221,8 +216,8 @@ private IInspectionResult CreateInspectionResult(QualifiedContext<ParserRuleCont
221
216
new QualifiedContext < ParserRuleContext > ( selectStmt . ModuleName , unreachableBlock ) ) ;
222
217
}
223
218
224
- public static IParseTreeValueVisitor CreateParseTreeValueVisitor ( IParseTreeValueFactory valueFactory , IReadOnlyList < VBAParser . EnumerationStmtContext > allEnums , Func < ParserRuleContext , ( bool success , IdentifierReference idRef ) > func )
225
- => new ParseTreeValueVisitor ( valueFactory , allEnums , func ) ;
219
+ public IParseTreeValueVisitor CreateParseTreeValueVisitor ( IReadOnlyList < VBAParser . EnumerationStmtContext > allEnums , Func < ParserRuleContext , ( bool success , IdentifierReference idRef ) > func )
220
+ => _parseTreeValueVisitorFactory . Create ( allEnums , func ) ;
226
221
227
222
//Method is used as a delegate to avoid propagating RubberduckParserState beyond this class
228
223
private ( bool success , IdentifierReference idRef ) GetIdentifierReferenceForContext ( ParserRuleContext context )
0 commit comments