private async Task <Document> AddAnnotation(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var highlightedNode = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent;

            var methodDeclaration = CodeFixUtil.GetParentNode(highlightedNode, typeof(MethodDeclarationSyntax)) as MethodDeclarationSyntax;
            var attributesList    = methodDeclaration.AttributeLists[0] as AttributeListSyntax;

            if (methodDeclaration == null)
            {
                return(document);
            }

            var annotationValidate = SF.AttributeList()
                                     .AddAttributes(SF.Attribute(SF.IdentifierName("ValidateAntiForgeryToken")))
                                     .WithLeadingTrivia(CodeFixUtil.KeepLastLine(attributesList.GetLeadingTrivia()));

            var nodes = new List <SyntaxNode>();

            nodes.Add(annotationValidate);

            var newRoot = root.InsertNodesAfter(attributesList, nodes);

            return(document.WithSyntaxRoot(newRoot));
        }
        private async Task <Document> AddAnnotation(Document document,
                                                    Diagnostic diagnostic,
                                                    CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync().ConfigureAwait(false);

            var methodDeclaration = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent
                                    .AncestorsAndSelf()
                                    .OfType <MethodDeclarationSyntax>()
                                    .First();

            if (methodDeclaration == null)
            {
                return(document);
            }

            var attributesList = methodDeclaration.AttributeLists[0];

            var annotationValidate = SF.AttributeList()
                                     .AddAttributes(SF.Attribute(SF.IdentifierName("ValidateAntiForgeryToken")))
                                     .WithLeadingTrivia(CodeFixUtil.KeepLastLine(attributesList.GetLeadingTrivia()));

            var nodes = new List <SyntaxNode> {
                annotationValidate
            };

            var newRoot = root.InsertNodesAfter(attributesList, nodes);

            return(document.WithSyntaxRoot(newRoot));
        }
Exemple #3
0
        private async Task <Document> AddSecureFlags(Document document,
                                                     Diagnostic diagnostic,
                                                     CancellationToken cancellationToken,
                                                     string[]          propertyNames)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var variableDeclarator = FindParentNode(root.FindToken(diagnostic.Location.SourceSpan.Start).Parent);

            if (variableDeclarator == null)
            {
                return(document); //Abort!
            }
            if (!(variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration))
            {
                return(document); //Abort!
            }
            if (!(variableDeclaration.Parent is LocalDeclarationStatementSyntax parentDeclaration))
            {
                return(document); //Abort!
            }
            var identifierCookie = variableDeclaration.Variables[0];

            //Building the nodes model

            var nodes = new List <SyntaxNode>();

            foreach (var property in propertyNames)
            {
                var newAssignment = SF.ExpressionStatement(
                    SF.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                            SF.MemberAccessExpression(
                                                SyntaxKind.SimpleMemberAccessExpression,
                                                SF.IdentifierName(identifierCookie.Identifier),
                                                SF.IdentifierName(property)),
                                            SF.LiteralExpression(SyntaxKind.TrueLiteralExpression)
                                            ))
                                    .WithLeadingTrivia(CodeFixUtil.KeepLastLine(parentDeclaration.GetLeadingTrivia()));

                /*
                 * .WithLeadingTrivia(parentDeclaration.GetLeadingTrivia()
                 *  .Insert(0, SF.ElasticEndOfLine(Environment.NewLine))
                 * );*/
                nodes.Add(newAssignment);
            }

            //Inserting the nodes
            var newRoot = root.InsertNodesAfter(parentDeclaration, nodes);

            return(document.WithSyntaxRoot(newRoot));
        }