private static async Task <SyntaxNode> CreateNewDocumentSyntaxRootAsync(FixAllContext fixAllContext, Document document, CancellationToken cancellationToken) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var nodesToFix = diagnostics.Select(diagnostic => DefaulIfNullExpressionHelper.GetTargetExpression(diagnostic, root)); return(root.ReplaceNodes(nodesToFix, (orignalNode, rewritten) => DefaulIfNullExpressionHelper.CreateRelacementNode(rewritten))); }
private static void AnalyzeNode(SyntaxNodeAnalysisContext context) { var node = (context.Node as MemberAccessExpressionSyntax); if (node == null || !DefaulIfNullExpressionHelper.CanFix(node)) { return; } var diagnostic = Diagnostic.Create(Rule, node.Name.GetLocation(), node.Name.ToString()); context.ReportDiagnostic(diagnostic); }
private static async Task <Document> CreateChangedDocument(CodeFixContext codeFixContext, CancellationToken cancellationToken) { var document = codeFixContext.Document; var diagnostic = codeFixContext.Diagnostics.First(); var root = await document.GetSyntaxRootAsync(cancellationToken); var invocationExpression = DefaulIfNullExpressionHelper.GetTargetExpression(diagnostic, root); var replacement = DefaulIfNullExpressionHelper.CreateRelacementNode(invocationExpression); var newRoot = root.ReplaceNode(invocationExpression, replacement); return(document.WithSyntaxRoot(newRoot)); }