public override void ExitModuleBodyElement(VBAParser.ModuleBodyElementContext context) { if (_unhandledContexts.Any()) { var labelIndex = -1; foreach (var errorContext in _unhandledContexts) { _bodyElementContextsMap.Add(errorContext, context); labelIndex++; var labelSuffix = labelIndex == 0 ? "" : labelIndex.ToString(); while (_errorHandlerLabels.Contains($"{LabelPrefix.ToLower()}{labelSuffix}")) { labelIndex++; labelSuffix = labelIndex == 0 ? "" : labelIndex.ToString(); } _errorHandlerLabelsMap.Add(errorContext, $"{LabelPrefix}{labelSuffix}"); } _contexts.AddRange(_unhandledContexts); _unhandledContexts.Clear(); _errorHandlerLabels.Clear(); } }
public override void ExitModuleBodyElement(VBAParser.ModuleBodyElementContext context) { if (_unhandledContexts.Any()) { foreach (var errorContext in _unhandledContexts) { _unhandledContextsMap.Add(errorContext, new List <VBAParser.OnErrorStmtContext>(_unhandledContexts.Select(ctx => ctx.Context))); SaveContext(errorContext.Context); } _unhandledContexts.Clear(); } }
public override void Fix(IInspectionResult result) { var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName); var context = (VBAParser.OnErrorStmtContext)result.Context; rewriter.Replace(context.RESUME(), Tokens.GoTo); rewriter.Replace(context.NEXT(), result.Properties.Label); var exitStatement = "Exit "; VBAParser.BlockContext block; VBAParser.ModuleBodyElementContext bodyElementContext = result.Properties.BodyElement; if (bodyElementContext.propertyGetStmt() != null) { exitStatement += "Property"; block = bodyElementContext.propertyGetStmt().block(); } else if (bodyElementContext.propertyLetStmt() != null) { exitStatement += "Property"; block = bodyElementContext.propertyLetStmt().block(); } else if (bodyElementContext.propertySetStmt() != null) { exitStatement += "Property"; block = bodyElementContext.propertySetStmt().block(); } else if (bodyElementContext.functionStmt() != null) { exitStatement += "Function"; block = bodyElementContext.functionStmt().block(); } else { exitStatement += "Sub"; block = bodyElementContext.subStmt().block(); } var errorHandlerSubroutine = $@" {exitStatement} {result.Properties.Label}: If Err.Number > 0 Then 'TODO: handle specific error Err.Clear Resume Next End If "; rewriter.InsertAfter(block.Stop.TokenIndex, errorHandlerSubroutine); }