private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) { var ifStatement = (IfStatementSyntax)context.Node; foreach (IfRefactoring refactoring in IfRefactoring.Analyze(ifStatement, AnalysisOptions, context.SemanticModel, context.CancellationToken)) { Debug.Assert(refactoring.Kind == RefactoringKind.IfElseToAssignmentWithCoalesceExpression || refactoring.Kind == RefactoringKind.IfElseToReturnWithCoalesceExpression || refactoring.Kind == RefactoringKind.IfElseToYieldReturnWithCoalesceExpression || refactoring.Kind == RefactoringKind.IfReturnToReturnWithCoalesceExpression, refactoring.Kind.ToString()); context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpressionInsteadOfIf, ifStatement); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); IfStatementSyntax ifStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <IfStatementSyntax>(); Debug.Assert(ifStatement != null, $"{nameof(ifStatement)} is null"); if (ifStatement == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.MergeIfStatementWithNestedIfStatement: { CodeAction codeAction = CodeAction.Create( "Merge if with nested if", cancellationToken => { return(MergeIfStatementWithNestedIfStatementRefactoring.RefactorAsync( context.Document, ifStatement, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ReplaceIfStatementWithAssignment: { CodeAction codeAction = CodeAction.Create( "Replace if with assignment", cancellationToken => { return(ReplaceIfStatementWithAssignmentRefactoring.RefactorAsync( context.Document, ifStatement, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfIf: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); IfRefactoring refactoring = IfRefactoring.Analyze( ifStatement, UseCoalesceExpressionInsteadOfIfDiagnosticAnalyzer.AnalysisOptions, semanticModel, context.CancellationToken).First(); CodeAction codeAction = CodeAction.Create( refactoring.Title, cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out IfStatementSyntax ifStatement)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.MergeIfStatementWithNestedIfStatement: { CodeAction codeAction = CodeAction.Create( "Merge if with nested if", cancellationToken => { return(MergeIfStatementWithNestedIfStatementRefactoring.RefactorAsync( context.Document, ifStatement, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ReplaceIfStatementWithAssignment: { CodeAction codeAction = CodeAction.Create( "Replace if with assignment", cancellationToken => { return(ReplaceIfStatementWithAssignmentRefactoring.RefactorAsync( context.Document, ifStatement, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfIf: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); IfRefactoring refactoring = IfRefactoring.Analyze( ifStatement, UseCoalesceExpressionInsteadOfIfDiagnosticAnalyzer.AnalysisOptions, semanticModel, context.CancellationToken).First(); CodeAction codeAction = CodeAction.Create( refactoring.Title, cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ReduceIfNesting: { CodeAction codeAction = CodeAction.Create( "Reduce if nesting", cancellationToken => { return(ReduceIfNestingRefactoring.RefactorAsync( context.Document, ifStatement, recursive: true, cancellationToken: cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }