private Declaration FindSelectedDeclaration(out VBProjectParseResult results)
        {
            var project = _vbe.ActiveVBProject;
            if (project == null)
            {
                results = null;
                return null;
            }

            var progress = new ParsingProgressPresenter();
            results = progress.Parse(_parser, _vbe.ActiveVBProject);

            var selection = _vbe.SelectedVBComponent;
            if (selection != null)
            {
                var componentName = selection.Name;
                var matches = results.Declarations[componentName].ToList();
                if (matches.Count == 1)
                {
                    return matches.Single();
                }

                var result = matches.SingleOrDefault(item =>
                    (item.DeclarationType == DeclarationType.Class || item.DeclarationType == DeclarationType.Module)
                    && item.Project == project);

                return result;
            }

            return results.Declarations[project.Name].SingleOrDefault(item =>
                item.DeclarationType == DeclarationType.Project && item.Project == project);
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var classes = parseResult.Declarations.Items.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Class).ToList();
            var modules = parseResult.Declarations.Items.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Module).ToList();

            var handlers = parseResult.Declarations.Items.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Control)
                .SelectMany(control => parseResult.Declarations.FindEventHandlers(control)).ToList();

            var withEventFields = parseResult.Declarations.Items.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Variable && item.IsWithEvents);
            handlers.AddRange(withEventFields.SelectMany(field => parseResult.Declarations.FindEventProcedures(field)));

            var forms = parseResult.Declarations.Items.Where(
                item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Class
                        && item.QualifiedName.QualifiedModuleName.Component.Type == vbext_ComponentType.vbext_ct_MSForm)
                .ToList();

            if (forms.Any())
            {
                handlers.AddRange(forms.SelectMany(form => parseResult.Declarations.FindFormEventHandlers(form)));
            }

            var issues = parseResult.Declarations.Items
                .Where(item => !item.IsBuiltIn && !IsIgnoredDeclaration(parseResult.Declarations, item, handlers, classes, modules))
                .Select(issue => new IdentifierNotUsedInspectionResult(string.Format(Description, issue.IdentifierName), Severity, issue.Context, issue.QualifiedName.QualifiedModuleName));

            issues = DocumentNames.DocumentEventHandlerPrefixes.Aggregate(issues, (current, item) => current.Where(issue => !issue.Name.Contains("'" + item)));

            return issues.ToList();
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var interfaceMembers = parseResult.Declarations.FindInterfaceMembers()
                .Concat(parseResult.Declarations.FindInterfaceImplementationMembers())
                .ToList();

            var formEventHandlerScopes = parseResult.Declarations.FindFormEventHandlers()
                .Select(handler => handler.Scope);

            var eventScopes = parseResult.Declarations.Items.Where(item => 
                !item.IsBuiltIn && item.DeclarationType == DeclarationType.Event)
                .Select(e => e.Scope);

            var declareScopes = parseResult.Declarations.Items.Where(item => 
                    item.DeclarationType == DeclarationType.LibraryFunction 
                    || item.DeclarationType == DeclarationType.LibraryProcedure)
                .Select(e => e.Scope);

            var ignoredScopes = formEventHandlerScopes.Concat(eventScopes).Concat(declareScopes);

            var issues = parseResult.Declarations.Items.Where(declaration =>
                !ignoredScopes.Contains(declaration.ParentScope)
                && declaration.DeclarationType == DeclarationType.Parameter
                && !interfaceMembers.Select(m => m.Scope).Contains(declaration.ParentScope)
                && PrimitiveTypes.Contains(declaration.AsTypeName)
                && ((VBAParser.ArgContext) declaration.Context).BYVAL() == null
                && !IsUsedAsByRefParam(parseResult.Declarations, declaration)
                && !declaration.References.Any(reference => reference.IsAssignment))
                .Select(issue => new ParameterCanBeByValInspectionResult(string.Format(Description, issue.IdentifierName), Severity, ((dynamic)issue.Context).ambiguousIdentifier(), issue.QualifiedName));

            return issues;
        }
 public ParameterNotUsedInspectionResult(string inspection, CodeInspectionSeverity type,
     ParserRuleContext context, QualifiedMemberName qualifiedName, bool isInterfaceImplementation, RemoveParametersRefactoring quickFixRefactoring, VBProjectParseResult parseResult)
     : base(inspection, type, qualifiedName.QualifiedModuleName, context)
 {
     _isInterfaceImplementation = isInterfaceImplementation;
     _quickFixRefactoring = quickFixRefactoring;
     _parseResult = parseResult;
 }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var issues = from item in parseResult.Declarations.Items
                         where !item.IsBuiltIn && item.Accessibility == Accessibility.Global
                         && item.Context != null
                         select new ObsoleteGlobalInspectionResult(Description, Severity, new QualifiedContext<ParserRuleContext>(item.QualifiedName.QualifiedModuleName, item.Context));

            return issues;
        }
        public void QuickFix(VBProjectParseResult parseResult, QualifiedSelection selection)
        {
            _model = new RemoveParametersModel(parseResult, selection);
            var target = _model.Declarations.FindSelection(selection, new[] { DeclarationType.Parameter });

            // ReSharper disable once PossibleUnintendedReferenceComparison
            _model.Parameters.Find(param => param.Declaration == target).IsRemoved = true;
            RemoveParameters();
        }
