Skip to content

Commit 113a114

Browse files
committed
Merge pull request #1544 from vweijsters/fix-1528
2 parents b977a00 + a80a6bc commit 113a114

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)
@@ -533,20 +533,50 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
533533
{
534534
var currentUsing = usingsList[i];
535535

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

538538
// preserve leading trivia (excluding directive trivia), indenting each line as appropriate
539539
var newLeadingTrivia = currentUsing
540540
.GetLeadingTrivia()
541-
.WithoutLeadingWhitespace()
542-
.Where(tr => !tr.IsDirective)
541+
.Where(tr => !tr.IsDirective && !tr.IsKind(SyntaxKind.DisabledTextTrivia))
543542
.ToList();
544543

545544
if (i == 0)
546545
{
547546
newLeadingTrivia = StripFileHeader(newLeadingTrivia);
548547
}
549548

549+
// strip any leading whitespace on each line (and also all blank lines)
550+
var k = 0;
551+
var startOfLine = true;
552+
while (k < newLeadingTrivia.Count)
553+
{
554+
switch (newLeadingTrivia[k].Kind())
555+
{
556+
case SyntaxKind.WhitespaceTrivia:
557+
newLeadingTrivia.RemoveAt(k);
558+
break;
559+
560+
case SyntaxKind.EndOfLineTrivia:
561+
if (startOfLine)
562+
{
563+
newLeadingTrivia.RemoveAt(k);
564+
}
565+
else
566+
{
567+
startOfLine = true;
568+
k++;
569+
}
570+
571+
break;
572+
573+
default:
574+
startOfLine = newLeadingTrivia[k].IsDirective;
575+
k++;
576+
break;
577+
}
578+
}
579+
550580
for (var j = newLeadingTrivia.Count - 1; j >= 0; j--)
551581
{
552582
if (newLeadingTrivia[j].IsKind(SyntaxKind.EndOfLineTrivia))

0 commit comments

Comments
 (0)