Skip to content

Commit 0a517ae

Browse files
committed
Make empty block inspections derive from the same base providing the listener base
1 parent af2b026 commit 0a517ae

9 files changed

+99
-92
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
3+
using Antlr4.Runtime;
4+
using Antlr4.Runtime.Tree;
5+
using Rubberduck.Parsing.Grammar;
6+
using Rubberduck.Parsing.VBA;
7+
8+
namespace Rubberduck.Inspections.Abstract
9+
{
10+
public abstract class EmptyBlockInspectionBase<TContext> : ParseTreeInspectionBase<TContext>
11+
where TContext : ParserRuleContext
12+
{
13+
protected EmptyBlockInspectionBase(IDeclarationFinderProvider declarationFinderProvider)
14+
: base(declarationFinderProvider)
15+
{}
16+
17+
protected class EmptyBlockInspectionListenerBase : InspectionListenerBase<TContext>
18+
{
19+
public void InspectBlockForExecutableStatements<T>(VBAParser.BlockContext block, T context) where T : TContext
20+
{
21+
if (!BlockContainsExecutableStatements(block))
22+
{
23+
SaveContext(context);
24+
}
25+
}
26+
27+
private bool BlockContainsExecutableStatements(VBAParser.BlockContext block)
28+
{
29+
return block?.children != null && ContainsExecutableStatements(block.children);
30+
}
31+
32+
private bool ContainsExecutableStatements(IList<IParseTree> blockChildren)
33+
{
34+
foreach (var child in blockChildren)
35+
{
36+
if (child is VBAParser.BlockStmtContext blockStmt)
37+
{
38+
var mainBlockStmt = blockStmt.mainBlockStmt();
39+
40+
if (mainBlockStmt == null)
41+
{
42+
continue; //We have a lone line label, which is not executable.
43+
}
44+
45+
Debug.Assert(mainBlockStmt.ChildCount == 1);
46+
47+
// exclude variables and consts because they are not executable statements
48+
if (mainBlockStmt.GetChild(0) is VBAParser.VariableStmtContext ||
49+
mainBlockStmt.GetChild(0) is VBAParser.ConstStmtContext)
50+
{
51+
continue;
52+
}
53+
54+
return true;
55+
}
56+
57+
if (child is VBAParser.RemCommentContext ||
58+
child is VBAParser.CommentContext ||
59+
child is VBAParser.CommentOrAnnotationContext ||
60+
child is VBAParser.EndOfStatementContext)
61+
{
62+
continue;
63+
}
64+
65+
return true;
66+
}
67+
68+
return false;
69+
}
70+
71+
public void InspectBlockForExecutableStatements<T>(VBAParser.UnterminatedBlockContext block, T context) where T : TContext
72+
{
73+
if (!BlockContainsExecutableStatements(block))
74+
{
75+
SaveContext(context);
76+
}
77+
}
78+
79+
private bool BlockContainsExecutableStatements(VBAParser.UnterminatedBlockContext block)
80+
{
81+
return block?.children != null && ContainsExecutableStatements(block.children);
82+
}
83+
}
84+
}
85+
}

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyCaseBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyCaseBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Rubberduck.Inspections.Concrete
4040
/// ]]>
4141
/// </example>
4242
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
43-
internal sealed class EmptyCaseBlockInspection : ParseTreeInspectionBase<VBAParser.CaseClauseContext>
43+
internal sealed class EmptyCaseBlockInspection : EmptyBlockInspectionBase<VBAParser.CaseClauseContext>
4444
{
4545
public EmptyCaseBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4646
: base(declarationFinderProvider)
@@ -55,7 +55,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.CaseClaus
5555
return InspectionResults.EmptyCaseBlockInspection;
5656
}
5757

