Skip to content

Commit 2b7a0c9

Browse files
committed
Initial Hungarian notation inspection commit
1 parent b48635f commit 2b7a0c9

File tree

6 files changed

+206
-3
lines changed

6 files changed

+206
-3
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
7+
using Rubberduck.Inspections.Abstract;
8+
using Rubberduck.Inspections.Resources;
9+
using Rubberduck.Inspections.Results;
10+
using Rubberduck.Parsing.Symbols;
11+
using Rubberduck.Parsing.VBA;
12+
13+
namespace Rubberduck.Inspections
14+
{
15+
public sealed class HungarianNotationInspection : InspectionBase
16+
{
17+
#region statics
18+
private static readonly List<string> HungarianPrefixes = new List<string>
19+
{
20+
"chk",
21+
"cbo",
22+
"cmd",
23+
"btn",
24+
"fra",
25+
"img",
26+
"lbl",
27+
"lst",
28+
"mnu",
29+
"opt",
30+
"pic",
31+
"shp",
32+
"txt",
33+
"tmr",
34+
"chk",
35+
"dlg",
36+
"drv",
37+
"frm",
38+
"grd",
39+
"obj",
40+
"rpt",
41+
"fld",
42+
"idx",
43+
"tbl",
44+
"tbd",
45+
"bas",
46+
"cls",
47+
"g",
48+
"m",
49+
"bln",
50+
"byt",
51+
"col",
52+
"dtm",
53+
"dbl",
54+
"cur",
55+
"int",
56+
"lng",
57+
"sng",
58+
"str",
59+
"udt",
60+
"vnt",
61+
"var",
62+
"pgr",
63+
"dao",
64+
"b",
65+
"by",
66+
"c",
67+
"chr",
68+
"i",
69+
"l",
70+
"s",
71+
"o",
72+
"n",
73+
"dt",
74+
"dat",
75+
"a",
76+
"arr"
77+
};
78+
79+
private static readonly Regex HungarianIdentifierRegex = new Regex(string.Format("^({0})[A-Z0-9].*$", string.Join("|", HungarianPrefixes)));
80+
81+
private static readonly List<DeclarationType> TargetDeclarationTypes = new List<DeclarationType>
82+
{
83+
DeclarationType.Parameter,
84+
DeclarationType.Constant,
85+
DeclarationType.Control,
86+
DeclarationType.ClassModule,
87+
DeclarationType.Member,
88+
DeclarationType.Module,
89+
DeclarationType.ProceduralModule,
90+
DeclarationType.UserForm,
91+
DeclarationType.UserDefinedType,
92+
DeclarationType.UserDefinedTypeMember,
93+
DeclarationType.Variable
94+
};
95+
96+
#endregion
97+
98+
public HungarianNotationInspection(RubberduckParserState state, CodeInspectionSeverity defaultSeverity = CodeInspectionSeverity.Suggestion)
99+
: base(state, defaultSeverity)
100+
{
101+
}
102+
103+
public override string Description
104+
{
105+
get { return InspectionsUI.HungarianNotationInspectionName; }
106+
}
107+
108+
public override CodeInspectionType InspectionType
109+
{
110+
get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; }
111+
}
112+
113+
public override IEnumerable<InspectionResultBase> GetInspectionResults()
114+
{
115+
var hungarians = UserDeclarations
116+
.Where(declaration => TargetDeclarationTypes.Contains(declaration.DeclarationType) &&
117+
HungarianIdentifierRegex.IsMatch(declaration.IdentifierName))
118+
.Select(issue => new HungarianNotationInspectionResult(this, issue))
119+
.ToList();
120+
return hungarians;
121+
}
122+
}
123+
}