Example #7
0
        public RenameModel(VBE vbe, VBProjectParseResult parseResult, QualifiedSelection selection)
        {
            _vbe = vbe;
            _parseResult = parseResult;
            _declarations = parseResult.Declarations;
            _selection = selection;

            AcquireTarget(out _target, Selection);
        }
        public RemoveParametersModel(VBProjectParseResult parseResult, QualifiedSelection selection)
        {
            _parseResult = parseResult;
            _declarations = parseResult.Declarations;

            AcquireTarget(selection);

            Parameters = new List<Parameter>();
            LoadParameters();
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var issues = parseResult.Declarations.Items
                .Where(item => !item.IsBuiltIn)
                .SelectMany(item =>
                item.References.Where(reference => reference.HasExplicitLetStatement))
                .Select(issue => new ObsoleteLetStatementUsageInspectionResult(Description, Severity, new QualifiedContext<ParserRuleContext>(issue.QualifiedModuleName, issue.Context)));

            return issues;
        }
Example #10
0
 private void _bgw_DoWork(object sender, DoWorkEventArgs e)
 {
     _parser.ParseStarted += _parser_ParseStarted;
     _parser.ResolutionProgress += _parser_ResolutionProgress;
     _parser.ParseProgress += _parser_ParseProgress;
     Result = _parser.Parse(_project);
     _parser.ParseStarted -= _parser_ParseStarted;
     _parser.ResolutionProgress -= _parser_ResolutionProgress;
     _parser.ParseProgress -= _parser_ParseProgress;
 }
 public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
 {
     var issues = from item in parseResult.Declarations.Items
                  where !item.IsBuiltIn
                        && ProcedureTypes.Contains(item.DeclarationType)
                        && item.Accessibility == Accessibility.Implicit
                  let context = new QualifiedContext<ParserRuleContext>(item.QualifiedName, item.Context)
                        select new ImplicitPublicMemberInspectionResult(string.Format(Description, ((dynamic)context.Context).ambiguousIdentifier().GetText()), Severity, context);
     return issues;
 }
 public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
 {
     var issues = from item in parseResult.Declarations.Items
                        where !item.IsBuiltIn
                         && ProcedureTypes.Contains(item.DeclarationType)
                         && !item.IsTypeSpecified()
                        let issue = new {Declaration = item, QualifiedContext = new QualifiedContext<ParserRuleContext>(item.QualifiedName, item.Context)}
                        select new ImplicitVariantReturnTypeInspectionResult(string.Format(Description, issue.Declaration.IdentifierName), Severity, issue.QualifiedContext);
     return issues;
 }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var issues = from item in parseResult.Declarations.Items.Where(item => !item.IsBuiltIn)
                         where (item.DeclarationType == DeclarationType.Variable
                            || item.DeclarationType == DeclarationType.Constant
                            || item.DeclarationType == DeclarationType.Parameter)
                         && !item.IsTypeSpecified()
                         select new VariableTypeNotDeclaredInspectionResult(string.Format(Description, item.DeclarationType, item.IdentifierName), Severity, ((dynamic)item.Context).ambiguousIdentifier(), item.QualifiedName.QualifiedModuleName);

            return issues;
        }
 public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
 {
     var modules = parseResult.ComponentParseResults.ToList();
     foreach (var comment in modules.SelectMany(module => module.Comments))
     {
         if (comment.Marker == Tokens.Rem)
         {
             yield return new ObsoleteCommentSyntaxInspectionResult(Description, Severity, comment);
         }
     }
 }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var issues = parseResult.Declarations.Items
                            .Where(declaration => !declaration.IsBuiltIn 
                                                && declaration.DeclarationType == DeclarationType.Project
                                                && declaration.IdentifierName.StartsWith("VBAProject"))
                            .Select(issue => new GenericProjectNameInspectionResult(string.Format(Description, issue.IdentifierName), Severity, issue, parseResult))
                            .ToList();

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var declarations = parseResult.Declarations.Items.Where(declaration =>
                !declaration.IsBuiltIn &&
                (declaration.DeclarationType == DeclarationType.Constant)
                && !declaration.References.Any());

            foreach (var issue in declarations)
            {
                yield return new IdentifierNotUsedInspectionResult(string.Format(Description, issue.IdentifierName), Severity, ((dynamic)issue.Context).ambiguousIdentifier(), issue.QualifiedName.QualifiedModuleName);
            }
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var issues = parseResult.Declarations.Items
                .Where(item => !item.IsBuiltIn)
                .Where(item => item.DeclarationType == DeclarationType.Variable
                            || item.DeclarationType == DeclarationType.Constant)
                .GroupBy(variable => variable.Context.Parent as ParserRuleContext)
                .Where(grouping => grouping.Count() > 1)
                .Select(grouping => new MultipleDeclarationsInspectionResult(Description, Severity, new QualifiedContext<ParserRuleContext>(grouping.First().QualifiedName.QualifiedModuleName, grouping.Key)));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var declarations = from item in parseResult.Declarations.Items
                where !item.IsBuiltIn && item.HasTypeHint()
                select new ObsoleteTypeHintInspectionResult(string.Format(Description, RubberduckUI.Inspections_DeclarationOf + item.DeclarationType.ToString().ToLower(), item.IdentifierName), Severity, new QualifiedContext(item.QualifiedName, item.Context), item);

            var references = from item in parseResult.Declarations.Items.Where(item => !item.IsBuiltIn).SelectMany(d => d.References)
                where item.HasTypeHint()
                select new ObsoleteTypeHintInspectionResult(string.Format(Description, RubberduckUI.Inspections_UsageOf + item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), Severity, new QualifiedContext(item.QualifiedModuleName, item.Context), item.Declaration);

            return declarations.Union(references);
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var multilineParameters = from p in parseResult.Declarations.Items
                .Where(item => item.DeclarationType == DeclarationType.Parameter)
                where p.Context.GetSelection().LineCount > 1
                select p;

            var issues = multilineParameters
                .Select(param => new MultilineParameterInspectionResult(string.Format(param.Context.GetSelection().LineCount > 3 ? RubberduckUI.EasterEgg_Continuator : Description, param.IdentifierName), Severity, param.Context, param.QualifiedName));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var assignedByValParameters =
                parseResult.Declarations.Items.Where(declaration => !declaration.IsBuiltIn &&
                    declaration.DeclarationType == DeclarationType.Parameter
                    && ((VBAParser.ArgContext)declaration.Context).BYVAL() != null
                    && declaration.References.Any(reference => reference.IsAssignment));

            var issues = assignedByValParameters
                .Select(param => new AssignedByValParameterInspectionResult(string.Format(Name, param.IdentifierName), Severity, param.Context, param.QualifiedName));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var interfaceMembers = parseResult.Declarations.FindInterfaceImplementationMembers();
            var issues = (from item in parseResult.Declarations.Items
                where item.DeclarationType == DeclarationType.Parameter
                    && !item.IsBuiltIn
                    && !interfaceMembers.Select(m => m.Scope).Contains(item.ParentScope)
                let arg = item.Context.Parent as VBAParser.ArgContext
                where arg != null && arg.BYREF() == null && arg.BYVAL() == null
                select new QualifiedContext<VBAParser.ArgContext>(item.QualifiedName, arg))
                .Select(issue => new ImplicitByRefParameterInspectionResult(string.Format(Description, issue.Context.ambiguousIdentifier().GetText()), Severity, issue));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var interfaceMembers = parseResult.Declarations.FindInterfaceMembers();
            var interfaceImplementationMembers = parseResult.Declarations.FindInterfaceImplementationMembers();

            var functions = parseResult.Declarations.Items
                .Where(declaration => !declaration.IsBuiltIn && ReturningMemberTypes.Contains(declaration.DeclarationType)
                    && !interfaceMembers.Contains(declaration)).ToList();

            var issues = functions
                .Where(declaration => declaration.References.All(r => !r.IsAssignment))
                .Select(issue => new NonReturningFunctionInspectionResult(string.Format(Description, issue.IdentifierName), Severity, new QualifiedContext<ParserRuleContext>(issue.QualifiedName, issue.Context), interfaceImplementationMembers.Select(m => m.Scope).Contains(issue.Scope)));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var usages = parseResult.Declarations.Items.Where(declaration => !declaration.IsBuiltIn 
                && declaration.DeclarationType == DeclarationType.Variable
                && !declaration.References.Any(reference => reference.IsAssignment))
                .SelectMany(declaration => declaration.References);

            foreach (var issue in usages)
            {
                //todo: add context to IdentifierReference
                //yield return new UnassignedVariableUsageInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
            }

            return null;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var options = parseResult.Declarations.Items
                .Where(declaration => !declaration.IsBuiltIn 
                                      && declaration.DeclarationType == DeclarationType.ModuleOption
                                      && declaration.Context is VBAParser.OptionExplicitStmtContext)
                .ToList();

            var modules = parseResult.Declarations.Items
                .Where(declaration => !declaration.IsBuiltIn && ModuleTypes.Contains(declaration.DeclarationType));

            var issues = modules.Where(module => !options.Select(option => option.Scope).Contains(module.Scope))
                .Select(issue => new OptionExplicitInspectionResult(Description, Severity, issue.QualifiedName.QualifiedModuleName));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var options = parseResult.Declarations.Items
                .Where(declaration => !declaration.IsBuiltIn
                                      && declaration.DeclarationType == DeclarationType.ModuleOption
                                      && declaration.Context is VBAParser.OptionBaseStmtContext)
                .ToList();

            if (!options.Any())
            {
                return new List<CodeInspectionResultBase>();
            }

            var issues = options.Where(option => ((VBAParser.OptionBaseStmtContext)option.Context).INTEGERLITERAL().GetText() == "1")
                                .Select(issue => new OptionBaseInspectionResult(Description, Severity, issue.QualifiedName.QualifiedModuleName));

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            //note: this misses calls to procedures/functions without a Declaration object.
            // alternative is to walk the tree and listen for "CallStmt".

            var calls = (from declaration in parseResult.Declarations.Items
                from reference in declaration.References
                where (reference.Declaration.DeclarationType == DeclarationType.Function
                       || reference.Declaration.DeclarationType == DeclarationType.Procedure)
                      && reference.HasExplicitCallStatement()
                select reference).ToList();

            var issues = from reference in calls
                let context = reference.Context.Parent.Parent as VBAParser.ExplicitCallStmtContext
                where context != null
                let qualifiedContext = new QualifiedContext<VBAParser.ExplicitCallStmtContext>
                    (reference.QualifiedModuleName, (VBAParser.ExplicitCallStmtContext)reference.Context.Parent.Parent)
                select new ObsoleteCallStatementUsageInspectionResult(Description, Severity, qualifiedContext);

            return issues;
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            // ignore arrays. todo: ArrayIndicesNotAccessedInspection
            var arrays = parseResult.Declarations.Items.Where(declaration =>
                declaration.DeclarationType == DeclarationType.Variable
                && declaration.IsArray()).ToList();

            var declarations = parseResult.Declarations.Items.Where(declaration =>
                declaration.DeclarationType == DeclarationType.Variable
                && !declaration.IsBuiltIn 
                && !arrays.Contains(declaration)
                && !parseResult.Declarations.Items.Any(item => 
                    item.IdentifierName == declaration.AsTypeName 
                    && item.DeclarationType == DeclarationType.UserDefinedType) // UDT variables don't need to be assigned
                && !declaration.IsSelfAssigned
                && !declaration.References.Any(reference => reference.IsAssignment));

            foreach (var issue in declarations)
            {
                yield return new IdentifierNotAssignedInspectionResult(string.Format(Description, issue.IdentifierName), Severity, ((dynamic)issue.Context).ambiguousIdentifier(), issue.QualifiedName.QualifiedModuleName);
            }
        }
        public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
        {
            var interfaceMemberScopes = parseResult.Declarations.FindInterfaceMembers().Select(m => m.Scope).ToList();
            var interfaceImplementationMemberScopes = parseResult.Declarations.FindInterfaceImplementationMembers().Select(m => m.Scope).ToList();

            var parameters = parseResult.Declarations.Items.Where(parameter => !parameter.IsBuiltIn
                && parameter.DeclarationType == DeclarationType.Parameter
                && !(parameter.Context.Parent.Parent is VBAParser.EventStmtContext)
                && !(parameter.Context.Parent.Parent is VBAParser.DeclareStmtContext));

            var unused = parameters.Where(parameter => !parameter.References.Any()).ToList();
            var quickFixRefactoring =
                new RemoveParametersRefactoring(
                    new RemoveParametersPresenterFactory(new ActiveCodePaneEditor(parseResult.Project.VBE),
                        new RemoveParametersDialog(), parseResult));

            var issues = from issue in unused.Where(parameter => !IsInterfaceMemberParameter(parameter, interfaceMemberScopes))
                         let isInterfaceImplementationMember = IsInterfaceMemberImplementationParameter(issue, interfaceImplementationMemberScopes)
                         select new ParameterNotUsedInspectionResult(string.Format(Description, issue.IdentifierName), Severity, ((dynamic)issue.Context).ambiguousIdentifier(), issue.QualifiedName, isInterfaceImplementationMember, quickFixRefactoring, parseResult);

            return issues.ToList();
        }
Example #29
0
        public async Task<IList<ICodeInspectionResult>> FindIssuesAsync(VBProjectParseResult project, CancellationToken token)
        {
            await Task.Yield();

            OnReset();

            var allIssues = new ConcurrentBag<ICodeInspectionResult>();

            var inspections = _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow)
                .Select(inspection =>
                    new Task(() =>
                    {
                        token.ThrowIfCancellationRequested();
                        var inspectionResults = inspection.GetInspectionResults(project);
                        var results = inspectionResults as IList<CodeInspectionResultBase> ?? inspectionResults.ToList();

                        if (results.Any())
                        {
                            OnIssuesFound(results);

                            foreach (var inspectionResult in results)
                            {
                                allIssues.Add(inspectionResult);
                            }
                        }
                    })).ToArray();

            foreach (var inspection in inspections)
            {
                inspection.Start();
            }

            Task.WaitAll(inspections);

            return allIssues.ToList();
        }
 public GenericProjectNameInspectionResult(string inspection, CodeInspectionSeverity type, Declaration target, VBProjectParseResult parseResult) 
     : base(inspection, type, target)
 {
     _parseResult = parseResult;
 }