58-
private class EmptyCaseBlockListener : EmptyBlockInspectionListenerBase<VBAParser.CaseClauseContext>
58+
private class EmptyCaseBlockListener : EmptyBlockInspectionListenerBase
5959
{
6060
public override void EnterCaseClause([NotNull] VBAParser.CaseClauseContext context)
6161
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyDoWhileBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyDoWhileBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Rubberduck.Inspections.Concrete
3535
/// ]]>
3636
/// </example>
3737
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
38-
internal sealed class EmptyDoWhileBlockInspection : ParseTreeInspectionBase<VBAParser.DoLoopStmtContext>
38+
internal sealed class EmptyDoWhileBlockInspection : EmptyBlockInspectionBase<VBAParser.DoLoopStmtContext>
3939
{
4040
public EmptyDoWhileBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4141
: base(declarationFinderProvider)
@@ -50,7 +50,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.DoLoopStm
5050
return InspectionResults.EmptyDoWhileBlockInspection;
5151
}
5252

53-
private class EmptyDoWhileBlockListener : EmptyBlockInspectionListenerBase<VBAParser.DoLoopStmtContext>
53+
private class EmptyDoWhileBlockListener : EmptyBlockInspectionListenerBase
5454
{
5555
public override void EnterDoLoopStmt([NotNull] VBAParser.DoLoopStmtContext context)
5656
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyElseBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyElseBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Rubberduck.Inspections.Concrete
3737
/// ]]>
3838
/// </example>
3939
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
40-
internal sealed class EmptyElseBlockInspection : ParseTreeInspectionBase<VBAParser.ElseBlockContext>
40+
internal sealed class EmptyElseBlockInspection : EmptyBlockInspectionBase<VBAParser.ElseBlockContext>
4141
{
4242
public EmptyElseBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4343
: base(declarationFinderProvider)
@@ -52,7 +52,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.ElseBlock
5252
return InspectionResults.EmptyElseBlockInspection;
5353
}
5454

55-
private class EmptyElseBlockListener : EmptyBlockInspectionListenerBase<VBAParser.ElseBlockContext>
55+
private class EmptyElseBlockListener : EmptyBlockInspectionListenerBase
5656
{
5757
public override void EnterElseBlock([NotNull] VBAParser.ElseBlockContext context)
5858
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyForEachBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyForEachBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Rubberduck.Inspections.Concrete
3737
/// ]]>
3838
/// </example>
3939
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
40-
internal sealed class EmptyForEachBlockInspection : ParseTreeInspectionBase<VBAParser.ForEachStmtContext>
40+
internal sealed class EmptyForEachBlockInspection : EmptyBlockInspectionBase<VBAParser.ForEachStmtContext>
4141
{
4242
public EmptyForEachBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4343
: base(declarationFinderProvider)
@@ -52,7 +52,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.ForEachSt
5252
return InspectionResults.EmptyForEachBlockInspection;
5353
}
5454

55-
private class EmptyForEachBlockListener : EmptyBlockInspectionListenerBase<VBAParser.ForEachStmtContext>
55+
private class EmptyForEachBlockListener : EmptyBlockInspectionListenerBase
5656
{
5757
public override void EnterForEachStmt([NotNull] VBAParser.ForEachStmtContext context)
5858
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyForLoopBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyForLoopBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Rubberduck.Inspections.Concrete
3737
/// ]]>
3838
/// </example>
3939
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
40-
internal sealed class EmptyForLoopBlockInspection : ParseTreeInspectionBase<VBAParser.ForNextStmtContext>
40+
internal sealed class EmptyForLoopBlockInspection : EmptyBlockInspectionBase<VBAParser.ForNextStmtContext>
4141
{
4242
public EmptyForLoopBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4343
: base(declarationFinderProvider)
@@ -52,7 +52,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.ForNextSt
5252
return InspectionResults.EmptyForLoopBlockInspection;
5353
}
5454

55-
private class EmptyForLoopBlockListener : EmptyBlockInspectionListenerBase<VBAParser.ForNextStmtContext>
55+
private class EmptyForLoopBlockListener : EmptyBlockInspectionListenerBase
5656
{
5757
public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context)
5858
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyIfBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyIfBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Rubberduck.Inspections.Concrete
3434
/// End Sub
3535
/// ]]>
3636
/// </example>
37-
internal sealed class EmptyIfBlockInspection : ParseTreeInspectionBase<ParserRuleContext>
37+
internal sealed class EmptyIfBlockInspection : EmptyBlockInspectionBase<ParserRuleContext>
3838
{
3939
public EmptyIfBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4040
: base(declarationFinderProvider)
@@ -49,7 +49,7 @@ protected override string ResultDescription(QualifiedContext<ParserRuleContext>
4949

5050
protected override IInspectionListener<ParserRuleContext> ContextListener { get; }
5151

52-
private class EmptyIfBlockListener : EmptyBlockInspectionListenerBase<ParserRuleContext>
52+
private class EmptyIfBlockListener : EmptyBlockInspectionListenerBase
5353
{
5454
public override void EnterIfStmt([NotNull] VBAParser.IfStmtContext context)
5555
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyWhileWendBlockInspection.cs renamed to Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlock/EmptyWhileWendBlockInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Rubberduck.Inspections.Concrete
3535
/// ]]>
3636
/// </example>
3737
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
38-
internal sealed class EmptyWhileWendBlockInspection : ParseTreeInspectionBase<VBAParser.WhileWendStmtContext>
38+
internal sealed class EmptyWhileWendBlockInspection : EmptyBlockInspectionBase<VBAParser.WhileWendStmtContext>
3939
{
4040
public EmptyWhileWendBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
4141
: base(declarationFinderProvider)
@@ -50,7 +50,7 @@ protected override string ResultDescription(QualifiedContext<VBAParser.WhileWend
5050
return InspectionResults.EmptyWhileWendBlockInspection;
5151
}
5252

53-
private class EmptyWhileWendBlockListener : EmptyBlockInspectionListenerBase<VBAParser.WhileWendStmtContext>
53+
private class EmptyWhileWendBlockListener : EmptyBlockInspectionListenerBase
5454
{
5555
public override void EnterWhileWendStmt([NotNull] VBAParser.WhileWendStmtContext context)
5656
{

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlockInspectionListenerBase.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)