private class ExtractMethodValidatorVisitor : VBAParserBaseVisitor<ExtractMethodValidationResult<VBAParser.BlockStmtContext>BlockStmtContext>>
{
private readonly QualifiedSelection _qualifiedSelection;
public ExtractMethodValidatorVisitor(QualifiedSelection qualifiedSelection)
{
_qualifiedSelection = qualifiedSelection;
}
protected override ExtractMethodValidationResult<VBAParser.BlockStmtContext> DefaultResult => new ExtractMethodValidationResult<VBAParser.BlockStmtContext>();
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitBlockStmt([NotNull] VBAParser.BlockStmtContext context)
{
var children = base.VisitBlockStmt(context);
if (children.InvalidContexts.Count == 0)
{
children.ValidContexts.Add(context);
}
return children;
}
protected override ExtractMethodValidationResult<VBAParser.BlockStmtContext> AggregateResult(ExtractMethodValidationResult<VBAParser.BlockStmtContext> aggregate, ExtractMethodValidationResult<VBAParser.BlockStmtContext> nextResult)
{
if (aggregate.InvalidContexts.Count == 0)
{
if (nextResult != null)
{
aggregate.ValidContexts = aggregate.ValidContexts.Concat(nextResult.ValidContexts).ToList();
aggregate.InvalidContexts = aggregate.InvalidContexts.Concat(nextResult.InvalidContexts).ToList();
}
}
return aggregate;
}
protected override bool ShouldVisitNextChild(IRuleNode node, ExtractMethodValidationResult<VBAParser.BlockStmtContext> currentResult)
{
// Don't visit any more children if we have any invalid contexts
return (currentResult.InvalidContexts.Count == 0);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitErrorStmt([NotNull] VBAParser.ErrorStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains an Error statement.");
}
return base.VisitErrorStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitEndStmt([NotNull] VBAParser.EndStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains an End statement.");
}
return base.VisitEndStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitExitStmt([NotNull] VBAParser.ExitStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains an Exit statement");
}
return base.VisitExitStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitGoSubStmt([NotNull] VBAParser.GoSubStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a GoSub statement");
}
return base.VisitGoSubStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitGoToStmt([NotNull] VBAParser.GoToStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a GoTo statement");
}
return base.VisitGoToStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitOnErrorStmt([NotNull] VBAParser.OnErrorStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a On Error statement");
}
return base.VisitOnErrorStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitIdentifierStatementLabel([NotNull] VBAParser.IdentifierStatementLabelContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a Line Label statement");
}
return base.VisitIdentifierStatementLabel(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitCombinedLabels([NotNull] VBAParser.CombinedLabelsContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a Line Label statement");
}
return base.VisitCombinedLabels(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitOnGoSubStmt([NotNull] VBAParser.OnGoSubStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a On ... GoSub statement");
}
return base.VisitOnGoSubStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitOnGoToStmt([NotNull] VBAParser.OnGoToStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a On ... GoTo statement");
}
return base.VisitOnGoToStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitResumeStmt([NotNull] VBAParser.ResumeStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a Resume statement");
}
return base.VisitResumeStmt(context);
}
public override ExtractMethodValidationResult<VBAParser.BlockStmtContext> VisitReturnStmt([NotNull] VBAParser.ReturnStmtContext context)
{
if (_qualifiedSelection.Selection.Contains(context))
{
return new ExtractMethodValidationResult<VBAParser.BlockStmtContext>(context, "Extract method cannot extract methods that contains a Return statement");
}
return base.VisitReturnStmt(context);
}
}