/
DefTypeStatementInspection.cs
94 lines (85 loc) · 3.4 KB
/
DefTypeStatementInspection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Parsing.VBA;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Grammar;
using Antlr4.Runtime;
using Rubberduck.Parsing;
using Rubberduck.VBEditor;
using Antlr4.Runtime.Misc;
using Rubberduck.Inspections.Results;
using Rubberduck.Inspections.Inspections.Extensions;
namespace Rubberduck.Inspections.Concrete
{
/// <summary>
/// Warns about Def[Type] statements.
/// </summary>
/// <why>
/// These declarative statements make the first letter of identifiers determine the data type.
/// </why>
/// <example>
/// <![CDATA[
/// DefBool B
/// DefDbl D
///
/// Public Sub DoSomething()
/// Dim bar ' implicit Boolean
/// ' ...
/// End Sub
/// ]]>
/// </example>
public sealed class DefTypeStatementInspection : ParseTreeInspectionBase
{
public DefTypeStatementInspection(RubberduckParserState state)
: base(state)
{
Listener = new DefTypeStatementInspectionListener();
}
public override IInspectionListener Listener { get; }
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
var results = Listener.Contexts.Where(context => !context.IsIgnoringInspectionResultFor(State.DeclarationFinder, AnnotationName))
.Select(context => new QualifiedContextInspectionResult(this,
string.Format(InspectionResults.DefTypeStatementInspection,
GetTypeOfDefType(context.Context.start.Text),
context.Context.start.Text),
context));
return results;
}
public class DefTypeStatementInspectionListener : VBAParserBaseListener, IInspectionListener
{
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
public QualifiedModuleName CurrentModuleName { get; set; }
public void ClearContexts()
{
_contexts.Clear();
}
public override void ExitDefType([NotNull] VBAParser.DefTypeContext context)
{
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
}
}
private string GetTypeOfDefType(string defType)
{
_defTypes.TryGetValue(defType, out var value);
return value;
}
private readonly Dictionary<string, string> _defTypes = new Dictionary<string, string>
{
{ "DefBool", "Boolean" },
{ "DefByte", "Byte" },
{ "DefInt", "Integer" },
{ "DefLng", "Long" },
{ "DefCur", "Currency" },
{ "DefSng", "Single" },
{ "DefDbl", "Double" },
{ "DefDate", "Date" },
{ "DefStr", "String" },
{ "DefObj", "Object" },
{ "DefVar", "Variant" }
};
}
}