private static async Task <Document> InvokeMp0100(Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
        {
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var root           = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            // Find the type declaration identified by the diagnostic.
            var invocation = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().First();

            document = await CodeAnalysisUtilities.InvokeAsync(document, invocation, cancellationToken);

            root = await document.GetSyntaxRootAsync(cancellationToken);

            var awaitExpression = root.FindNode(invocation.Span) as AwaitExpressionSyntax;

            invocation = (InvocationExpressionSyntax)awaitExpression.Expression;
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            // TODO: find usages of the left identifier and remove ".Result"
            if (invocation.Parent is EqualsValueClauseSyntax)
            {
                var symbol =
                    semanticModel.GetDeclaredSymbol(((VariableDeclarationSyntax)invocation.Parent.Parent.Parent).Variables.First());
            }
            else
            {
                var assignmentExpression = invocation.Parent as AssignmentExpressionSyntax;
                Debug.Assert(assignmentExpression != null);
                // find references to assignmentExpression.Left
                var x = assignmentExpression.Left;
            }
            return(document);
        }
Exemple #2
0
        public async Task <ProjectAnalysisResult> AnalyzeProjectAsync(Project project, CancellationToken cancellationToken = default)
        {
            ImmutableArray <DiagnosticAnalyzer> analyzers = CodeAnalysisUtilities.GetAnalyzers(
                project: project,
                analyzerAssemblies: _analyzerAssemblies,
                analyzerReferences: _analyzerReferences,
                options: Options);

            if (!analyzers.Any())
            {
                WriteLine($"  No analyzers found to analyze '{project.Name}'", ConsoleColor.DarkGray, Verbosity.Normal);
                return(default);
Exemple #3
0
        public async Task <ProjectFixResult> FixProjectAsync(Project project, CancellationToken cancellationToken = default)
        {
            (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) = CodeAnalysisUtilities.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.TryGetValue(f.id, out ImmutableArray <CodeFixProvider> fixers2),
                cancellationToken).ConfigureAwait(false);

            ImmutableArray <Diagnostic> unfixedDiagnostics = await GetDiagnosticsAsync(
                analyzers,
                fixResult.FixedDiagnostics.Concat(unfixableDiagnostics),
                compilation,
                f => fixersById.TryGetValue(f.id, out ImmutableArray <CodeFixProvider> fixers2),
                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);
            }

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