private static bool IsMessage(SyntaxNodeAnalysisContext context, MethodDeclarationSyntax method, Type metadata, string methodName)
        {
            var classDeclaration = method?.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (classDeclaration == null)
            {
                return(false);
            }

            var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);

            var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
            var scriptInfo = new ScriptInfo(typeSymbol);

            if (!scriptInfo.HasMessages)
            {
                return(false);
            }

            if (!scriptInfo.IsMessage(methodSymbol))
            {
                return(false);
            }

            return(scriptInfo.Metadata == metadata && methodSymbol.Name == methodName);
        }
        private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var method = (MethodDeclarationSyntax)context.Node;

            if (method?.Body == null)
            {
                return;
            }

            if (HasPolymorphicModifier(method))
            {
                return;
            }

            if (method.Body.Statements.Count > 0)
            {
                return;
            }

            var classDeclaration = method.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (classDeclaration == null)
            {
                return;
            }

            var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
            var scriptInfo = new ScriptInfo(typeSymbol);

            if (!scriptInfo.HasMessages)
            {
                return;
            }

            var symbol = context.SemanticModel.GetDeclaredSymbol(method);

            if (!scriptInfo.IsMessage(symbol))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, method.Identifier.GetLocation(), symbol.Name));
        }
        private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
        {
            var node = context.GetSuppressibleNode <SyntaxNode>(diagnostic, n => n is ParameterSyntax || n is MethodDeclarationSyntax);

            if (node is ParameterSyntax)
            {
                node = node
                       .Ancestors()
                       .OfType <MethodDeclarationSyntax>()
                       .FirstOrDefault();
            }

            if (node == null)
            {
                return;
            }

            var model = context.GetSemanticModel(diagnostic.Location.SourceTree);

            if (!(model.GetDeclaredSymbol(node) is IMethodSymbol methodSymbol))
            {
                return;
            }

            var scriptInfo = new ScriptInfo(methodSymbol.ContainingType);

            if (!scriptInfo.IsMessage(methodSymbol))
            {
                return;
            }

            foreach (var suppression in SupportedSuppressions)
            {
                if (suppression.SuppressedDiagnosticId == diagnostic.Id)
                {
                    context.ReportSuppression(Suppression.Create(suppression, diagnostic));
                }
            }
        }
예제 #4
0
        private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
        {
            if (!(context.Node is ClassDeclarationSyntax classDeclaration))
            {
                return;
            }

            var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
            var scriptInfo = new ScriptInfo(typeSymbol);

            if (!scriptInfo.HasMessages)
            {
                return;
            }

            var methods  = classDeclaration.Members.OfType <MethodDeclarationSyntax>();
            var messages = scriptInfo
                           .GetMessages()
                           .ToLookup(m => m.Name);

            foreach (var method in methods)
            {
                var methodName = method.Identifier.Text;

                if (!messages.Contains(methodName))
                {
                    continue;
                }

                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);
                if (scriptInfo.IsMessage(methodSymbol))
                {
                    continue;
                }

                context.ReportDiagnostic(Diagnostic.Create(Rule, method.Identifier.GetLocation(), methodName));
            }
        }
예제 #5
0
        private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var method = (MethodDeclarationSyntax)context.Node;

            if (method?.Body == null)
            {
                return;
            }

            if (method.Modifiers.Any(SyntaxKind.ProtectedKeyword))
            {
                return;
            }

            var classDeclaration = method.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (classDeclaration == null || classDeclaration.Modifiers.Any(SyntaxKind.SealedKeyword))
            {
                return;
            }

            var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
            var scriptInfo = new ScriptInfo(typeSymbol);

            if (!scriptInfo.HasMessages)
            {
                return;
            }

            var symbol = context.SemanticModel.GetDeclaredSymbol(method);

            if (!scriptInfo.IsMessage(symbol))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, method.Identifier.GetLocation()));
        }
        private static bool IsCriticalMessage(SyntaxNodeAnalysisContext context, MethodDeclarationSyntax method)
        {
            var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);

            var classDeclaration = method.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (classDeclaration == null)
            {
                return(false);
            }

            var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
            var scriptInfo = new ScriptInfo(typeSymbol);

            if (!scriptInfo.HasMessages)
            {
                return(false);
            }

            if (!scriptInfo.IsMessage(methodSymbol))
            {
                return(false);
            }

            switch (methodSymbol.Name)
            {
            case "Update":
            case "FixedUpdate":
            case "LateUpdate":
            case "OnGUI":
                return(true);

            default:
                return(false);
            }
        }