/
IsMissingInspectionBase.cs
61 lines (52 loc) · 2.36 KB
/
IsMissingInspectionBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using NLog;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
namespace Rubberduck.Inspections.Inspections.Abstract
{
public abstract class IsMissingInspectionBase : InspectionBase
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
protected IsMissingInspectionBase(RubberduckParserState state)
: base(state) { }
private static readonly List<string> IsMissingQualifiedNames = new List<string>
{
"VBE7.DLL;VBA.Information.IsMissing",
"VBA6.DLL;VBA.Information.IsMissing"
};
protected IReadOnlyList<Declaration> IsMissingDeclarations
{
get
{
var isMissing = BuiltInDeclarations.Where(decl => IsMissingQualifiedNames.Contains(decl.QualifiedName.ToString())).ToList();
if (isMissing.Count == 0)
{
_logger.Trace("No 'IsMissing' Declarations were found in IsMissingInspectionBase.");
}
return isMissing;
}
}
protected ParameterDeclaration GetParameterForReference(IdentifierReference reference)
{
// First case is for unqualified use: IsMissing(foo)
// Second case if for use as a member access: VBA.IsMissing(foo)
var argument = ((ParserRuleContext)reference.Context.Parent).GetDescendent<VBAParser.ArgumentExpressionContext>() ??
((ParserRuleContext)reference.Context.Parent.Parent).GetDescendent<VBAParser.ArgumentExpressionContext>();
var name = argument?.GetDescendent<VBAParser.SimpleNameExprContext>();
if (name == null || name.Parent.Parent != argument)
{
return null;
}
var procedure = reference.Context.GetAncestor<VBAParser.ModuleBodyElementContext>();
return UserDeclarations.Where(decl => decl is ModuleBodyElementDeclaration)
.Cast<ModuleBodyElementDeclaration>()
.FirstOrDefault(decl => decl.Context.Parent == procedure)?
.Parameters.FirstOrDefault(param => param.IdentifierName.Equals(name.GetText()));
}
}
}