コード例 #1
0
        private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            AnalyzeBraces(context, ifStatement);

            MergeIfStatementWithNestedIfStatementRefactoring.Analyze(context, ifStatement);

            ReplaceIfStatementWithReturnStatementRefactoring.Analyze(context, ifStatement);

            ReplaceIfStatementWithAssignmentRefactoring.Analyze(context, ifStatement);
        }
コード例 #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstDescendantOrSelf(root, context.Span, out IfStatementSyntax ifStatement))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Replace if-else with return",
                cancellationToken => ReplaceIfStatementWithReturnStatementRefactoring.RefactorAsync(context.Document, ifStatement, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.ReplaceIfStatementWithReturnStatement));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var ifStatement = (IfStatementSyntax)root.DescendantNodes(context.Span)
                              .FirstOrDefault(f => f.IsKind(SyntaxKind.IfStatement) && context.Span.Contains(f.Span));

            if (ifStatement == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Replace if-else with return",
                cancellationToken => ReplaceIfStatementWithReturnStatementRefactoring.RefactorAsync(context.Document, ifStatement, cancellationToken),
                DiagnosticIdentifiers.ReplaceIfStatementWithReturnStatement + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }