private void SaveUnhandledContext(VBAParser.OnErrorStmtContext context) { var module = CurrentModuleName; var qualifiedContext = new QualifiedContext <VBAParser.OnErrorStmtContext>(module, context); _unhandledContexts.Add(qualifiedContext); }
private static bool HasExpectedReturnType(QualifiedContext <ParserRuleContext> procedureContext) { var function = procedureContext.Context as VBAParser.FunctionStmtContext; var getter = procedureContext.Context as VBAParser.PropertyGetStmtContext; return(function != null || getter != null); }
protected virtual IInspectionResult InspectionResult(QualifiedContext <TContext> context) { return(new QualifiedContextInspectionResult( this, ResultDescription(context), context, DisabledQuickFixes(context))); }
private void CreateInspectionResult(QualifiedContext <ParserRuleContext> selectStmt, ParserRuleContext unreachableBlock, string message) { var result = new QualifiedContextInspectionResult(this, message, new QualifiedContext <ParserRuleContext>(selectStmt.ModuleName, unreachableBlock)); _inspectionResults.Add(result); }
protected override string ResultDescription(QualifiedContext <VBAParser.OptionBaseStmtContext> context) { var moduleName = context.ModuleName.ComponentName; return(string.Format( InspectionResults.OptionBaseInspection, moduleName)); }
protected override string ResultDescription(QualifiedContext <VBAParser.DeclareStmtContext> context) { var identifierName = ((VBAParser.DeclareStmtContext)context.Context).identifier().GetText(); return(string.Format( InspectionResults.ObsoleteCallingConventionInspection, identifierName)); }
private static bool ProcedureHasMinusOneLabel(DeclarationFinder finder, QualifiedContext <ParserRuleContext> context) { return(finder.Members(context.ModuleName, DeclarationType.LineLabel) .Any(label => label.IdentifierName.Equals("-1") && (label.ParentScopeDeclaration .Context?.GetSelection() .Contains(context.Context.GetSelection()) ?? false))); }
public EmptyStringLiteralInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext) : base(inspection, qualifiedContext.ModuleName, qualifiedContext.Context) { _quickFixes = new[] { new RepaceEmptyStringLiteralStatementQuickFix(Context, QualifiedSelection), }; }
protected override string ResultDescription(QualifiedContext <VBAParser.VariableSubStmtContext> context) { var identifierName = context.Context.identifier().GetText(); return(string.Format( InspectionResults.ModuleScopeDimKeywordInspection, identifierName)); }
public ObsoleteGlobalInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> context) : base(inspection, context.ModuleName, context.Context) { _quickFixes = new CodeInspectionQuickFix[] { new ReplaceGlobalModifierQuickFix(Context, QualifiedSelection), new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName), }; }
private IInspectionResult InspectionResult(IParseTreeAnnotation pta) { var qualifiedContext = new QualifiedContext(pta.QualifiedSelection.QualifiedName, pta.Context); return(new QualifiedContextInspectionResult( this, ResultDescription(pta), qualifiedContext)); }
public ImplicitPublicMemberInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext, Declaration item) : base(inspection, item) { _quickFixes = new CodeInspectionQuickFix[] { new SpecifyExplicitPublicModifierQuickFix(qualifiedContext.Context, QualifiedSelection), new IgnoreOnceQuickFix(qualifiedContext.Context, QualifiedSelection, Inspection.AnnotationName), }; }
public EmptyStringLiteralInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext) : base(inspection, qualifiedContext.ModuleName, qualifiedContext.Context) { _quickFixes = new CodeInspectionQuickFix[] { new RepaceEmptyStringLiteralStatementQuickFix(Context, QualifiedSelection), new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName) }; }
public MultipleDeclarationsInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext) : base(inspection, qualifiedContext.ModuleName, qualifiedContext.Context) { _quickFixes = new CodeInspectionQuickFix[] { new SplitMultipleDeclarationsQuickFix(Context, QualifiedSelection), new IgnoreOnceQuickFix(qualifiedContext.Context, QualifiedSelection, Inspection.AnnotationName), }; }
public ObsoleteLetStatementUsageInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext) : base(inspection, qualifiedContext.ModuleName, qualifiedContext.Context) { _quickFixes = new CodeInspectionQuickFix[] { new RemoveExplicitLetStatementQuickFix(Context, QualifiedSelection), new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName), }; }
protected override string ResultDescription(QualifiedContext <VBAParser.ArgContext> context) { var parameterText = context.Context.unrestrictedIdentifier().GetText(); return(string.Format( context.Context.GetSelection().LineCount > 3 ? RubberduckUI.EasterEgg_Continuator : Resources.Inspections.InspectionResults.MultilineParameterInspection, parameterText)); }
protected override string ResultDescription(QualifiedContext <VBAParser.DefTypeContext> context) { var typeName = GetTypeOfDefType(context.Context.start.Text); var defStmtText = context.Context.start.Text; return(string.Format( InspectionResults.DefTypeStatementInspection, typeName, defStmtText)); }
public ProcedureCanBeWrittenAsFunctionInspectionResult(IInspection inspection, RubberduckParserState state, QualifiedContext <VBAParser.ArgListContext> argListQualifiedContext, QualifiedContext <VBAParser.SubStmtContext> subStmtQualifiedContext) : base(inspection, subStmtQualifiedContext.ModuleName, subStmtQualifiedContext.Context.subroutineName()) { _target = state.AllUserDeclarations.Single(declaration => declaration.DeclarationType == DeclarationType.Procedure && declaration.Context == subStmtQualifiedContext.Context); _argListQualifiedContext = argListQualifiedContext; _subStmtQualifiedContext = subStmtQualifiedContext; _state = state; }
private IEnumerable <IInspectionResult> ResultsForContext(QualifiedContext <VBAParser.SelectCaseStmtContext> qualifiedSelectCaseStmt, DeclarationFinder finder) { var module = qualifiedSelectCaseStmt.ModuleName; var selectStmt = qualifiedSelectCaseStmt.Context; var contextValues = _parseTreeValueVisitor.VisitChildren(module, selectStmt, finder); var results = _inspector.InspectForUnreachableCases(module, selectStmt, contextValues, finder); return(results .Select(resultTpl => CreateInspectionResult(qualifiedSelectCaseStmt, resultTpl.context, resultTpl.resultType)) .ToList()); }
public void AddAnnotation(IRewriteSession rewriteSession, QualifiedContext context, AnnotationType annotationType, IReadOnlyList <string> values = null) { var annotationValues = values ?? new List <string>(); if (context == null) { _logger.Warn("Tried to add an annotation to a context that is null."); _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to a context that is null."); return; } if (rewriteSession.TargetCodeKind != CodeKind.CodePaneCode) { _logger.Warn($"Tried to add an annotation with a rewriter not suitable for annotationss. (target code kind = {rewriteSession.TargetCodeKind})"); _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to {context.Context.GetText()} at {context.Context.GetSelection()} in module {context.ModuleName} using a rewriter not suitable for annotations."); return; } var annotationText = AnnotationText(annotationType, annotationValues); string codeToAdd; IModuleRewriter rewriter; if (context.Context.start.Line == 1) { codeToAdd = $"{annotationText}{Environment.NewLine}"; rewriter = rewriteSession.CheckOutModuleRewriter(context.ModuleName); rewriter.InsertBefore(0, codeToAdd); return; } var previousEndOfLine = PreviousEndOfLine(context.Context); if (previousEndOfLine == null) { //We are on the first logical line, but not the first physical line. return; } if (context.Context.start.Line > previousEndOfLine.stop.Line + 1) { _logger.Warn("Tried to add an annotation to a context not on the first physical line of a logical line."); _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to a the context with text '{context.Context.GetText()}' at {context.Context.GetSelection()} in module {context.ModuleName}."); return; } codeToAdd = previousEndOfLine.TryGetFollowingContext(out VBAParser.WhiteSpaceContext whitespaceAtStartOfLine) ? $"{whitespaceAtStartOfLine.GetText()}{annotationText}{Environment.NewLine}" : $"{annotationText}{Environment.NewLine}"; rewriter = rewriteSession.CheckOutModuleRewriter(context.ModuleName); rewriter.InsertAfter(previousEndOfLine.stop.TokenIndex, codeToAdd); }
public ChangeProcedureToFunction(RubberduckParserState state, QualifiedContext <VBAParser.ArgListContext> argListQualifiedContext, QualifiedContext <VBAParser.SubStmtContext> subStmtQualifiedContext, QualifiedSelection selection) : base(subStmtQualifiedContext.Context, selection, InspectionsUI.ProcedureShouldBeFunctionInspectionQuickFix) { _state = state; _argListQualifiedContext = argListQualifiedContext; _subStmtQualifiedContext = subStmtQualifiedContext; _argQualifiedContext = new QualifiedContext <VBAParser.ArgContext>(_argListQualifiedContext.ModuleName, _argListQualifiedContext.Context.arg() .First(a => a.BYREF() != null || (a.BYREF() == null && a.BYVAL() == null))); }
protected override string ResultDescription(QualifiedContext <VBAParser.IfStmtContext> context) { var literalText = context.Context .block() .GetDescendent <VBAParser.LetStmtContext>() .lExpression() .GetText() .Trim(); return(string.Format( InspectionResults.BooleanAssignedInIfElseInspection, literalText)); }
public NonReturningFunctionInspectionResult(IInspection inspection, QualifiedContext <ParserRuleContext> qualifiedContext, bool isInterfaceImplementation, Declaration target) : base(inspection, qualifiedContext.ModuleName, qualifiedContext.Context, target) { _quickFixes = isInterfaceImplementation ? new CodeInspectionQuickFix[] { } : new CodeInspectionQuickFix[] { new ConvertToProcedureQuickFix(Context, QualifiedSelection), new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName), }; }
public QualifiedContextInspectionResult( IInspection inspection, string description, QualifiedContext context, ICollection <string> disabledQuickFixes = null) : base(inspection, description, context.ModuleName, context.Context, null, new QualifiedSelection(context.ModuleName, context.Context.GetSelection()), context.MemberName, disabledQuickFixes) { }
protected override bool IsResultContext(QualifiedContext <VBAParser.CallStmtContext> context, DeclarationFinder finder) { if (!context.Context.TryGetFollowingContext(out VBAParser.IndividualNonEOFEndOfStatementContext followingEndOfStatement) || followingEndOfStatement.COLON() == null) { return(true); } if (!context.Context.TryGetPrecedingContext(out VBAParser.IndividualNonEOFEndOfStatementContext precedingEndOfStatement) || precedingEndOfStatement.endOfLine() == null) { return(true); } return(false); }
public DeclarationInspectionResult( IInspection inspection, string description, Declaration target, QualifiedContext context = null, ICollection <string> disabledQuickFixes = null) : base(inspection, description, context == null ? target.QualifiedName.QualifiedModuleName : context.ModuleName, context == null ? target.Context : context.Context, target, target.QualifiedSelection, GetQualifiedMemberName(target), disabledQuickFixes) { }
public void DeclarationInspectionResultsAreDeemedInvalidatedIfTheirTargetsModuleGetsModified() { var inspectionMock = new Mock <IInspection>(); inspectionMock .Setup(m => m.ChangesInvalidateResult(It.IsAny <IInspectionResult>(), It.IsAny <ICollection <QualifiedModuleName> >())) .Returns(false); var module = new QualifiedModuleName("project", string.Empty, "module"); var declarationModule = new QualifiedModuleName("project", string.Empty, "declarationModule"); var declarationMemberName = new QualifiedMemberName(declarationModule, "test"); var context = new QualifiedContext(module, null); var declaration = new Declaration(declarationMemberName, null, string.Empty, string.Empty, string.Empty, false, false, Accessibility.Public, DeclarationType.Constant, null, null, default, false, null);
protected void SaveContext(TContext context) { var module = CurrentModuleName; var qualifiedContext = new QualifiedContext <TContext>(module, context); if (_contexts.TryGetValue(module, out var contexts)) { contexts.Add(qualifiedContext); } else { _contexts.Add(module, new List <QualifiedContext <TContext> > { qualifiedContext }); } }
public void InspectionResultsAreDeemedInvalidatedIfTheInspectionDeemsThemInvalidated() { var inspectionMock = new Mock <IInspection>(); inspectionMock .Setup(m => m.ChangesInvalidateResult(It.IsAny <IInspectionResult>(), It.IsAny <ICollection <QualifiedModuleName> >())) .Returns(true); var module = new QualifiedModuleName("project", string.Empty, "module"); var context = new QualifiedContext(module, null); var modifiedModules = new HashSet <QualifiedModuleName>(); var inspectionResult = new QualifiedContextInspectionResult(inspectionMock.Object, string.Empty, context); Assert.IsTrue(inspectionResult.ChangesInvalidateResult(modifiedModules)); }
public void AddAnnotation(IRewriteSession rewriteSession, QualifiedContext context, IAnnotation annotationInfo, IReadOnlyList <string> values = null) { var annotationValues = values ?? new List <string>(); if (context == null) { _logger.Warn("Tried to add an annotation to a context that is null."); _logger.Trace($"Tried to add annotation {annotationInfo.Name} with values {AnnotationValuesText(annotationValues)} to a context that is null."); return; } if (rewriteSession.TargetCodeKind != CodeKind.CodePaneCode) { _logger.Warn($"Tried to add an annotation with a rewriter not suitable to annotate contexts. (target code kind = {rewriteSession.TargetCodeKind})"); _logger.Trace($"Tried to add annotation {annotationInfo.Name} with values {AnnotationValuesText(annotationValues)} to {context.Context.GetText()} at {context.Context.GetSelection()} in module {context.ModuleName} using a rewriter not suitable for annotations."); return; } AddAnnotation(rewriteSession, context.ModuleName, context.Context, annotationInfo, annotationValues); }