Skip to content

Commit f0b7706

Browse files
committed
Fix crash in Change Procedure to Function quick fix
1 parent 4560320 commit f0b7706

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspectionResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ private void UpdateSignature()
7777

7878
var newArgText = argText.Contains("ByRef ") ? argText.Replace("ByRef ", "ByVal ") : "ByVal " + argText;
7979

80+
var asTypeClause = _argQualifiedContext.Context.asTypeClause() != null
81+
? _argQualifiedContext.Context.asTypeClause().GetText()
82+
: "As Variant";
83+
8084
var newFunctionWithoutReturn = subStmtText.Insert(subStmtText.IndexOf(argListText, StringComparison.Ordinal) + argListText.Length,
81-
" " + _argQualifiedContext.Context.asTypeClause().GetText())
85+
" " + asTypeClause)
8286
.Replace("Sub", "Function")
8387
.Replace(argText, newArgText);
8488

RubberduckTests/Inspections/ProcedureShouldBeFunctionInspectionTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,46 @@ public void ProcedureShouldBeFunction_QuickFixWorks()
330330
Assert.AreEqual(expectedCode, module.Lines());
331331
}
332332

333+
[TestMethod]
334+
[TestCategory("Inspections")]
335+
public void ProcedureShouldBeFunction_QuickFixWorks_NoAsTypeClauseInParam()
336+
{
337+
const string inputCode =
338+
@"Private Sub Foo(ByRef arg1)
339+
End Sub";
340+
341+
const string expectedCode =
342+
@"Private Function Foo(ByVal arg1) As Variant
343+
Foo = arg1
344+
End Function";
345+
346+
//Arrange
347+
var settings = new Mock<IGeneralConfigService>();
348+
var config = GetTestConfig();
349+
settings.Setup(x => x.LoadConfiguration()).Returns(config);
350+
351+
var builder = new MockVbeBuilder();
352+
VBComponent component;
353+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
354+
var project = vbe.Object.VBProjects.Item(0);
355+
var module = project.VBComponents.Item(0).CodeModule;
356+
var mockHost = new Mock<IHostApplication>();
357+
mockHost.SetupAllProperties();
358+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));
359+
360+
parser.Parse(new CancellationTokenSource());
361+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
362+
363+
var inspection = new ProcedureCanBeWrittenAsFunctionInspection(parser.State);
364+
var inspector = new Inspector(settings.Object, new IInspection[] { inspection });
365+
366+
var inspectionResults = inspector.FindIssuesAsync(parser.State, CancellationToken.None).Result;
367+
368+
inspectionResults.First().QuickFixes.First().Fix();
369+
370+
Assert.AreEqual(expectedCode, module.Lines());
371+
}
372+
333373
[TestMethod]
334374
[TestCategory("Inspections")]
335375
public void ProcedureShouldBeFunction_QuickFixWorks_DoesNotInterfereWithBody()

0 commit comments

Comments
 (0)