Skip to content

Commit 5d75299

Browse files
committed
adding @IgnoreModule annotation; closes #2352
1 parent 8186131 commit 5d75299

File tree

7 files changed

+145
-19
lines changed

7 files changed

+145
-19
lines changed

RetailCoder.VBE/Inspections/Abstract/InspectionBase.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,27 @@ protected bool IsIgnoringInspectionResultFor(IVBComponent component, int line)
116116

117117
protected bool IsIgnoringInspectionResultFor(Declaration declaration, string inspectionName)
118118
{
119+
var isIgnoredAtModuleLevel =
120+
Declaration.GetModuleParent(declaration).Annotations
121+
.Any(annotation => annotation.AnnotationType == AnnotationType.IgnoreModule
122+
&& ((IgnoreModuleAnnotation) annotation).IsIgnored(inspectionName));
123+
124+
119125
if (declaration.DeclarationType == DeclarationType.Parameter)
120126
{
121-
return declaration.ParentDeclaration.Annotations.Any(annotation =>
127+
return isIgnoredAtModuleLevel || declaration.ParentDeclaration.Annotations.Any(annotation =>
122128
annotation.AnnotationType == AnnotationType.Ignore
123129
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
124130
}
125131

126-
return declaration.Annotations.Any(annotation =>
132+
return isIgnoredAtModuleLevel || declaration.Annotations.Any(annotation =>
127133
annotation.AnnotationType == AnnotationType.Ignore
128134
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
129135
}
130136

131137
protected bool IsIgnoringInspectionResultFor(IdentifierReference reference, string inspectionName)
132138
{
133-
if (reference == null)
134-
{
135-
return false;
136-
}
137-
138-
return reference.Annotations.Any(annotation =>
139-
annotation.AnnotationType == AnnotationType.Ignore
140-
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
139+
return reference != null && reference.IsIgnoringInspectionResultFor(inspectionName);
141140
}
142141

143142
public int CompareTo(IInspection other)

Rubberduck.Parsing/Annotations/AnnotationType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum AnnotationType
1010
TestCleanup,
1111
IgnoreTest,
1212
Ignore,
13+
IgnoreModule,
1314
Folder,
1415
NoIndent,
1516
Interface
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.VBEditor;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public sealed class IgnoreModuleAnnotation : AnnotationBase
8+
{
9+
private readonly IEnumerable<string> _inspectionNames;
10+
11+
public IgnoreModuleAnnotation(QualifiedSelection qualifiedSelection, IEnumerable<string> parameters)
12+
: base(AnnotationType.IgnoreModule, qualifiedSelection)
13+
{
14+
_inspectionNames = parameters;
15+
}
16+
17+
public IEnumerable<string> InspectionNames
18+
{
19+
get
20+
{
21+
return _inspectionNames;
22+
}
23+
}
24+
25+
public bool IsIgnored(string inspectionName)
26+
{
27+
return !_inspectionNames.Any() || _inspectionNames.Contains(inspectionName);
28+
}
29+
30+
public override string ToString()
31+
{
32+
return string.Format("Ignored inspections: {0}", string.Join(", ", _inspectionNames));
33+
}
34+
}
35+
}

Rubberduck.Parsing/Annotations/VBAParserAnnotationFactory.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public VBAParserAnnotationFactory()
1919
_creators.Add(AnnotationType.TestInitialize.ToString().ToUpperInvariant(), typeof(TestInitializeAnnotation));
2020
_creators.Add(AnnotationType.TestCleanup.ToString().ToUpperInvariant(), typeof(TestCleanupAnnotation));
2121
_creators.Add(AnnotationType.Ignore.ToString().ToUpperInvariant(), typeof(IgnoreAnnotation));
22+
_creators.Add(AnnotationType.IgnoreModule.ToString().ToUpperInvariant(), typeof(IgnoreModuleAnnotation));
2223
_creators.Add(AnnotationType.IgnoreTest.ToString().ToUpperInvariant(), typeof(IgnoreTestAnnotation));
2324
_creators.Add(AnnotationType.Folder.ToString().ToUpperInvariant(), typeof(FolderAnnotation));
2425
_creators.Add(AnnotationType.NoIndent.ToString().ToUpperInvariant(), typeof(NoIndentAnnotation));
@@ -27,14 +28,14 @@ public VBAParserAnnotationFactory()
2728

2829
public IAnnotation Create(VBAParser.AnnotationContext context, QualifiedSelection qualifiedSelection)
2930
{
30-
string annotationName = context.annotationName().GetText();
31-
List<string> parameters = AnnotationParametersFromContext(context);
31+
var annotationName = context.annotationName().GetText();
32+
var parameters = AnnotationParametersFromContext(context);
3233
return CreateAnnotation(annotationName, parameters, qualifiedSelection);
3334
}
3435

3536
private static List<string> AnnotationParametersFromContext(VBAParser.AnnotationContext context)
3637
{
37-
List<string> parameters = new List<string>();
38+
var parameters = new List<string>();
3839
var argList = context.annotationArgList();
3940
if (argList != null)
4041
{
@@ -45,10 +46,10 @@ private static List<string> AnnotationParametersFromContext(VBAParser.Annotation
4546

4647
private IAnnotation CreateAnnotation(string annotationName, List<string> parameters, QualifiedSelection qualifiedSelection)
4748
{
48-
Type annotationCLRType = null;
49-
if (_creators.TryGetValue(annotationName.ToUpperInvariant(), out annotationCLRType))
49+
Type annotationClrType;
50+
if (_creators.TryGetValue(annotationName.ToUpperInvariant(), out annotationClrType))
5051
{
51-
return (IAnnotation)Activator.CreateInstance(annotationCLRType, qualifiedSelection, parameters);
52+
return (IAnnotation)Activator.CreateInstance(annotationClrType, qualifiedSelection, parameters);
5253
}
5354
return null;
5455
}

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="Annotations\IAnnotation.cs" />
6767
<Compile Include="Annotations\IAnnotationFactory.cs" />
6868
<Compile Include="Annotations\IgnoreAnnotation.cs" />
69+
<Compile Include="Annotations\IgnoreModuleAnnotation.cs" />
6970
<Compile Include="Annotations\InterfaceAnnotation.cs" />
7071
<Compile Include="Annotations\InvalidAnnotationArgumentException.cs" />
7172
<Compile Include="Annotations\ModuleCleanupAnnotation.cs" />

Rubberduck.Parsing/Symbols/IdentifierReference.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ public IdentifierReference(
7373

7474
public bool IsIgnoringInspectionResultFor(string inspectionName)
7575
{
76-
return Annotations.Any(annotation =>
77-
annotation.AnnotationType == AnnotationType.Ignore
78-
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
76+
var isIgnoredAtModuleLevel =
77+
Declaration.GetModuleParent(_parentScopingDeclaration).Annotations
78+
.Any(annotation => annotation.AnnotationType == AnnotationType.IgnoreModule
79+
&& ((IgnoreModuleAnnotation)annotation).IsIgnored(inspectionName));
80+
81+
return isIgnoredAtModuleLevel || Annotations.Any(annotation =>
82+
annotation.AnnotationType == AnnotationType.Ignore
83+
&& ((IgnoreAnnotation) annotation).IsIgnored(inspectionName));
7984
}
8085

8186
private readonly bool _hasExplicitLetStatement;

RubberduckTests/Inspections/ConstantNotUsedInspectionTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,90 @@ Public Sub Goo(ByVal arg1 As Integer)
131131
Assert.AreEqual(0, inspectionResults.Count());
132132
}
133133

134+
[TestMethod]
135+
[TestCategory("Inspections")]
136+
public void ConstantNotUsed_IgnoreModule_All_YieldsNoResult()
137+
{
138+
const string inputCode =
139+
@"'@IgnoreModule
140+
141+
Public Sub Foo()
142+
Const const1 As Integer = 9
143+
End Sub";
144+
145+
//Arrange
146+
var builder = new MockVbeBuilder();
147+
IVBComponent component;
148+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
149+
var mockHost = new Mock<IHostApplication>();
150+
mockHost.SetupAllProperties();
151+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));
152+
153+
parser.Parse(new CancellationTokenSource());
154+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
155+
156+
var inspection = new ConstantNotUsedInspection(parser.State);
157+
var inspectionResults = inspection.GetInspectionResults();
158+
159+
Assert.IsFalse(inspectionResults.Any());
160+
}
161+
162+
[TestMethod]
163+
[TestCategory("Inspections")]
164+
public void ConstantNotUsed_IgnoreModule_AnnotationName_YieldsNoResult()
165+
{
166+
const string inputCode =
167+
@"'@IgnoreModule ConstantNotUsed
168+
169+
Public Sub Foo()
170+
Const const1 As Integer = 9
171+
End Sub";
172+
173+
//Arrange
174+
var builder = new MockVbeBuilder();
175+
IVBComponent component;
176+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
177+
var mockHost = new Mock<IHostApplication>();
178+
mockHost.SetupAllProperties();
179+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));
180+
181+
parser.Parse(new CancellationTokenSource());
182+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
183+
184+
var inspection = new ConstantNotUsedInspection(parser.State);
185+
var inspectionResults = inspection.GetInspectionResults();
186+
187+
Assert.IsFalse(inspectionResults.Any());
188+
}
189+
190+
[TestMethod]
191+
[TestCategory("Inspections")]
192+
public void ConstantNotUsed_IgnoreModule_OtherAnnotationName_YieldsResults()
193+
{
194+
const string inputCode =
195+
@"'@IgnoreModule VariableNotUsed
196+
197+
Public Sub Foo()
198+
Const const1 As Integer = 9
199+
End Sub";
200+
201+
//Arrange
202+
var builder = new MockVbeBuilder();
203+
IVBComponent component;
204+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
205+
var mockHost = new Mock<IHostApplication>();
206+
mockHost.SetupAllProperties();
207+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));
208+
209+
parser.Parse(new CancellationTokenSource());
210+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
211+
212+
var inspection = new ConstantNotUsedInspection(parser.State);
213+
var inspectionResults = inspection.GetInspectionResults();
214+
215+
Assert.IsTrue(inspectionResults.Any());
216+
}
217+
134218
[TestMethod]
135219
[TestCategory("Inspections")]
136220
public void ConstantNotUsed_Ignored_DoesNotReturnResult()

0 commit comments

Comments
 (0)