Пример #1
0
        /// <summary>
        /// Analyzes the project and returns information about the diagnostics in it.
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <ImmutableList <StyleCopDiagnostic> > GetDiagnosticsAsync()
        {
            var diagnostics = ImmutableList.CreateBuilder <StyleCopDiagnostic>();

            var syntaxTrees = this.analyzerCompilation.SyntaxTrees;

            foreach (var syntaxTree in syntaxTrees)
            {
                var match = DiagnosticPathRegex.Match(syntaxTree.FilePath);
                if (!match.Success)
                {
                    continue;
                }

                string shortName       = match.Groups["name"].Value;
                string noCodeFixReason = null;

                // Check if this syntax tree represents a diagnostic
                SyntaxNode syntaxRoot = await syntaxTree.GetRootAsync().ConfigureAwait(false);

                SemanticModel semanticModel   = this.analyzerCompilation.GetSemanticModel(syntaxTree);
                SyntaxNode    classSyntaxNode = syntaxRoot.DescendantNodes().FirstOrDefault(x => x.IsKind(SyntaxKind.ClassDeclaration));

                if (classSyntaxNode == null)
                {
                    continue;
                }

                INamedTypeSymbol classSymbol = semanticModel.GetDeclaredSymbol(classSyntaxNode) as INamedTypeSymbol;

                if (!this.InheritsFrom(classSymbol, this.diagnosticAnalyzerTypeSymbol))
                {
                    continue;
                }

                if (classSymbol.IsAbstract)
                {
                    continue;
                }

                bool hasImplementation = HasImplementation(syntaxRoot);

                IEnumerable <DiagnosticDescriptor> descriptorInfos = this.GetDescriptor(classSymbol);

                foreach (var descriptorInfo in descriptorInfos)
                {
                    var(codeFixStatus, fixAllStatus) = this.GetCodeFixAndFixAllStatus(descriptorInfo.Id, classSymbol, out noCodeFixReason);
                    string status = this.GetStatus(classSymbol, syntaxRoot, semanticModel, descriptorInfo);
                    if (descriptorInfo.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
                    {
                        continue;
                    }

                    var diagnostic = new StyleCopDiagnostic
                    {
                        Id                = descriptorInfo.Id,
                        Category          = descriptorInfo.Category,
                        HasImplementation = hasImplementation,
                        Status            = status,
                        Name              = shortName,
                        Title             = descriptorInfo.Title.ToString(),
                        HelpLink          = descriptorInfo.HelpLinkUri,
                        CodeFixStatus     = codeFixStatus,
                        FixAllStatus      = fixAllStatus,
                        NoCodeFixReason   = noCodeFixReason,
                    };
                    diagnostics.Add(diagnostic);
                }
            }

            return(diagnostics.ToImmutable());
        }
        /// <summary>
        /// Analyzes the project and returns information about the diagnostics in it.
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task<ImmutableList<StyleCopDiagnostic>> GetDiagnosticsAsync()
        {
            var diagnostics = ImmutableList.CreateBuilder<StyleCopDiagnostic>();

            var syntaxTrees = this.analyzerCompilation.SyntaxTrees;

            foreach (var syntaxTree in syntaxTrees)
            {
                var match = diagnosticPathRegex.Match(syntaxTree.FilePath);
                if (!match.Success)
                {
                    continue;
                }

                string shortName = match.Groups["name"].Value;
                CodeFixStatus codeFixStatus;
                string noCodeFixReason = null;

                // Check if this syntax tree represents a diagnostic
                SyntaxNode syntaxRoot = await syntaxTree.GetRootAsync();
                SemanticModel semanticModel = this.analyzerCompilation.GetSemanticModel(syntaxTree);
                SyntaxNode classSyntaxNode = syntaxRoot.DescendantNodes().FirstOrDefault(x => x.IsKind(SyntaxKind.ClassDeclaration));

                if (classSyntaxNode == null)
                {
                    continue;
                }

                INamedTypeSymbol classSymbol = semanticModel.GetDeclaredSymbol(classSyntaxNode) as INamedTypeSymbol;

                if (!this.InheritsFrom(classSymbol, this.diagnosticAnalyzerTypeSymbol))
                {
                    continue;
                }

                if (classSymbol.IsAbstract)
                {
                    continue;
                }

                bool hasImplementation = HasImplementation(syntaxRoot);

                IEnumerable<DiagnosticDescriptor> descriptorInfos = this.GetDescriptor(classSymbol);

                foreach (var descriptorInfo in descriptorInfos)
                {
                    codeFixStatus = this.HasCodeFix(descriptorInfo.Id, classSymbol, out noCodeFixReason);
                    string status = this.GetStatus(classSymbol, syntaxRoot, semanticModel, descriptorInfo);
                    if (descriptorInfo.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
                    {
                        continue;
                    }

                    var diagnostic = new StyleCopDiagnostic
                    {
                        Id = descriptorInfo.Id,
                        Category = descriptorInfo.Category,
                        HasImplementation = hasImplementation,
                        Status = status,
                        Name = shortName,
                        Title = descriptorInfo.Title.ToString(),
                        HelpLink = descriptorInfo.HelpLinkUri,
                        CodeFixStatus = codeFixStatus,
                        NoCodeFixReason = noCodeFixReason
                    };
                    diagnostics.Add(diagnostic);
                }
            }

            return diagnostics.ToImmutable();
        }