Exemplo n.º 1
0
        private void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context)
        {
            var constructor = (ConstructorDeclarationSyntax)context.Node;

            RemoveRedundantBaseConstructorCallRefactoring.Analyze(context, constructor);

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

            ConstructorDeclarationSyntax constructor = root
                                                       .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                       .FirstAncestorOrSelf <ConstructorDeclarationSyntax>();

            if (constructor == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantBaseConstructorCall:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant base constructor call",
                        cancellationToken => RemoveRedundantBaseConstructorCallRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.RemoveRedundantConstructor:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant constructor",
                        cancellationToken => RemoveRedundantConstructorRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AbstractTypeShouldNotHavePublicConstructors:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Change accessibility to 'protected'",
                        cancellationToken => AbstractTypeShouldNotHavePublicConstructorsRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }