|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Antlr4.Runtime; |
| 5 | +using Rubberduck.Inspections.Abstract; |
| 6 | +using Rubberduck.Inspections.Results; |
| 7 | +using Rubberduck.Parsing; |
| 8 | +using Rubberduck.Parsing.Grammar; |
| 9 | +using Rubberduck.Parsing.Inspections.Abstract; |
| 10 | +using Rubberduck.Parsing.Inspections.Resources; |
| 11 | +using Rubberduck.Parsing.Symbols; |
| 12 | +using Rubberduck.Parsing.VBA; |
| 13 | +using Rubberduck.VBEditor; |
| 14 | + |
| 15 | +namespace Rubberduck.Inspections.Concrete |
| 16 | +{ |
| 17 | + public sealed class BooleanAssignedInIfElseInspection : ParseTreeInspectionBase |
| 18 | + { |
| 19 | + public BooleanAssignedInIfElseInspection(RubberduckParserState state) |
| 20 | + : base(state) { } |
| 21 | + |
| 22 | + public override CodeInspectionType InspectionType => CodeInspectionType.MaintainabilityAndReadabilityIssues; |
| 23 | + |
| 24 | + public override IInspectionListener Listener { get; } = |
| 25 | + new BooleanAssignedInIfElseListener(); |
| 26 | + |
| 27 | + protected override IEnumerable<IInspectionResult> DoGetInspectionResults() |
| 28 | + { |
| 29 | + return Listener.Contexts |
| 30 | + .Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line)) |
| 31 | + .Select(result => new QualifiedContextInspectionResult(this, |
| 32 | + string.Format(InspectionsUI.BooleanAssignedInIfElseInspectionResultFormat, |
| 33 | + ParserRuleContextHelper.GetDescendent<VBAParser.LetStmtContext>(((VBAParser.IfStmtContext)result.Context).block()).lExpression().GetText().Trim()), |
| 34 | + result)); |
| 35 | + } |
| 36 | + |
| 37 | + public class BooleanAssignedInIfElseListener : VBAParserBaseListener, IInspectionListener |
| 38 | + { |
| 39 | + private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>(); |
| 40 | + public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts; |
| 41 | + |
| 42 | + public QualifiedModuleName CurrentModuleName { get; set; } |
| 43 | + |
| 44 | + public void ClearContexts() |
| 45 | + { |
| 46 | + _contexts.Clear(); |
| 47 | + } |
| 48 | + |
| 49 | + public override void ExitIfStmt(VBAParser.IfStmtContext context) |
| 50 | + { |
| 51 | + if (context.elseIfBlock().Any()) |
| 52 | + { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!IsSingleBooleanAssignment(context.block()) || |
| 57 | + !IsSingleBooleanAssignment(context.elseBlock().block())) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // make sure the assignments are the opposite |
| 63 | + if (!(ParserRuleContextHelper.GetDescendent<VBAParser.BooleanLiteralIdentifierContext>(context.block()).GetText() == Tokens.True ^ |
| 64 | + ParserRuleContextHelper.GetDescendent<VBAParser.BooleanLiteralIdentifierContext>(context.elseBlock().block()).GetText() == Tokens.True)) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (ParserRuleContextHelper.GetDescendent<VBAParser.LetStmtContext>(context.block()).lExpression().GetText().ToLowerInvariant() != |
| 70 | + ParserRuleContextHelper.GetDescendent<VBAParser.LetStmtContext>(context.elseBlock().block()).lExpression().GetText().ToLowerInvariant()) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + _contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context)); |
| 76 | + } |
| 77 | + |
| 78 | + private bool IsSingleBooleanAssignment(VBAParser.BlockContext block) |
| 79 | + { |
| 80 | + if (block.ChildCount != 2) |
| 81 | + { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + var mainBlockStmtContext = |
| 86 | + ParserRuleContextHelper.GetDescendent<VBAParser.MainBlockStmtContext>(block); |
| 87 | + |
| 88 | + return mainBlockStmtContext.children.FirstOrDefault() is VBAParser.LetStmtContext letStmt && |
| 89 | + letStmt.expression() is VBAParser.LiteralExprContext literal && |
| 90 | + ParserRuleContextHelper.GetDescendent<VBAParser.BooleanLiteralIdentifierContext>(literal) != null; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments