public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            if (ParseTreeResults == null)
            {
                Logger.Debug("Aborting GetInspectionResults because ParseTree results were not passed");
                return(new InspectionResultBase[] { });
            }
            var subStmts = ParseTreeResults.ArgListsWithOneByRefParam
                           .Where(context => context.Context.Parent is VBAParser.SubStmtContext)
                           .Select(context => (VBAParser.SubStmtContext)context.Context.Parent)
                           .ToList();

            var subStmtsNotImplementingInterfaces = subStmts
                                                    .Where(c =>
            {
                var declaration =
                    UserDeclarations.SingleOrDefault(d => d.Context == c);

                if (UserDeclarations.FindInterfaceMembers().Contains(declaration))
                {
                    return(false);
                }

                var interfaceImplementation = UserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));
                if (interfaceImplementation == null)
                {
                    return(true);
                }

                var interfaceMember = UserDeclarations.FindInterfaceMember(interfaceImplementation);

                return(interfaceMember == null);
            });

            var subStmtsNotImplementingEvents = subStmts
                                                .Where(c =>
            {
                var declaration = UserDeclarations.SingleOrDefault(d => d.Context == c);

                if (declaration == null)
                {
                    return(false);
                }                                               // rather be safe than sorry

                return(UserDeclarations.Where(item => item.IsWithEvents)
                       .All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null) &&
                       !State.AllDeclarations.FindBuiltInEventHandlers().Contains(declaration));
            });

            return(ParseTreeResults.ArgListsWithOneByRefParam
                   .Where(context => context.Context.Parent is VBAParser.SubStmtContext &&
                          subStmtsNotImplementingInterfaces.Contains(context.Context.Parent) &&
                          subStmtsNotImplementingEvents.Contains(context.Context.Parent))
                   .Select(context => new ProcedureShouldBeFunctionInspectionResult(this,
                                                                                    State,
                                                                                    new QualifiedContext <VBAParser.ArgListContext>(context.ModuleName,
                                                                                                                                    context.Context as VBAParser.ArgListContext),
                                                                                    new QualifiedContext <VBAParser.SubStmtContext>(context.ModuleName,
                                                                                                                                    context.Context.Parent as VBAParser.SubStmtContext))));
        }
        private Declaration ParentDeclaration(IdentifierReference reference)
        {
            var declarationTypes = new[] { DeclarationType.Function, DeclarationType.Procedure, DeclarationType.Property };

            return(UserDeclarations.SingleOrDefault(d =>
                                                    reference.ParentScoping.Equals(d) && declarationTypes.Contains(d.DeclarationType) &&
                                                    d.QualifiedName.QualifiedModuleName.Equals(reference.QualifiedModuleName)));
        }
Exemple #3
0
        public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            var subStmts = State.ArgListsWithOneByRefParam
                           .Where(context => context.Context.Parent is VBAParser.SubStmtContext)
                           .Select(context => (VBAParser.SubStmtContext)context.Context.Parent)
                           .ToList();

            var subStmtsNotImplementingInterfaces = subStmts
                                                    .Where(c =>
            {
                var declaration =
                    UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
                                                     d.IdentifierName == c.identifier().GetText() &&
                                                     d.Context.GetSelection().Equals(c.GetSelection()));

                var interfaceImplementation = UserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));

                if (interfaceImplementation == null)
                {
                    return(true);
                }

                var interfaceMember = UserDeclarations.FindInterfaceMember(interfaceImplementation);
                return(interfaceMember == null);
            });

            var subStmtsNotImplementingEvents = subStmts
                                                .Where(c =>
            {
                var declaration = UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
                                                                   d.IdentifierName == c.identifier().GetText() &&
                                                                   d.Context.GetSelection().Equals(c.GetSelection()));

                if (declaration == null)
                {
                    return(false);
                }                                               // rather be safe than sorry

                return(UserDeclarations.Where(item => item.IsWithEvents)
                       .All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null));
            });

            return(State.ArgListsWithOneByRefParam
                   .Where(context => context.Context.Parent is VBAParser.SubStmtContext &&
                          subStmtsNotImplementingInterfaces.Contains(context.Context.Parent) &&
                          subStmtsNotImplementingEvents.Contains(context.Context.Parent))
                   .Select(context => new ProcedureShouldBeFunctionInspectionResult(this,
                                                                                    State,
                                                                                    new QualifiedContext <VBAParser.ArgListContext>(context.ModuleName,
                                                                                                                                    context.Context as VBAParser.ArgListContext),
                                                                                    new QualifiedContext <VBAParser.SubStmtContext>(context.ModuleName,
                                                                                                                                    context.Context.Parent as VBAParser.SubStmtContext))));
        }