Skip to content

Commit fde7744

Browse files
authored
Merge pull request #3400 from rubberduck-vba/revert-3376-next
Revert "Consolidate Empty(If|Else)Block into EmptyConditionBlock"
2 parents f107b55 + 986629e commit fde7744

16 files changed

+1851
-2281
lines changed

Rubberduck.Inspections/Concrete/EmptyConditionBlockInspection.cs

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Antlr4.Runtime.Misc;
2+
using Rubberduck.Inspections.Abstract;
3+
using Rubberduck.Inspections.Results;
4+
using Rubberduck.Parsing.Grammar;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Parsing.Inspections.Resources;
7+
using Rubberduck.Parsing.VBA;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Rubberduck.Inspections.Concrete
13+
{
14+
internal class EmptyElseBlockInspection : ParseTreeInspectionBase
15+
{
16+
public EmptyElseBlockInspection(RubberduckParserState state)
17+
: base(state, CodeInspectionSeverity.Suggestion) { }
18+
19+
public override Type Type => typeof(EmptyElseBlockInspection);
20+
21+
public override CodeInspectionType InspectionType => CodeInspectionType.CodeQualityIssues;
22+
23+
public override IEnumerable<IInspectionResult> GetInspectionResults()
24+
{
25+
return Listener.Contexts
26+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
27+
.Select(result => new QualifiedContextInspectionResult(this,
28+
InspectionsUI.EmptyElseBlockInspectionResultFormat,
29+
result));
30+
}
31+
32+
public override IInspectionListener Listener { get; }
33+
= new EmptyElseBlockListener();
34+
35+
public class EmptyElseBlockListener : EmptyBlockInspectionListenerBase
36+
{
37+
public override void EnterElseBlock([NotNull] VBAParser.ElseBlockContext context)
38+
{
39+
InspectBlockForExecutableStatements(context.block(), context);
40+
}
41+
}
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Misc;
3+
using Rubberduck.Inspections.Abstract;
4+
using Rubberduck.Inspections.Results;
5+
using Rubberduck.Parsing;
6+
using Rubberduck.Parsing.Grammar;
7+
using Rubberduck.Parsing.Inspections.Abstract;
8+
using Rubberduck.Parsing.Inspections.Resources;
9+
using Rubberduck.Parsing.VBA;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
14+
namespace Rubberduck.Inspections.Concrete
15+
{
16+
internal class EmptyIfBlockInspection : ParseTreeInspectionBase
17+
{
18+
public EmptyIfBlockInspection(RubberduckParserState state)
19+
: base(state, CodeInspectionSeverity.Suggestion) { }
20+
21+
public override Type Type => typeof(EmptyIfBlockInspection);
22+
23+
public override CodeInspectionType InspectionType => CodeInspectionType.CodeQualityIssues;
24+
25+
public override IEnumerable<IInspectionResult> GetInspectionResults()
26+
{
27+
return Listener.Contexts
28+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
29+
.Select(result => new QualifiedContextInspectionResult(this,
30+
InspectionsUI.EmptyIfBlockInspectionResultFormat,
31+
result));
32+
}
33+
34+
public override IInspectionListener Listener { get; } =
35+
new EmptyIfBlockListener();
36+
37+
public class EmptyIfBlockListener : EmptyBlockInspectionListenerBase
38+
{
39+
public override void EnterIfStmt([NotNull] VBAParser.IfStmtContext context)
40+
{
41+
InspectBlockForExecutableStatements(context.block(), context);
42+
}
43+
44+
public override void EnterElseIfBlock([NotNull] VBAParser.ElseIfBlockContext context)
45+
{
46+
InspectBlockForExecutableStatements(context.block(), context);
47+
}
48+
49+
public override void EnterSingleLineIfStmt([NotNull] VBAParser.SingleLineIfStmtContext context)
50+
{
51+
if (context.ifWithEmptyThen() != null)
52+
{
53+
AddResult(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context.ifWithEmptyThen()));
54+
}
55+
}
56+
}
57+
}
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Antlr4.Runtime.Tree;
2+
using Antlr4.Runtime;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Rubberduck.Inspections.Concrete;
9+
using Rubberduck.Parsing.Inspections.Abstract;
10+
using Rubberduck.Parsing.Inspections.Resources;
11+
using Rubberduck.Parsing.Grammar;
12+
using Rubberduck.Parsing.Rewriter;
13+
using Rubberduck.Parsing.VBA;
14+
15+
16+
namespace Rubberduck.Inspections.QuickFixes
17+
{
18+
class RemoveEmptyElseBlockQuickFix : IQuickFix
19+
{
20+
private static readonly HashSet<Type> _supportedInspections = new HashSet<Type> { typeof(EmptyElseBlockInspection) };
21+
private readonly RubberduckParserState _state;
22+
23+
public IReadOnlyCollection<Type> SupportedInspections => _supportedInspections.ToList();
24+
25+
public RemoveEmptyElseBlockQuickFix(RubberduckParserState state)
26+
{
27+
_state = state;
28+
}
29+
30+
public void Fix(IInspectionResult result)
31+
{
32+
IModuleRewriter rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
33+
34+
//dynamic used since it's not known at run-time
35+
UpdateContext((dynamic)result.Context, rewriter);
36+
}
37+
38+
private void UpdateContext(VBAParser.ElseBlockContext context, IModuleRewriter rewriter)
39+
{
40+
VBAParser.BlockContext elseBlock = context.block();
41+
42+
if (elseBlock.ChildCount == 0 )
43+
{
44+
//string rewrittenBlock = AdjustedBlockText(context.block());
45+
//rewriter.InsertBefore(context.start.StartIndex, rewrittenBlock);
46+
rewriter.Remove(context);
47+
}
48+
/*
49+
* There isn't any need to invert the condition since its
50+
* only the else block thats empty. IE: it doesn't affect
51+
* the TRUE portion that preceeds it.
52+
*/
53+
}
54+
55+
//private bool FirstBlockStmntHasLabel(VBAParser.BlockContext block)
56+
// => block.blockStmt()?.FirstOrDefault()?.statementLabelDefinition() != null;
57+
58+
//private bool BlockHasDeclaration(VBAParser.BlockContext block)
59+
// => block.blockStmt()?.Any() ?? false;
60+
61+
62+
public string Description(IInspectionResult result)
63+
{
64+
return InspectionsUI.RemoveEmptyElseBlockQuickFix;
65+
}
66+
67+
public bool CanFixInProcedure { get; } = false;
68+
public bool CanFixInModule { get; } = false;
69+
public bool CanFixInProject { get; } = false;
70+
}
71+
}

Rubberduck.Inspections/QuickFixes/RemoveEmptyConditionBlockQuickFix.cs renamed to Rubberduck.Inspections/QuickFixes/RemoveEmptyIfBlockQuickFix.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
5+
using Antlr4.Runtime;
46
using Rubberduck.Inspections.Concrete;
5-
using Rubberduck.Parsing.VBA;
6-
using Rubberduck.Parsing.Inspections.Abstract;
77
using Rubberduck.Parsing.Grammar;
8-
using Rubberduck.Parsing.Rewriter;
9-
using Antlr4.Runtime;
8+
using Rubberduck.Parsing.Inspections.Abstract;
109
using Rubberduck.Parsing.Inspections.Resources;
11-
using System.Diagnostics;
10+
using Rubberduck.Parsing.Rewriter;
11+
using Rubberduck.Parsing.VBA;
1212

1313
namespace Rubberduck.Inspections.QuickFixes
1414
{
15-
internal sealed class RemoveEmptyConditionBlockQuickFix : IQuickFix
15+
internal sealed class RemoveEmptyIfBlockQuickFix : IQuickFix
1616
{
17-
private static readonly HashSet<Type> _supportedInspections = new HashSet<Type> { typeof(EmptyConditionBlockInspection) };
17+
private static readonly HashSet<Type> _supportedInspections = new HashSet<Type> { typeof(EmptyIfBlockInspection) };
1818
private readonly RubberduckParserState _state;
1919

20-
public RemoveEmptyConditionBlockQuickFix(RubberduckParserState state)
20+
public RemoveEmptyIfBlockQuickFix(RubberduckParserState state)
2121
{
2222
_state = state;
2323
}
@@ -92,16 +92,6 @@ private void UpdateContext(VBAParser.ElseIfBlockContext context, IModuleRewriter
9292
rewriter.Remove(context);
9393
}
9494

95-
private void UpdateContext(VBAParser.ElseBlockContext context, IModuleRewriter rewriter)
96-
{
97-
var elseBlock = context.block();
98-
99-
if (elseBlock.ChildCount == 0)
100-
{
101-
rewriter.Remove(context);
102-
}
103-
}
104-
10595
private void UpdateCondition(VBAParser.RelationalOpContext condition, IModuleRewriter rewriter)
10696
{
10797
if (condition.EQ() != null)
@@ -188,7 +178,7 @@ private bool FirstBlockStmntHasLabel(VBAParser.BlockContext block)
188178

189179
public string Description(IInspectionResult result)
190180
{
191-
return InspectionsUI.RemoveEmptyConditionBlockQuickFix;
181+
return InspectionsUI.RemoveEmptyIfBlockQuickFix;
192182
}
193183

194184
public bool CanFixInProcedure { get; } = false;

Rubberduck.Inspections/Rubberduck.Inspections.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
<Compile Include="Concrete\AssignedByValParameterInspection.cs" />
6363
<Compile Include="Concrete\EmptyBlockInspectionListenerBase.cs" />
6464
<Compile Include="Concrete\EmptyCaseBlockInspection.cs" />
65-
<Compile Include="Concrete\EmptyConditionBlockInspection.cs" />
6665
<Compile Include="Concrete\EmptyDoWhileBlockInspection.cs" />
6766
<Compile Include="Concrete\EmptyForEachBlockInspection.cs" />
6867
<Compile Include="Concrete\EmptyForLoopBlockInspection.cs" />
@@ -74,6 +73,7 @@
7473
<Compile Include="ParseTreeListeners\AttributeAnnotationListener.cs" />
7574
<Compile Include="Concrete\ConstantNotUsedInspection.cs" />
7675
<Compile Include="Concrete\DefaultProjectNameInspection.cs" />
76+
<Compile Include="Concrete\EmptyIfBlockInspection.cs" />
7777
<Compile Include="Concrete\EmptyStringLiteralInspection.cs" />
7878
<Compile Include="Concrete\EncapsulatePublicFieldInspection.cs" />
7979
<Compile Include="Concrete\FunctionReturnValueNotUsedInspection.cs" />
@@ -90,6 +90,7 @@
9090
<Compile Include="Concrete\MissingAttributeInspection.cs" />
9191
<Compile Include="Abstract\InspectionResultBase.cs" />
9292
<Compile Include="Concrete\RedundantByRefModifierInspection.cs" />
93+
<Compile Include="Concrete\EmptyElseBlockInspection.cs" />
9394
<Compile Include="Inspector.cs" />
9495
<Compile Include="Concrete\MemberNotOnInterfaceInspection.cs" />
9596
<Compile Include="Concrete\MissingAnnotationArgumentInspection.cs" />
@@ -117,7 +118,6 @@
117118
<Compile Include="QuickFixes\AssignedByValParameterMakeLocalCopyQuickFix.cs" />
118119
<Compile Include="QuickFixes\ChangeDimToPrivateQuickFix.cs" />
119120
<Compile Include="QuickFixes\ChangeIntegerToLongQuickFix.cs" />
120-
<Compile Include="QuickFixes\RemoveEmptyConditionBlockQuickFix.cs" />
121121
<Compile Include="QuickFixes\RemoveStopKeywordQuickFix.cs" />
122122
<Compile Include="QuickFixes\SpecifyExplicitByRefModifierQuickFix.cs" />
123123
<Compile Include="QuickFixes\ChangeProcedureToFunctionQuickFix.cs" />
@@ -132,6 +132,8 @@
132132
<Compile Include="QuickFixes\PassParameterByReferenceQuickFix.cs" />
133133
<Compile Include="QuickFixes\PassParameterByValueQuickFix.cs" />
134134
<Compile Include="QuickFixes\RemoveCommentQuickFix.cs" />
135+
<Compile Include="QuickFixes\RemoveEmptyElseBlockQuickFix.cs" />
136+
<Compile Include="QuickFixes\RemoveEmptyIfBlockQuickFix.cs" />
135137
<Compile Include="QuickFixes\RemoveExplicitCallStatmentQuickFix.cs" />
136138
<Compile Include="QuickFixes\RemoveExplicitLetStatementQuickFix.cs" />
137139
<Compile Include="QuickFixes\RemoveOptionBaseStatementQuickFix.cs" />

0 commit comments

Comments
 (0)