Exemple #1
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (context.Node?.SyntaxTree?.FilePath == null ||
                context.SemanticModel?.Compilation?.Assembly == null)
            {
                return;
            }

            var syntaxNode    = context.Node;
            var semanticModel = context.SemanticModel;
            var assemblyName  = semanticModel.Compilation.AssemblyName;

            var projectFilePath = _projectFileResolver.FindByAssemblyName(assemblyName);

            if (projectFilePath == null)
            {
                return;
            }

            var dependencyAnalyzer = _analyzerProvider.GetDependencyAnalyzer(projectFilePath);

            if (dependencyAnalyzer == null)
            {
                return;
            }

            var configState = dependencyAnalyzer.ConfigState;

            switch (configState)
            {
            case AnalyzerConfigState.NoConfig:
                break;

            case AnalyzerConfigState.ConfigError:
                ReportConfigException(context, dependencyAnalyzer.ConfigException);
                break;

            case AnalyzerConfigState.Enabled:
                var config = dependencyAnalyzer.Config;
                var illegalDependencies = dependencyAnalyzer.AnalyzeSyntaxNode(new RoslynSyntaxNode(syntaxNode), new RoslynSemanticModel(semanticModel));
                ReportIllegalDependencies(illegalDependencies, context, config.IssueKind, config.MaxIssueCount);
                break;

            case AnalyzerConfigState.Disabled:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(configState), configState, "Unexpected value.");
            }
        }
Exemple #2
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (context.Node?.SyntaxTree?.FilePath == null ||
                context.SemanticModel?.Compilation?.Assembly == null)
            {
                return;
            }

            var syntaxNode    = context.Node;
            var semanticModel = context.SemanticModel;
            var assemblyName  = semanticModel.Compilation.AssemblyName;

            var projectFilePath = _projectFileResolver.FindByAssemblyName(assemblyName);

            if (projectFilePath == null)
            {
                return;
            }

            var dependencyAnalyzer = _analyzerProvider.GetDependencyAnalyzer(projectFilePath);

            if (dependencyAnalyzer == null)
            {
                return;
            }

            var analyzerMessages = dependencyAnalyzer.AnalyzeSyntaxNode(new RoslynSyntaxNode(syntaxNode), new RoslynSemanticModel(semanticModel));

            foreach (var analyzerMessage in analyzerMessages)
            {
                switch (analyzerMessage)
                {
                case ConfigErrorMessage configErrorMessage:
                    ReportConfigException(context, configErrorMessage.Exception);
                    break;

                case IllegalDependencyMessage illegalDependencyMessage:
                    ReportIllegalDependency(context, illegalDependencyMessage.IllegalDependency, illegalDependencyMessage.IssueKind);
                    break;

                case TooManyIssuesMessage _:
                    ReportTooManyIssues(context);
                    break;
                }
            }
        }
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (context.Node?.SyntaxTree?.FilePath == null ||
                context.SemanticModel?.Compilation?.Assembly == null)
            {
                return;
            }

            var syntaxNode    = context.Node;
            var semanticModel = context.SemanticModel;
            var assemblyName  = semanticModel.Compilation.AssemblyName;

            var projectFilePath = _projectFileResolver.FindByAssemblyName(assemblyName);

            if (projectFilePath == null)
            {
                return;
            }

            var dependencyAnalyzer = _analyzerProvider.GetDependencyAnalyzer(projectFilePath);

            if (dependencyAnalyzer == null)
            {
                return;
            }

            var analyzerMessages = dependencyAnalyzer.AnalyzeSyntaxNode(new RoslynSyntaxNode(syntaxNode), new RoslynSemanticModel(semanticModel));

            foreach (var analyzerMessage in analyzerMessages)
            {
                var diagnostic = ConvertAnalyzerMessageToDiagnostic(context.Node, analyzerMessage);

                if (diagnostic != null)
                {
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
 protected override string RetrieveItemToCache(string key) => _projectFileResolver.FindByAssemblyName(key);