Skip to content

Commit 16155cc

Browse files
authored
Merge pull request #1912 from Hosch250/Issue1877
Do not fire Obsolete Call inspection when call has instruction separators
2 parents 7484c9e + 3f57d3d commit 16155cc

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

RetailCoder.VBE/Inspections/ObsoleteCallStatementInspection.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using Rubberduck.Parsing;
43
using Rubberduck.Parsing.Grammar;
54
using Rubberduck.Parsing.VBA;
@@ -25,9 +24,30 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
2524
return new InspectionResultBase[] { };
2625
}
2726

28-
return ParseTreeResults.ObsoleteCallContexts.Select(context =>
29-
new ObsoleteCallStatementUsageInspectionResult(this,
30-
new QualifiedContext<VBAParser.CallStmtContext>(context.ModuleName, context.Context as VBAParser.CallStmtContext)));
27+
var results = new List<ObsoleteCallStatementUsageInspectionResult>();
28+
29+
foreach (var context in ParseTreeResults.ObsoleteCallContexts)
30+
{
31+
var lines = context.ModuleName.Component.CodeModule.Lines[
32+
context.Context.Start.Line, context.Context.Stop.Line - context.Context.Start.Line + 1];
33+
34+
var stringStrippedLines = string.Join(string.Empty, lines).StripStringLiterals();
35+
36+
int commentIndex;
37+
if (stringStrippedLines.HasComment(out commentIndex))
38+
{
39+
stringStrippedLines = stringStrippedLines.Remove(commentIndex);
40+
}
41+
42+
if (!stringStrippedLines.Contains(":"))
43+
{
44+
results.Add(new ObsoleteCallStatementUsageInspectionResult(this,
45+
new QualifiedContext<VBAParser.CallStmtContext>(context.ModuleName,
46+
context.Context as VBAParser.CallStmtContext)));
47+
}
48+
}
49+
50+
return results;
3151
}
3252

3353
public class ObsoleteCallStatementListener : VBAParserBaseListener

RubberduckTests/Inspections/ObsoleteCallStatementInspectionTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,102 @@ public void ObsoleteCallStatement_DoesNotReturnResult()
8080
Assert.AreEqual(0, inspectionResults.Count());
8181
}
8282

83+
[TestMethod]
84+
[TestCategory("Inspections")]
85+
public void ObsoleteCallStatement_DoesNotReturnResult_InstructionSeparator()
86+
{
87+
const string inputCode =
88+
@"Sub Foo()
89+
Call Foo: Foo
90+
End Sub";
91+
92+
//Arrange
93+
var settings = new Mock<IGeneralConfigService>();
94+
var config = GetTestConfig();
95+
settings.Setup(x => x.LoadConfiguration()).Returns(config);
96+
97+
var builder = new MockVbeBuilder();
98+
VBComponent component;
99+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
100+
var mockHost = new Mock<IHostApplication>();
101+
mockHost.SetupAllProperties();
102+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
103+
104+
parser.Parse();
105+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
106+
107+
var inspection = new ObsoleteCallStatementInspection(parser.State);
108+
var inspector = new Inspector(settings.Object, new IInspection[] { inspection });
109+
110+
var inspectionResults = inspector.FindIssuesAsync(parser.State, CancellationToken.None).Result;
111+
112+
Assert.AreEqual(0, inspectionResults.Count());
113+
}
114+
115+
[TestMethod]
116+
[TestCategory("Inspections")]
117+
public void ObsoleteCallStatement_ReturnsResult_ColonInComment()
118+
{
119+
const string inputCode =
120+
@"Sub Foo()
121+
Call Foo ' I''ve got a colon: see?
122+
End Sub";
123+
124+
//Arrange
125+
var settings = new Mock<IGeneralConfigService>();
126+
var config = GetTestConfig();
127+
settings.Setup(x => x.LoadConfiguration()).Returns(config);
128+
129+
var builder = new MockVbeBuilder();
130+
VBComponent component;
131+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
132+
var mockHost = new Mock<IHostApplication>();
133+
mockHost.SetupAllProperties();
134+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
135+
136+
parser.Parse();
137+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
138+
139+
var inspection = new ObsoleteCallStatementInspection(parser.State);
140+
var inspector = new Inspector(settings.Object, new IInspection[] { inspection });
141+
142+
var inspectionResults = inspector.FindIssuesAsync(parser.State, CancellationToken.None).Result;
143+
144+
Assert.AreEqual(1, inspectionResults.Count());
145+
}
146+
147+
[TestMethod]
148+
[TestCategory("Inspections")]
149+
public void ObsoleteCallStatement_ReturnsResult_ColonInStringLiteral()
150+
{
151+
const string inputCode =
152+
@"Sub Foo(ByVal str As String)
153+
Call Foo("":"")
154+
End Sub";
155+
156+
//Arrange
157+
var settings = new Mock<IGeneralConfigService>();
158+
var config = GetTestConfig();
159+
settings.Setup(x => x.LoadConfiguration()).Returns(config);
160+
161+
var builder = new MockVbeBuilder();
162+
VBComponent component;
163+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
164+
var mockHost = new Mock<IHostApplication>();
165+
mockHost.SetupAllProperties();
166+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
167+
168+
parser.Parse();
169+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
170+
171+
var inspection = new ObsoleteCallStatementInspection(parser.State);
172+
var inspector = new Inspector(settings.Object, new IInspection[] { inspection });
173+
174+
var inspectionResults = inspector.FindIssuesAsync(parser.State, CancellationToken.None).Result;
175+
176+
Assert.AreEqual(1, inspectionResults.Count());
177+
}
178+
83179
[TestMethod]
84180
[TestCategory("Inspections")]
85181
public void ObsoleteCallStatement_ReturnsMultipleResults()

0 commit comments

Comments
 (0)