Exemplo n.º 1
0
        private async Task <ProjectAnalysisResult> AnalyzeProjectCoreAsync(Project project, CancellationToken cancellationToken = default)
        {
            ImmutableArray <DiagnosticAnalyzer> analyzers = CodeAnalysisHelpers.GetAnalyzers(
                project: project,
                analyzerAssemblies: _analyzerAssemblies,
                analyzerReferences: _analyzerReferences,
                options: Options);

            if (!analyzers.Any())
            {
                WriteLine($"  No analyzers found to analyze '{project.Name}'", ConsoleColor.DarkGray, Verbosity.Normal);

                if (Options.IgnoreCompilerDiagnostics)
                {
                    return(default);
Exemplo n.º 2
0
        public async Task <ProjectFixResult> FixProjectAsync(Project project, CancellationToken cancellationToken = default)
        {
            (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) = CodeAnalysisHelpers.GetAnalyzersAndFixers(
                project: project,
                analyzerAssemblies: _analyzerAssemblies,
                analyzerReferences: _analyzerReferences,
                options: Options);

            ProjectFixResult fixResult = await FixProjectAsync(project, analyzers, fixers, cancellationToken).ConfigureAwait(false);

            Compilation compilation = await CurrentSolution.GetProject(project.Id).GetCompilationAsync(cancellationToken).ConfigureAwait(false);

            Dictionary <string, ImmutableArray <CodeFixProvider> > fixersById = fixers
                                                                                .SelectMany(f => f.FixableDiagnosticIds.Select(id => (id, fixer: f)))
                                                                                .GroupBy(f => f.id)
                                                                                .ToDictionary(f => f.Key, g => g.Select(f => f.fixer).Distinct().ToImmutableArray());

            ImmutableArray <Diagnostic> unfixableDiagnostics = await GetDiagnosticsAsync(
                analyzers,
                fixResult.FixedDiagnostics,
                compilation,
                f => !fixersById.ContainsKey(f.id),
                cancellationToken)
                                                               .ConfigureAwait(false);

            ImmutableArray <Diagnostic> unfixedDiagnostics = await GetDiagnosticsAsync(
                analyzers,
                fixResult.FixedDiagnostics.Concat(unfixableDiagnostics),
                compilation,
                f => fixersById.ContainsKey(f.id),
                cancellationToken)
                                                             .ConfigureAwait(false);

            int numberOfAddedFileBanners = 0;

            if (Options.FileBannerLines.Any())
            {
                numberOfAddedFileBanners = await AddFileBannerAsync(CurrentSolution.GetProject(project.Id), Options.FileBannerLines, cancellationToken).ConfigureAwait(false);
            }

            ImmutableArray <DocumentId> formattedDocuments = ImmutableArray <DocumentId> .Empty;

            if (Options.Format)
            {
                formattedDocuments = await FormatProjectAsync(CurrentSolution.GetProject(project.Id), cancellationToken).ConfigureAwait(false);
            }

            var result = new ProjectFixResult(
                kind: fixResult.Kind,
                fixedDiagnostics: fixResult.FixedDiagnostics,
                unfixedDiagnostics: unfixedDiagnostics,
                unfixableDiagnostics: unfixableDiagnostics,
                analyzers: fixResult.Analyzers,
                fixers: fixResult.Fixers,
                numberOfFormattedDocuments: formattedDocuments.Length,
                numberOfAddedFileBanners: numberOfAddedFileBanners);

            LogHelpers.WriteFixSummary(
                result.FixedDiagnostics,
                result.UnfixedDiagnostics,
                result.UnfixableDiagnostics,
                baseDirectoryPath: Path.GetDirectoryName(project.FilePath),
                indentation: "  ",
                formatProvider: FormatProvider,
                verbosity: Verbosity.Detailed);

            return(result);
        }