private static async Task <Document> AddNonSerializedAttributes(SyntaxNode root, TypeDeclarationSyntax declaration, CodeFixContext context, CancellationToken cancellationToken)
        {
            var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);

            var(serializableMembers, _) = SerializationAttributesHelper.AnalyzeTypeDeclaration(declaration);

            var insertUsingDirective = true;
            var ns = root.DescendantNodesAndSelf()
                     .OfType <UsingDirectiveSyntax>()
                     .FirstOrDefault(directive => string.Equals(directive.Name.ToString(), Constants.SystemNamespace));

            if (ns is object)
            {
                insertUsingDirective = false;
            }

            if (insertUsingDirective)
            {
                var usingDirective = UsingDirective(IdentifierName(Constants.SystemNamespace)).WithTrailingTrivia(EndOfLine("\r\n"));
                var lastUsing      = root.DescendantNodesAndSelf().OfType <UsingDirectiveSyntax>().LastOrDefault();
                if (lastUsing is object)
                {
                    editor.InsertAfter(lastUsing, usingDirective);
                }
                else if (root.DescendantNodesAndSelf().OfType <NamespaceDeclarationSyntax>().FirstOrDefault() is NamespaceDeclarationSyntax firstNamespace)
                {
                    editor.InsertBefore(lastUsing, usingDirective);
                }
                else if (root.DescendantNodesAndSelf().FirstOrDefault() is SyntaxNode firstNode)
                {
                    editor.InsertBefore(firstNode, usingDirective);
                }
            }

            foreach (var member in serializableMembers)
            {
                // Add the [NonSerialized] attribute
                var attribute = AttributeList().AddAttributes(Attribute(ParseName(Constants.NonSerializedAttributeFullyQualifiedName)).WithAdditionalAnnotations(Simplifier.Annotation));

                // Since [NonSerialized] is a field-only attribute, add the field target specifier.
                if (member is PropertyDeclarationSyntax)
                {
                    attribute = attribute.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.FieldKeyword)));
                }

                editor.AddAttribute(member, attribute);
            }

            return(editor.GetChangedDocument());
        }
 private void CheckSyntaxNode(SyntaxNodeAnalysisContext context)
 {
     if (context.Node is TypeDeclarationSyntax declaration && !declaration.Modifiers.Any(m => m.Kind() == SyntaxKind.StaticKeyword))
     {
         if (declaration.HasAttribute(Constants.GenerateSerializerAttributeName) || declaration.HasAttribute(Constants.SerializableAttributeName))
         {
             var(serializableMembers, nextId) = SerializationAttributesHelper.AnalyzeTypeDeclaration(declaration);
             if (serializableMembers.Count > 0)
             {
                 context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation()));
             }
         }
     }
 }
        private static async Task <Document> AddSerializationAttributes(TypeDeclarationSyntax declaration, CodeFixContext context, CancellationToken cancellationToken)
        {
            var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);

            var(serializableMembers, nextId) = SerializationAttributesHelper.AnalyzeTypeDeclaration(declaration);

            foreach (var member in serializableMembers)
            {
                // Add the [Id(x)] attribute
                var attribute = Attribute(ParseName(Constants.IdAttributeFullyQualifiedName))
                                .AddArgumentListArguments(AttributeArgument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)nextId++))))
                                .WithAdditionalAnnotations(Simplifier.Annotation);
                editor.AddAttribute(member, attribute);
            }

            return(editor.GetChangedDocument());
        }