Exemplo n.º 1
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));
            }
        }
Exemplo n.º 2
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()));
        }
Exemplo n.º 3
0
        private void AnalyzeDiagnostic(Diagnostic diagnostic, SuppressionAnalysisContext context)
        {
            var node = diagnostic.Location.SourceTree.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan);

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

            if (!(node is MethodDeclarationSyntax method))
            {
                return;
            }

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

            if (!(model.GetDeclaredSymbol(method) 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));
                }
            }
        }
        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);
            }
        }