Ejemplo n.º 1
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var codeFixHelper        = new MemberInvocationCodeFixHelper(context);
            var diagnostic           = codeFixHelper.GetFirstDiagnostic();
            var invocationExpression = await codeFixHelper.GetDiagnosedInvocation();

            if (invocationExpression == null)
            {
                return;
            }

            var oldArgument     = invocationExpression.ArgumentList.Arguments.First();
            var newArgumentName = GetNewMethodArgument(oldArgument);

            if (newArgumentName == null)
            {
                return;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    title: CodeFixMessagesProvider.GetReplaceWithMessage(newArgumentName),
                    createChangedDocument: c => codeFixHelper.ReplaceExpressionWith(oldArgument, newArgumentName, c, "CMS.EventLog"),
                    equivalenceKey: nameof(EventLogArgumentsCodeFixProvider)),
                diagnostic);
        }
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var editor     = new MemberInvocationCodeFixHelper(context);
            var diagnostic = editor.GetFirstDiagnostic();

            if (diagnostic.IsMarkedAsConditionalAccess())
            {
                // currently no support for fixing complicated cases with Conditional Access
                return;
            }

            var invocation = await editor.GetDiagnosedInvocation();

            if (invocation == null || !invocation.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                return;
            }

            // only apply codefix if invocation is placed in class inheriting from System.Web.Ui.Control
            // since as first argument we add 'this' and it has to have right type
            var enclosingClassName = invocation.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (enclosingClassName == null)
            {
                return;
            }

            var semanticModel = await context.Document.GetSemanticModelAsync();

            var enclosingClassType = semanticModel.GetDeclaredSymbol(enclosingClassName);
            var uiControlType      = "System.Web.UI.Control";

            if (enclosingClassType == null ||
                !enclosingClassType.IsDerivedFrom(uiControlType, semanticModel.Compilation))
            {
                return;
            }

            var usingNamespace          = "CMS.Base.Web.UI";
            var newInvocationExpression = GetNewInvocationExpression(invocation);

            if (newInvocationExpression == null)
            {
                return;
            }

            context.RegisterCodeFix(
                CodeAction.Create(
                    title: CodeFixMessagesProvider.GetReplaceWithMessage(newInvocationExpression),
                    createChangedDocument: c => editor.ReplaceExpressionWith(invocation, newInvocationExpression, c, usingNamespace),
                    equivalenceKey: nameof(ClientScriptMethodsCodeFixProvider)),
                diagnostic);
        }