private FieldDeclarationSyntax WithReadOnlyAdded(FieldDeclarationSyntax fieldDeclaration)
        {
            SyntaxTriviaList leadingTrivia = fieldDeclaration.GetLeadingTrivia();

            fieldDeclaration = fieldDeclaration.WithLeadingTrivia();
            fieldDeclaration = fieldDeclaration.AddModifiers(_readonlyModifier);
            fieldDeclaration = fieldDeclaration.WithLeadingTrivia(leadingTrivia);
            return(fieldDeclaration);
        }
Example #2
0
        private static SyntaxNode MakeSingleFieldReadonly(SyntaxNode root, FieldDeclarationSyntax fieldDeclaration)
        {
            var newFieldDeclaration = fieldDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
                                      .WithTrailingTrivia(fieldDeclaration.GetTrailingTrivia())
                                      .WithLeadingTrivia(fieldDeclaration.GetLeadingTrivia())
                                      .WithAdditionalAnnotations(Formatter.Annotation);
            var newRoot = root.ReplaceNode(fieldDeclaration, newFieldDeclaration);

            return(newRoot);
        }
            public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
            {
                if (node.Modifiers.Any(SyntaxKind.ReadOnlyKeyword))
                {
                    return(node);
                }
                if (semanticModel.GetTypeInfo(node.Declaration.Type).Type.Name != "Bindable")
                {
                    return(node);
                }
                if (!node.Declaration.Type.IsKind(SyntaxKind.GenericName))
                {
                    return(node);
                }

                if (semanticModel.GetTypeInfo(node.Declaration.Type).Type.ContainingNamespace.Name != "Zebble" &&
                    semanticModel.GetTypeInfo(node.Declaration.Type).Type.ContainingNamespace.Name != "Olive")
                {
                    return(node);
                }

                if (isReportOnlyMode)
                {
                    var lineSpan = node.GetFileLinePosSpan();

                    AddReport(new ChangesReport(node)
                    {
                        LineNumber = lineSpan.StartLinePosition.Line,
                        Column     = lineSpan.StartLinePosition.Character,
                        Message    = "Should Add Readonly Modifier",
                        Generator  = nameof(ZebbleCleaner)
                    });

                    return(node);
                }

                return(node.AddModifiers(
                           SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)
                           .WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia(" "))));
            }
Example #4
0
 public static FieldDeclarationSyntax AddModifiers(this FieldDeclarationSyntax syntax, params SyntaxKind[] modifier)
 {
     return(syntax.AddModifiers(modifier.Select(Token).ToArray()));
 }
Example #5
0
 public static FieldDeclarationSyntax WithModifiers(this FieldDeclarationSyntax node, params SyntaxKind[] modifiers)
 {
     return(node.AddModifiers(CreateModifiers(modifiers)));
 }