Skip to content

Commit a80a6bc

Browse files
committed
Implemented fix
1 parent 1c1d3e9 commit a80a6bc

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderUnitTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,42 @@ namespace TestNamespace2
441441
Assert.Empty(offeredFixes);
442442
}
443443

444+
/// <summary>
445+
/// Verifies that the code fix will handle using statements in the else part of a #if directive trivia.
446+
/// This is a regression test for #1528
447+
/// </summary>
448+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
449+
[Fact]
450+
public async Task VerifyCodefixForElsePartOfDirectiveTriviaAsync()
451+
{
452+
var testCode = @"namespace NamespaceName
453+
{
454+
#if false
455+
using System.Runtime.CompilerServices;
456+
#else
457+
using System.Collections.Generic;
458+
using System.Collections.Concurrent;
459+
#endif
460+
}
461+
";
462+
463+
var fixedTestCode = @"namespace NamespaceName
464+
{
465+
#if false
466+
using System.Runtime.CompilerServices;
467+
#else
468+
using System.Collections.Concurrent;
469+
using System.Collections.Generic;
470+
#endif
471+
}
472+
";
473+
var expected = this.CSharpDiagnostic(SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.DiagnosticId).WithLocation(6, 5);
474+
475+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
476+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
477+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
478+
}
479+
444480
/// <inheritdoc/>
445481
protected override IEnumerable<string> GetDisabledDiagnostics()
446482
{

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/UsingCodeFixProvider.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5353
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
5454
var compilationUnit = (CompilationUnitSyntax)syntaxRoot;
5555

56-
foreach (Diagnostic diagnostic in context.Diagnostics.Where(d => FixableDiagnostics.Contains(d.Id)))
56+
foreach (Diagnostic diagnostic in context.Diagnostics)
5757
{
5858
// do not offer a code fix for SA1200 when there are multiple namespaces in the source file
5959
if ((diagnostic.Id == SA1200UsingDirectivesMustBePlacedWithinNamespace.DiagnosticId)
@@ -525,20 +525,50 @@ private static List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSynt
525525
{
526526
var currentUsing = usingsList[i];
527527

528-
triviaToMove.AddRange(currentUsing.GetLeadingTrivia().Where(tr => tr.IsDirective));
528+
triviaToMove.AddRange(currentUsing.GetLeadingTrivia().Where(tr => tr.IsDirective || tr.IsKind(SyntaxKind.DisabledTextTrivia)));
529529

530530
// preserve leading trivia (excluding directive trivia), indenting each line as appropriate
531531
var newLeadingTrivia = currentUsing
532532
.GetLeadingTrivia()
533-
.WithoutLeadingWhitespace()
534-
.Where(tr => !tr.IsDirective)
533+
.Where(tr => !tr.IsDirective && !tr.IsKind(SyntaxKind.DisabledTextTrivia))
535534
.ToList();
536535

537536
if (i == 0)
538537
{
539538
newLeadingTrivia = StripFileHeader(newLeadingTrivia);
540539
}
541540

541+
// strip any leading whitespace on each line (and also all blank lines)
542+
var k = 0;
543+
var startOfLine = true;
544+
while (k < newLeadingTrivia.Count)
545+
{
546+
switch (newLeadingTrivia[k].Kind())
547+
{
548+
case SyntaxKind.WhitespaceTrivia:
549+
newLeadingTrivia.RemoveAt(k);
550+
break;
551+
552+
case SyntaxKind.EndOfLineTrivia:
553+
if (startOfLine)
554+
{
555+
newLeadingTrivia.RemoveAt(k);
556+
}
557+
else
558+
{
559+
startOfLine = true;
560+
k++;
561+
}
562+
563+
break;
564+
565+
default:
566+
startOfLine = newLeadingTrivia[k].IsDirective;
567+
k++;
568+
break;
569+
}
570+
}
571+
542572
for (var j = newLeadingTrivia.Count - 1; j >= 0; j--)
543573
{
544574
if (newLeadingTrivia[j].IsKind(SyntaxKind.EndOfLineTrivia))

0 commit comments

Comments
 (0)