Exemplo n.º 1
0
        private static void CheckUsingDeclarations(SyntaxNodeAnalysisContext context, SyntaxList <UsingDirectiveSyntax> usingDirectives)
        {
            UsingDirectiveSyntax lastStaticUsingDirective = null;

            foreach (var usingDirective in usingDirectives)
            {
                if (usingDirective.IsPrecededByPreprocessorDirective())
                {
                    lastStaticUsingDirective = null;
                }

                if (usingDirective.StaticKeyword.IsKind(SyntaxKind.StaticKeyword))
                {
                    if (lastStaticUsingDirective != null)
                    {
                        var firstName  = lastStaticUsingDirective.Name;
                        var secondName = usingDirective.Name;

                        if (NameSyntaxHelpers.Compare(firstName, secondName) > 0)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(
                                                         Descriptor,
                                                         lastStaticUsingDirective.GetLocation(),
                                                         new[] { firstName.ToNormalizedString(), secondName.ToNormalizedString() }));
                            return;
                        }
                    }

                    lastStaticUsingDirective = usingDirective;
                }
            }
        }
Exemplo n.º 2
0
            private int CompareUsings(UsingDirectiveSyntax left, UsingDirectiveSyntax right)
            {
                if ((left.Alias != null) && (right.Alias != null))
                {
                    return(NameSyntaxHelpers.Compare(left.Alias.Name, right.Alias.Name));
                }

                return(NameSyntaxHelpers.Compare(left.Name, right.Name));
            }
        private static void CheckIncorrectlyOrderedUsingsAndReportDiagnostic(SyntaxNodeAnalysisContext context, IEnumerable <UsingDirectiveSyntax> usings)
        {
            UsingDirectiveSyntax previousUsingDirective = null;

            foreach (var directive in usings)
            {
                if (previousUsingDirective != null)
                {
                    if (NameSyntaxHelpers.Compare(previousUsingDirective.Name, directive.Name) > 0)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, previousUsingDirective.GetLocation()));
                    }
                }

                previousUsingDirective = directive;
            }
        }