RetailCoder.VBE/Inspections/Resources/InspectionsUI.Designer.cs

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/Inspections/Resources/InspectionsUI.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,13 @@ If the parameter can be null, ignore this inspection result; passing a null valu
606606
<value>{0} ({1} results)</value>
607607
<comment>{0} inpection description, {1} result count</comment>
608608
</data>
609+
<data name="HungarianNotationInspectionMeta" xml:space="preserve">
610+
<value>Hungarian notation makes code less readable, and is redundant when strongly typed variables and meaningful names are used.</value>
611+
</data>
612+
<data name="HungarianNotationInspectionName" xml:space="preserve">
613+
<value>Variable uses Hungarian notation.</value>
614+
</data>
615+
<data name="HungarianNotationInspectionResultFormat" xml:space="preserve">
616+
<value>Consider renaming {0} '{1}'</value>
617+
</data>
609618
</root>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
using Antlr4.Runtime;
3+
using Rubberduck.Common;
4+
using Rubberduck.Inspections.Abstract;
5+
using Rubberduck.Inspections.QuickFixes;
6+
using Rubberduck.Inspections.Resources;
7+
using Rubberduck.Parsing.Symbols;
8+
using Rubberduck.UI;
9+
using Rubberduck.VBEditor;
10+
11+
namespace Rubberduck.Inspections.Results
12+
{
13+
public sealed class HungarianNotationInspectionResult : InspectionResultBase
14+
{
15+
private readonly IEnumerable<QuickFixBase> _quickFixes;
16+
17+
public HungarianNotationInspectionResult(IInspection inspection, Declaration target)
18+
: base(inspection, target)
19+
{
20+
_quickFixes = new QuickFixBase[]
21+
{
22+
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName),
23+
};
24+
}
25+
26+
public override string Description
27+
{
28+
get
29+
{
30+
return
31+
string.Format(InspectionsUI.HungarianNotationInspectionResultFormat,
32+
RubberduckUI.ResourceManager.GetString("DeclarationType_" + Target.DeclarationType,
33+
UI.Settings.Settings.Culture), Target.IdentifierName).Captialize();
34+
}
35+
}
36+
37+
public override NavigateCodeEventArgs GetNavigationArgs()
38+
{
39+
return new NavigateCodeEventArgs(Target);
40+
}
41+
}
42+
}

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,15 @@
365365
<Compile Include="Common\WinAPI\WM.cs" />
366366
<Compile Include="Common\WindowsOperatingSystem.cs" />
367367
<Compile Include="Common\UndocumentedAttribute.cs" />
368+
<Compile Include="Inspections\HungarianNotationInspection.cs" />
368369
<Compile Include="Inspections\ImplicitDefaultMemberAssignmentInspection.cs" />
369370
<Compile Include="Inspections\Resources\InspectionsUI.Designer.cs">
370371
<AutoGen>True</AutoGen>
371372
<DesignTime>True</DesignTime>
372373
<DependentUpon>InspectionsUI.resx</DependentUpon>
373374
</Compile>
374375
<Compile Include="Inspections\Results\AggregateInspectionResult.cs" />
376+
<Compile Include="Inspections\Results\HungarianNotationInspectionResult.cs" />
375377
<Compile Include="Inspections\Results\ImplicitDefaultMemberAssignmentInspectionResult.cs" />
376378
<Compile Include="Inspections\QuickFixes\IntroduceLocalVariableQuickFix.cs" />
377379
<Compile Include="Inspections\QuickFixes\OptionExplicitQuickFix.cs" />

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@
305305
<Compile Include="VBA\VBAModuleParser.cs" />
306306
</ItemGroup>
307307
<ItemGroup>
308-
<None Include="Grammar\VBAParser.g4">
308+
<Antlr4 Include="Grammar\VBAParser.g4">
309309
<Generator>MSBuild:Compile</Generator>
310310
<CustomToolNamespace>Rubberduck.Parsing.Grammar</CustomToolNamespace>
311311
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
312-
</None>
312+
</Antlr4>
313313
<None Include="packages.config" />
314314
<None Include="Grammar\VBALexer.g4">
315315
<Generator>MSBuild:Compile</Generator>

0 commit comments

Comments
 (0)