Skip to content

Commit 6891b50

Browse files
committed
Quick fix
1 parent b814cee commit 6891b50

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Rubberduck.Inspections.Abstract;
2+
using Rubberduck.Inspections.Concrete;
3+
using Rubberduck.Parsing.Grammar;
4+
using Rubberduck.Parsing.Inspections.Abstract;
5+
using Rubberduck.Parsing.VBA;
6+
7+
namespace Rubberduck.Inspections.QuickFixes
8+
{
9+
public sealed class RemoveLocalErrorQuickFix : QuickFixBase
10+
{
11+
private readonly RubberduckParserState _state;
12+
13+
public RemoveLocalErrorQuickFix(RubberduckParserState state)
14+
: base(typeof(OnLocalErrorInspection))
15+
{
16+
_state = state;
17+
}
18+
19+
public override void Fix(IInspectionResult result)
20+
{
21+
var errorStmt = (VBAParser.OnErrorStmtContext)result.Context;
22+
23+
var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
24+
rewriter.Replace(errorStmt.ON_LOCAL_ERROR(), Tokens.On + " " + Tokens.Error);
25+
}
26+
27+
public override string Description(IInspectionResult result) => Resources.Inspections.QuickFixes.RemoveLocalErrorQuickFix;
28+
29+
public override bool CanFixInProcedure => true;
30+
public override bool CanFixInModule => true;
31+
public override bool CanFixInProject => true;
32+
}
33+
}

Rubberduck.CodeAnalysis/Rubberduck.CodeAnalysis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<Compile Include="Inspections\Concrete\ProcedureNotUsedInspection.cs" />
159159
<Compile Include="Properties\AssemblyInfo.cs" />
160160
<Compile Include="QuickFixes\AccessSheetUsingCodeNameQuickFix.cs" />
161+
<Compile Include="QuickFixes\RemoveLocalErrorQuickFix.cs" />
161162
<Compile Include="QuickFixes\RemoveDuplicatedAnnotationQuickFix.cs" />
162163
<Compile Include="QuickFixes\ReplaceIfElseWithConditionalStatementQuickFix.cs" />
163164
<Compile Include="QuickFixes\AddIdentifierToWhiteListQuickFix.cs" />

Rubberduck.Resources/Inspections/QuickFixes.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/Inspections/QuickFixes.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,7 @@
255255
<data name="RemoveDuplicatedAnnotationQuickFix" xml:space="preserve">
256256
<value>Remove duplicated annotation</value>
257257
</data>
258+
<data name="RemoveLocalErrorQuickFix" xml:space="preserve">
259+
<value>Replace 'On Local Error' with 'On Error'</value>
260+
</data>
258261
</root>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
using RubberduckTests.Mocks;
4+
using System.Threading;
5+
using Rubberduck.Inspections.Concrete;
6+
using Rubberduck.Inspections.QuickFixes;
7+
using RubberduckTests.Inspections;
8+
9+
namespace RubberduckTests.QuickFixes
10+
{
11+
[TestFixture]
12+
public class RemoveLocalErrorQuickFixTests
13+
{
14+
[TestCase("On Local Error GoTo 0")]
15+
[TestCase("On Local Error GoTo 1")]
16+
[TestCase(@"On Local Error GoTo Label
17+
Label:")]
18+
[TestCase("On Local Error Resume Next")]
19+
[Category("QuickFixes")]
20+
public void OptionBaseZeroStatement_QuickFixWorks_RemoveStatement(string stmt)
21+
{
22+
var inputCode = $@"Sub foo()
23+
{stmt}
24+
End Sub";
25+
var expectedCode = $@"Sub foo()
26+
{stmt.Replace("Local ", "")}
27+
End Sub";
28+
29+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
30+
using (var state = MockParser.CreateAndParse(vbe.Object))
31+
{
32+
var inspection = new OnLocalErrorInspection(state);
33+
var inspector = InspectionsHelper.GetInspector(inspection);
34+
var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;
35+
36+
new RemoveLocalErrorQuickFix(state).Fix(inspectionResults.First());
37+
Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
38+
}
39+
}
40+
}
41+
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="QuickFixes\AccessSheetUsingCodeNameQuickFixTests.cs" />
135135
<Compile Include="QuickFixes\IntroduceLocalVariableQuickFixTests.cs" />
136136
<Compile Include="QuickFixes\RemoveDuplicatedAnnotationQuickFixTests.cs" />
137+
<Compile Include="QuickFixes\RemoveLocalErrorQuickFixTests.cs" />
137138
<Compile Include="Rewriter\ArgumentRewriterInfoFinderTests.cs" />
138139
<Compile Include="Rewriter\ParameterRewriterInfoFinderTests.cs" />
139140
<Compile Include="Rewriter\ConstantRewriterInfoFinderTests.cs" />

0 commit comments

Comments
 (0)