Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Rubberduck.Inspections/Concrete/EmptyConditionBlockInspection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Inspections.Resources;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing;
using Rubberduck.Parsing.VBA;
using Rubberduck.Inspections.Results;

namespace Rubberduck.Inspections.Concrete
{
[Flags]
public enum ConditionBlockToInspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ConditionBlockTypeToInspect. The current name suggests an actual code block.

{
NA = 0x0,
If = 0x1,
ElseIf = 0x2,
Else = 0x4,
All = If | ElseIf | Else
}

internal class EmptyConditionBlockInspection : ParseTreeInspectionBase
{
public EmptyConditionBlockInspection(RubberduckParserState state,
ConditionBlockToInspect BlockToInspect)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ninject has no idea how to create the inspection instance, ConditionBlockToInspect doesn't belong as a constructor parameter. The inspection needs to inspect them all anyway.

: base(state, CodeInspectionSeverity.Suggestion)
{
_blockToInspect = BlockToInspect;
_listener = new EmptyConditionBlockListener(BlockToInspect);
}

public static ConditionBlockToInspect _blockToInspect { get; private set; }

public override Type Type => typeof(EmptyConditionBlockInspection);

public override IEnumerable<IInspectionResult> GetInspectionResults()
{
return Listener.Contexts
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
.Select(result => new QualifiedContextInspectionResult(this,
InspectionsUI.EmptyConditionBlockInspectionsResultFormat,
result));
}

private IInspectionListener _listener;
public override IInspectionListener Listener { get { return _listener; } }

public class EmptyConditionBlockListener : EmptyBlockInspectionListenerBase
{
ConditionBlockToInspect _blockToInspect;

public EmptyConditionBlockListener(ConditionBlockToInspect blockToInspect)
{
_blockToInspect = blockToInspect;
}

public override void EnterIfStmt([NotNull] VBAParser.IfStmtContext context)
{
if (_blockToInspect.HasFlag(ConditionBlockToInspect.If))
{
InspectBlockForExecutableStatements(context.block(), context);
}
}

public override void EnterElseIfBlock([NotNull] VBAParser.ElseIfBlockContext context)
{
if (_blockToInspect.HasFlag(ConditionBlockToInspect.ElseIf))
{
InspectBlockForExecutableStatements(context.block(), context);
}
}

public override void EnterSingleLineIfStmt([NotNull] VBAParser.SingleLineIfStmtContext context)
{
if (context.ifWithEmptyThen() != null & _blockToInspect.HasFlag(ConditionBlockToInspect.If))
{
AddResult(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context.ifWithEmptyThen()));
}
}

public override void EnterElseBlock([NotNull] VBAParser.ElseBlockContext context)
{
if (_blockToInspect.HasFlag(ConditionBlockToInspect.Else))
{
InspectBlockForExecutableStatements(context.block(), context);
}
}
}
}
}
43 changes: 0 additions & 43 deletions Rubberduck.Inspections/Concrete/EmptyElseBlockInspection.cs

This file was deleted.

58 changes: 0 additions & 58 deletions Rubberduck.Inspections/Concrete/EmptyIfBlockInspection.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.Inspections.Concrete;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Inspections.Resources;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.VBA;
using Antlr4.Runtime;
using Rubberduck.Parsing.Inspections.Resources;
using System.Diagnostics;

