public override SyntaxNode VisitCompilationUnit(CompilationUnitSyntax node) { if (!node.Usings.Any(u => NameUtilities.FullName(u.Name).StartsWith("Xunit"))) { // This doesn't have any Xunit using declarations, it's probably some other framework return(node); } _fileHasFacts = false; node = (CompilationUnitSyntax)base.VisitCompilationUnit(node); var firstTrivia = node?.Usings.FirstOrDefault()?.GetLeadingTrivia() ?? TriviaList(); var usingDirectives = node.Usings.Where(u => !NameUtilities.IsName(u.Name, "Xunit") && !NameUtilities.IsName(u.Name, "Xunit.Abstractions")).ToList(); if (_fileHasFacts) { UsingDirectiveSyntax nUnitUsing = UsingDirective(ParseName("NUnit.Framework").WithLeadingTrivia(Space)); usingDirectives.Add(nUnitUsing); usingDirectives = SortUsing(usingDirectives); } if (usingDirectives.Count != 0) { usingDirectives[0] = usingDirectives[0].WithLeadingTrivia(firstTrivia); } node = node.WithUsings(List(usingDirectives)); return(node); }
private List <UsingDirectiveSyntax> SortUsing(IReadOnlyList <UsingDirectiveSyntax> usingDirectives) { int NameSortingComparison(UsingDirectiveSyntax a, UsingDirectiveSyntax b) { string aLeft = NameUtilities.LeftMostPart(a.Name); string bLeft = NameUtilities.LeftMostPart(b.Name); string aFull = NameUtilities.FullName(a.Name); string bBull = NameUtilities.FullName(b.Name); if (aLeft == "System") { if (bLeft == "System") { return(StringComparer.Ordinal.Compare(aFull, bBull)); } return(-1); } if (bLeft == "System") { return(1); } return(StringComparer.Ordinal.Compare(aFull, bBull)); } List <UsingDirectiveSyntax> normal = usingDirectives .Where(u => u.Alias == null && u.StaticKeyword == default) .Select(u => u.WithoutTrivia().WithTrailingTrivia(CarriageReturnLineFeed)) .ToList(); normal.Sort(NameSortingComparison); List <UsingDirectiveSyntax> aliases = usingDirectives.Where(u => u.Alias != null) .Select(u => u.WithoutTrivia().WithTrailingTrivia(CarriageReturnLineFeed)) .ToList(); aliases.Sort(NameSortingComparison); List <UsingDirectiveSyntax> statics = usingDirectives.Where(u => u.StaticKeyword != default) .Select(u => u.WithoutTrivia().WithTrailingTrivia(CarriageReturnLineFeed)) .ToList(); statics.Sort(NameSortingComparison); if (normal.Count != 0 && (aliases.Count != 0 || statics.Count != 0)) { normal[^ 1] = normal[^ 1].WithTrailingTrivia(CarriageReturnLineFeed, CarriageReturnLineFeed);
public static bool IsAssert(NameSyntax name) { string fullName = NameUtilities.FullName(name); return(fullName == "Assert" || fullName == "Xunit.Assert"); }