Skip to content

Commit b981d67

Browse files
committed
Add inspection
1 parent e300f48 commit b981d67

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Inspections.Abstract;
4+
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Parsing.Inspections.Resources;
6+
using Rubberduck.Parsing.Inspections.Abstract;
7+
using Rubberduck.Parsing.Grammar;
8+
using Antlr4.Runtime;
9+
using Rubberduck.Parsing;
10+
using Rubberduck.VBEditor;
11+
using Antlr4.Runtime.Misc;
12+
using Rubberduck.Inspections.Results;
13+
14+
namespace Rubberduck.Inspections.Concrete
15+
{
16+
public sealed class DefTypeStatementInspection : ParseTreeInspectionBase
17+
{
18+
public DefTypeStatementInspection(RubberduckParserState state)
19+
: base(state, CodeInspectionSeverity.Suggestion)
20+
{
21+
Listener = new DefTypeStatementInspectionListener();
22+
}
23+
24+
public override CodeInspectionType InspectionType => CodeInspectionType.LanguageOpportunities;
25+
public override IInspectionListener Listener { get; }
26+
27+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
28+
{
29+
var results = Listener.Contexts.Where(context => !IsIgnoringInspectionResultFor(context.ModuleName, context.Context.Start.Line))
30+
.Select(context => new QualifiedContextInspectionResult(this,
31+
string.Format(InspectionsUI.DefTypeStatementInspectionResultFormat,
32+
GetTypeOfDefType(context.Context.start.Text),
33+
context.Context.start.Text),
34+
context));
35+
return results;
36+
}
37+
38+
public class DefTypeStatementInspectionListener : VBAParserBaseListener, IInspectionListener
39+
{
40+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
41+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
42+
43+
public QualifiedModuleName CurrentModuleName { get; set; }
44+
45+
public void ClearContexts()
46+
{
47+
_contexts.Clear();
48+
}
49+
50+
public override void ExitDefType([NotNull] VBAParser.DefTypeContext context)
51+
{
52+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
53+
}
54+
}
55+
56+
private string GetTypeOfDefType(string defType)
57+
{
58+
_defTypes.TryGetValue(defType, out var value);
59+
return value;
60+
}
61+
62+
private readonly Dictionary<string, string> _defTypes = new Dictionary<string, string>
63+
{
64+
{ "DefBool", "Boolean" },
65+
{ "DefByte", "Byte" },
66+
{ "DefInt", "Integer" },
67+
{ "DefLng", "Long" },
68+
{ "DefCur", "Currency" },
69+
{ "DefSng", "Single" },
70+
{ "DefDbl", "Double" },
71+
{ "DefDate", "Date" },
72+
{ "DefStr", "String" },
73+
{ "DefObj", "Object" },
74+
{ "DefVar", "Variant" }
75+
};
76+
}
77+
}

Rubberduck.Inspections/Rubberduck.Inspections.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Abstract\ParseTreeInspectionBase.cs" />
6262
<Compile Include="Concrete\ApplicationWorksheetFunctionInspection.cs" />
6363
<Compile Include="Concrete\AssignedByValParameterInspection.cs" />
64+
<Compile Include="Concrete\DefTypeStatementInspection.cs" />
6465
<Compile Include="Concrete\EmptyModuleInspection.cs" />
6566
<Compile Include="Concrete\EmptyBlockInspectionListenerBase.cs" />
6667
<Compile Include="Concrete\EmptyCaseBlockInspection.cs" />

0 commit comments

Comments
 (0)