namespace Rubberduck.Inspections.QuickFixes
{
internal sealed class RemoveEmptyIfBlockQuickFix : IQuickFix
internal sealed class RemoveEmptyConditionBlockQuickFix : IQuickFix
{
private static readonly HashSet<Type> _supportedInspections = new HashSet<Type> { typeof(EmptyIfBlockInspection) };
private static readonly HashSet<Type> _supportedInspections = new HashSet<Type> { typeof(EmptyConditionBlockInspection) };
private readonly RubberduckParserState _state;

public RemoveEmptyIfBlockQuickFix(RubberduckParserState state)
public RemoveEmptyConditionBlockQuickFix(RubberduckParserState state)
{
_state = state;
}
Expand Down Expand Up @@ -92,6 +92,16 @@ private void UpdateContext(VBAParser.ElseIfBlockContext context, IModuleRewriter
rewriter.Remove(context);
}

private void UpdateContext(VBAParser.ElseBlockContext context, IModuleRewriter rewriter)
{
var elseBlock = context.block();

if (elseBlock.ChildCount == 0)
{
rewriter.Remove(context);
}
}

private void UpdateCondition(VBAParser.RelationalOpContext condition, IModuleRewriter rewriter)
{
if (condition.EQ() != null)
Expand Down Expand Up @@ -178,7 +188,7 @@ private bool FirstBlockStmntHasLabel(VBAParser.BlockContext block)

public string Description(IInspectionResult result)
{
return InspectionsUI.RemoveEmptyIfBlockQuickFix;
return InspectionsUI.RemoveEmptyConditionBlockQuickFix;
}

public bool CanFixInProcedure { get; } = false;
Expand Down
71 changes: 0 additions & 71 deletions Rubberduck.Inspections/QuickFixes/RemoveEmptyElseBlockQuickFix.cs

This file was deleted.

6 changes: 2 additions & 4 deletions Rubberduck.Inspections/Rubberduck.Inspections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Concrete\AssignedByValParameterInspection.cs" />
<Compile Include="Concrete\EmptyBlockInspectionListenerBase.cs" />
<Compile Include="Concrete\EmptyCaseBlockInspection.cs" />
<Compile Include="Concrete\EmptyConditionBlockInspection.cs" />
<Compile Include="Concrete\EmptyDoWhileBlockInspection.cs" />
<Compile Include="Concrete\EmptyForEachBlockInspection.cs" />
<Compile Include="Concrete\EmptyForLoopBlockInspection.cs" />
Expand All @@ -73,7 +74,6 @@
<Compile Include="ParseTreeListeners\AttributeAnnotationListener.cs" />
<Compile Include="Concrete\ConstantNotUsedInspection.cs" />
<Compile Include="Concrete\DefaultProjectNameInspection.cs" />
<Compile Include="Concrete\EmptyIfBlockInspection.cs" />
<Compile Include="Concrete\EmptyStringLiteralInspection.cs" />
<Compile Include="Concrete\EncapsulatePublicFieldInspection.cs" />
<Compile Include="Concrete\FunctionReturnValueNotUsedInspection.cs" />
Expand All @@ -90,7 +90,6 @@
<Compile Include="Concrete\MissingAttributeInspection.cs" />
<Compile Include="Abstract\InspectionResultBase.cs" />
<Compile Include="Concrete\RedundantByRefModifierInspection.cs" />
<Compile Include="Concrete\EmptyElseBlockInspection.cs" />
<Compile Include="Inspector.cs" />
<Compile Include="Concrete\MemberNotOnInterfaceInspection.cs" />
<Compile Include="Concrete\MissingAnnotationArgumentInspection.cs" />
Expand Down Expand Up @@ -118,6 +117,7 @@
<Compile Include="QuickFixes\AssignedByValParameterMakeLocalCopyQuickFix.cs" />
<Compile Include="QuickFixes\ChangeDimToPrivateQuickFix.cs" />
<Compile Include="QuickFixes\ChangeIntegerToLongQuickFix.cs" />
<Compile Include="QuickFixes\RemoveEmptyConditionBlockQuickFix.cs" />
<Compile Include="QuickFixes\RemoveStopKeywordQuickFix.cs" />
<Compile Include="QuickFixes\SpecifyExplicitByRefModifierQuickFix.cs" />
<Compile Include="QuickFixes\ChangeProcedureToFunctionQuickFix.cs" />
Expand All @@ -132,8 +132,6 @@
<Compile Include="QuickFixes\PassParameterByReferenceQuickFix.cs" />
<Compile Include="QuickFixes\PassParameterByValueQuickFix.cs" />
<Compile Include="QuickFixes\RemoveCommentQuickFix.cs" />
<Compile Include="QuickFixes\RemoveEmptyElseBlockQuickFix.cs" />
<Compile Include="QuickFixes\RemoveEmptyIfBlockQuickFix.cs" />
<Compile Include="QuickFixes\RemoveExplicitCallStatmentQuickFix.cs" />
<Compile Include="QuickFixes\RemoveExplicitLetStatementQuickFix.cs" />
<Compile Include="QuickFixes\RemoveOptionBaseStatementQuickFix.cs" />
Expand Down
Loading