Beispiel #1
0
        private void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
        {
            var invocation = context.Node as InvocationExpressionSyntax;

            if (invocation == null)
            {
                return;
            }

            var methodName = invocation.MethodName();

            if (InvokeMethods.Contains(methodName))
            {
                // check if the method is the one from UnityEngine
                var symbolInfo   = context.SemanticModel.GetSymbolInfo(invocation);
                var methodSymbol = symbolInfo.Symbol as IMethodSymbol;

                var fullTypeName = methodSymbol?.ContainingType.ToString();

                if (fullTypeName == InvokeMethodTypeName && invocation.ArgumentList.Arguments.Count > 0)
                {
                    var firstArgumentExpression = invocation.ArgumentList.Arguments[0];

                    var invokedMethodName = firstArgumentExpression.GetArgumentValue <string>();

                    var containingClassDeclaration = invocation.Ancestors().FirstOrDefault(a => a is ClassDeclarationSyntax) as ClassDeclarationSyntax;

                    var allMethods = containingClassDeclaration?.Members.OfType <MethodDeclarationSyntax>();

                    var invokeEndPoint = allMethods.FirstOrDefault(m => m.Identifier.ValueText == invokedMethodName);

                    if (invokeEndPoint == null)
                    {
                        var diagnostic = Diagnostic.Create(SupportedDiagnostics.First(), firstArgumentExpression.GetLocation(),
                                                           methodName, invokedMethodName);

                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }
        protected void CheckComplexity <TSyntax>(SyntaxNodeAnalysisContext context,
                                                 Func <TSyntax, Location> getLocationToReport, string declarationType, int threshold)
            where TSyntax : SyntaxNode
        {
            var syntax = (TSyntax)context.Node;

            var cognitiveWalker = new CognitiveComplexityWalker();

            cognitiveWalker.Visit(syntax);
            if (cognitiveWalker.NestingLevel != 0)
            {
                throw new Exception($"There is a problem with the cognitive complexity walker. Expecting ending nesting to be '0' got '{cognitiveWalker.NestingLevel}'");
            }

            if (cognitiveWalker.Complexity > Threshold)
            {
                context.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics.First(), getLocationToReport(syntax),
                                                           cognitiveWalker.Flow.Select(x => x.Location),
                                                           CreatePropertiesFromCognitiveTrace(cognitiveWalker.Flow),
                                                           new object[] { declarationType, cognitiveWalker.Complexity, threshold }));
            }
        }
        private void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
        {
            var invocation = (InvocationExpressionSyntax)context.Node;

            if (invocation.Expression is not MemberAccessExpressionSyntax member)
            {
                return;
            }

            var symbol = context.SemanticModel.GetSymbolInfo(member);

            if (symbol.Symbol is not IMethodSymbol method)
            {
                return;
            }

            if (!IsReportable(method))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics.First(), member.Name.GetLocation(), method.Name));
        }