コード例 #1
0
        /// <summary>
        /// Generate a json file that has the implementation status of the passed in analyzer references.
        /// </summary>
        private static void GenerateStatus(IEnumerable <AnalyzerFileReference> analyzerReferences)
        {
            DescriptorEqualityComparer comparer = new DescriptorEqualityComparer();

            var allAnalyzers = analyzerReferences
                               .Select(analyzerReference => new { AnalyzerPackage = analyzerReference.Display, Analyzers = analyzerReference.GetAnalyzersForAllLanguages() });

            IEnumerable <string> fixableDiagnosticIds = analyzerReferences
                                                        .SelectMany(analyzerReference => analyzerReference.GetFixers())
                                                        .SelectMany(fixer => fixer.FixableDiagnosticIds)
                                                        .Distinct();

            Dictionary <string, AnalyzersStatusInfo> diagnosticInfoMap = new Dictionary <string, AnalyzersStatusInfo>();

            foreach (var group in allAnalyzers)
            {
                foreach (DiagnosticAnalyzer analyzer in group.Analyzers)
                {
                    bool hasImplementation       = HasImplementation(analyzer);
                    bool hasCSharpImplementation = hasImplementation && analyzer.GetType().GetCustomAttribute <DiagnosticAnalyzerAttribute>().Languages.Contains(LanguageNames.CSharp);
                    bool hasVBImplementation     = hasImplementation && analyzer.GetType().GetCustomAttribute <DiagnosticAnalyzerAttribute>().Languages.Contains(LanguageNames.VisualBasic);

                    foreach (DiagnosticDescriptor descriptor in analyzer.SupportedDiagnostics.Distinct(comparer))
                    {
                        if (!diagnosticInfoMap.ContainsKey(descriptor.Id))
                        {
                            bool hasCodeFix = fixableDiagnosticIds.Contains(descriptor.Id);

                            // This assumes a convention similar to A.B.Analyzers and A.B.CSharp.Analyzers and A.B.VisualBasic.Analyzers
                            // Some common dlls might have a common prefix as well.
                            string analyzerPackage = group.AnalyzerPackage.Replace(".CSharp", string.Empty).Replace(".VisualBasic", string.Empty).Replace(".Common", string.Empty);

                            var diagnosticInfo = new AnalyzersStatusInfo
                            {
                                Id       = descriptor.Id,
                                Category = descriptor.Category,
                                HasCSharpImplementation = hasCSharpImplementation,
                                HasVBImplementation     = hasVBImplementation,
                                Name               = analyzer.GetType().Name,
                                Title              = descriptor.Title.ToString(),
                                HelpLink           = descriptor.HelpLinkUri,
                                HasCodeFix         = hasCodeFix,
                                IsEnabledByDefault = descriptor.IsEnabledByDefault.ToString(),
                                AnalyzerPackage    = analyzerPackage
                            };
                            diagnosticInfoMap.Add(descriptor.Id, diagnosticInfo);
                        }
                        else
                        {
                            // Update the state of the existing info.
                            AnalyzersStatusInfo diagnosticInfo = diagnosticInfoMap[descriptor.Id];
                            diagnosticInfo.HasCSharpImplementation |= hasCSharpImplementation;
                            diagnosticInfo.HasVBImplementation     |= hasVBImplementation;
                        }
                    }
                }
            }

            Console.WriteLine(JsonConvert.SerializeObject(new { Diagnostics = diagnosticInfoMap.Values }));
        }
コード例 #2
0
        /// <summary>
        /// Generate a json file that has the implementation status of the passed in analyzer references.
        /// </summary>
        private static void GenerateStatus(IEnumerable<AnalyzerFileReference> analyzerReferences)
        {
            DescriptorEqualityComparer comparer = new DescriptorEqualityComparer();

            var allAnalyzers = analyzerReferences
                .Select(analyzerReference => new { AnalyzerPackage = analyzerReference.Display, Analyzers = analyzerReference.GetAnalyzersForAllLanguages() });

            var fixableDiagnosticIds = analyzerReferences
                .SelectMany(analyzerReference => analyzerReference.GetFixers())
                .SelectMany(fixer => fixer.FixableDiagnosticIds)
                .Distinct();

            Dictionary<string, AnalyzersStatusInfo> diagnosticInfoMap = new Dictionary<string, AnalyzersStatusInfo>();
            foreach (var group in allAnalyzers)
            {
                foreach (var analyzer in group.Analyzers)
                {
                    bool hasImplementation = HasImplementation(analyzer);
                    bool hasCSharpImplementation = hasImplementation && analyzer.GetType().GetCustomAttribute<DiagnosticAnalyzerAttribute>().Languages.Contains(LanguageNames.CSharp);
                    bool hasVBImplementation = hasImplementation && analyzer.GetType().GetCustomAttribute<DiagnosticAnalyzerAttribute>().Languages.Contains(LanguageNames.VisualBasic);

                    foreach (var descriptor in analyzer.SupportedDiagnostics.Distinct(comparer))
                    {
                        if (!diagnosticInfoMap.ContainsKey(descriptor.Id))
                        {
                            var hasCodeFix = fixableDiagnosticIds.Contains(descriptor.Id);

                            // This assumes a convention similar to A.B.Analyzers and A.B.CSharp.Analyzers and A.B.VisualBasic.Analyzers
                            // Some common dlls might have a common prefix as well.
                            var analyzerPackage = group.AnalyzerPackage.Replace(".CSharp", string.Empty).Replace(".VisualBasic", string.Empty).Replace(".Common", string.Empty);

                            var diagnosticInfo = new AnalyzersStatusInfo
                            {
                                Id = descriptor.Id,
                                Category = descriptor.Category,
                                HasCSharpImplementation = hasCSharpImplementation,
                                HasVBImplementation = hasVBImplementation,
                                Name = analyzer.GetType().Name,
                                Title = descriptor.Title.ToString(),
                                HelpLink = descriptor.HelpLinkUri,
                                HasCodeFix = hasCodeFix,
                                IsEnabledByDefault = descriptor.IsEnabledByDefault.ToString(),
                                AnalyzerPackage = analyzerPackage
                            };
                            diagnosticInfoMap.Add(descriptor.Id, diagnosticInfo);
                        }
                        else
                        {
                            // Update the state of the existing info.
                            var diagnosticInfo = diagnosticInfoMap[descriptor.Id];
                            diagnosticInfo.HasCSharpImplementation |= hasCSharpImplementation;
                            diagnosticInfo.HasVBImplementation |= hasVBImplementation;
                        }
                    }

                }
            }

            Console.WriteLine(JsonConvert.SerializeObject(new { Diagnostics = diagnosticInfoMap.Values}));
        }