Example #1
0
 public static void InitializedWithOther(MemberDeclarationSyntax x, MemberDeclarationSyntax y)
 {
     Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
 }
 public static void MemberDeclarationComparerCompare(MethodDeclarationSyntax x, MethodDeclarationSyntax y)
 {
     Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
 }
 public void MemberDeclarationComparerCompare(PropertyDeclarationSyntax x, PropertyDeclarationSyntax y)
 {
     Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
 }
Example #4
0
 public static void AttachedProperty(FieldDeclarationSyntax x, FieldDeclarationSyntax y)
 {
     Assert.AreEqual(-1, FieldDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, FieldDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, FieldDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, FieldDeclarationComparer.Compare(y, y));
     Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
 }
Example #5
0
 public static void BackingField(FieldDeclarationSyntax x, FieldDeclarationSyntax y)
 {
     Assert.AreEqual(-1, FieldDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, FieldDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, FieldDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, FieldDeclarationComparer.Compare(y, y));
     Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
     Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
     Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
 }
        public static void CompareAttachedPropertyMethods(MethodDeclarationSyntax x, MethodDeclarationSyntax y)
        {
            Assert.AreEqual(-1, MethodDeclarationComparer.Compare(x, y));
            Assert.AreEqual(1, MethodDeclarationComparer.Compare(y, x));
            Assert.AreEqual(0, MethodDeclarationComparer.Compare(x, x));
            Assert.AreEqual(0, MethodDeclarationComparer.Compare(y, y));

            Assert.AreEqual(-1, MemberDeclarationComparer.Compare(x, y));
            Assert.AreEqual(1, MemberDeclarationComparer.Compare(y, x));
            Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
            Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
        }
        private static SyntaxList <MemberDeclarationSyntax> WithMoved(SyntaxList <MemberDeclarationSyntax> members, MemberDeclarationSyntax member)
        {
            members = members.Remove(member);
            for (var i = 0; i < members.Count; i++)
            {
                var current = members[i];
                if (MemberDeclarationComparer.Compare(member, current) < 0)
                {
                    return(RemoveLeadingEndOfLine(members.Insert(i, UpdateLineFeed(member))));
                }
            }

            return(RemoveLeadingEndOfLine(members.Add(UpdateLineFeed(member))));
        }
Example #8
0
        public static void InitializedWithOther()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
namespace N
{
    class C
    {
        public static int PublicStatic1 { get; } = PublicStatic2;
        public static int PublicStatic2 { get; } = 3;
    }
}");
            var x          = syntaxTree.FindPropertyDeclaration("public static int PublicStatic1 { get; } = PublicStatic2");
            var y          = syntaxTree.FindPropertyDeclaration("public static int PublicStatic2 { get; } = 3");

            Assert.AreEqual(1, MemberDeclarationComparer.Compare(x, y));
            Assert.AreEqual(-1, MemberDeclarationComparer.Compare(y, x));
            Assert.AreEqual(0, MemberDeclarationComparer.Compare(x, x));
            Assert.AreEqual(0, MemberDeclarationComparer.Compare(y, y));
        }
Example #9
0
        private static TypeDeclarationSyntax AddSorted(
            SyntaxGenerator generator,
            TypeDeclarationSyntax containingType,
            FieldDeclarationSyntax field)
        {
            foreach (var member in containingType.Members)
            {
                if (member.IsEquivalentTo(field))
                {
                    return(containingType);
                }

                if (MemberDeclarationComparer.Compare(field, member) < 0)
                {
                    return((TypeDeclarationSyntax)generator.InsertNodesBefore(containingType, member, new[] { field }));
                }
            }

            return((TypeDeclarationSyntax)generator.AddMembers(containingType, field));
        }
Example #10
0
        private static void Handle(SyntaxNodeAnalysisContext context)
        {
            if (context.IsExcludedFromAnalysis())
            {
                return;
            }

            if (context.Node is BasePropertyDeclarationSyntax propertyDeclaration &&
                propertyDeclaration.TryFirstAncestor <TypeDeclarationSyntax>(out var typeDeclaration))
            {
                var index = typeDeclaration.Members.IndexOf(propertyDeclaration);
                if (typeDeclaration.Members.TryElementAt(index + 1, out var after) &&
                    (after is PropertyDeclarationSyntax || after is IndexerDeclarationSyntax))
                {
                    if (MemberDeclarationComparer.Compare(propertyDeclaration, after) > 0)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, context.Node.GetLocation()));
                    }
                }
            }
        }