Example #1
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            context.CancellationToken.ThrowIfCancellationRequested();

            var diagnostic = context.Diagnostics.First();
            var classNode  = root.FindNode(diagnostic.Location.SourceSpan) as ClassDeclarationSyntax;

            context.CancellationToken.ThrowIfCancellationRequested();

            var hasNonPublicNoArgumentConstructor = bool.Parse(
                diagnostic.Properties[PublicNoArgumentConstructorIsMissingConstants.HasNonPublicNoArgumentConstructor]);

            if (hasNonPublicNoArgumentConstructor)
            {
                if (context.Document.SupportsSemanticModel)
                {
                    var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                    CheckConstructorsAnalyzerPublicConstructorCodeFix.AddCodeFixWithUpdatingNonPublicConstructor(
                        context, root, diagnostic, classNode, model);
                }
            }
            else
            {
                CheckConstructorsAnalyzerPublicConstructorCodeFix.AddCodeFixWithNewPublicConstructor(
                    context, root, diagnostic, classNode);
            }
        }
    public void VerifyGetFixableDiagnosticIds()
    {
      var fix = new CheckConstructorsAnalyzerPublicConstructorCodeFix();
      var ids = fix.FixableDiagnosticIds.ToList();

      Assert.AreEqual(1, ids.Count, nameof(ids.Count));
      Assert.AreEqual(PublicNoArgumentConstructorIsMissingConstants.DiagnosticId, ids[0],
        nameof(PublicNoArgumentConstructorIsMissingConstants.DiagnosticId));
    }
    public async Task VerifyGetFixesWhenPrivateConstructorNoArgumentsExists()
    {
      var code = File.ReadAllText(
        $@"Targets\{nameof(CheckConstructorsAnalyzerPublicConstructorCodeFixTests)}\{(nameof(this.VerifyGetFixesWhenPrivateConstructorNoArgumentsExists))}.cs");
      var document = TestHelpers.Create(code);
      var tree = await document.GetSyntaxTreeAsync();
      var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new CheckConstructorsAnalyzer());
      var sourceSpan = diagnostics[0].Location.SourceSpan;

      var actions = new List<CodeAction>();
      var codeActionRegistration = new Action<CodeAction, ImmutableArray<Diagnostic>>(
        (a, _) => { actions.Add(a); });

      var fix = new CheckConstructorsAnalyzerPublicConstructorCodeFix();
      var codeFixContext = new CodeFixContext(document, diagnostics[0],
        codeActionRegistration, new CancellationToken(false));
      await fix.RegisterCodeFixesAsync(codeFixContext);

      Assert.AreEqual(1, actions.Count, nameof(actions.Count));

      await TestHelpers.VerifyActionAsync(actions,
        CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.UpdateNonPublicConstructorToPublicDescription, document,
        tree, new[] { "    public" });
    }