Skip to content

Commit 172bc26

Browse files
committed
Merge pull request #1735 from vweijsters/fix-1733
Fixed issue in UsingCodeFixProvider where a file header followed by …
2 parents 38f943e + 59ea485 commit 172bc26

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ private static SyntaxNode ReAddFileHeader(SyntaxNode syntaxRoot, SyntaxNode newS
154154
}
155155

156156
var newFirstToken = newSyntaxRoot.GetFirstToken();
157-
return newSyntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader));
157+
var newLeadingTrivia = newFirstToken.LeadingTrivia.InsertRange(0, fileHeader);
158+
return newSyntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(newLeadingTrivia));
158159
}
159160

160161
private static int CountNamespaces(SyntaxList<MemberDeclarationSyntax> members)

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,98 @@ public async Task VerifyCodefixForElsePartOfDirectiveTriviaAsync()
477477
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
478478
}
479479

480+
/// <summary>
481+
/// Verifies that the code fix will handle using statements with directive trivia outside of namespaces
482+
/// This is a regression test for #1733
483+
/// </summary>
484+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
485+
[Fact]
486+
public async Task VerifyCodefixForDirectiveTriviaOutsideOfNamespacesAsync()
487+
{
488+
var testCode = @"// <copyright file=""Program.cs"" company=""PlaceholderCompany"" >
489+
// Copyright (c) PlaceholderCompany. All rights reserved.
490+
// </copyright>
491+
492+
#if DEBUG
493+
using Fish;
494+
#else
495+
using Fish.Face;
496+
#endif
497+
using System.Text;
498+
using System;
499+
500+
namespace StyleCopBugRepro
501+
{
502+
class Program
503+
{
504+
static void Main(string[] args)
505+
{
506+
Int32 q;
507+
Haddock h;
508+
StringBuilder sb;
509+
}
510+
}
511+
}
512+
513+
namespace Fish
514+
{
515+
public class Haddock { }
516+
517+
namespace Face
518+
{
519+
public class Haddock { }
520+
}
521+
}
522+
";
523+
524+
var fixedTestCode = @"// <copyright file=""Program.cs"" company=""PlaceholderCompany"" >
525+
// Copyright (c) PlaceholderCompany. All rights reserved.
526+
// </copyright>
527+
528+
#if DEBUG
529+
using Fish;
530+
#else
531+
using Fish.Face;
532+
#endif
533+
using System;
534+
using System.Text;
535+
536+
namespace StyleCopBugRepro
537+
{
538+
class Program
539+
{
540+
static void Main(string[] args)
541+
{
542+
Int32 q;
543+
Haddock h;
544+
StringBuilder sb;
545+
}
546+
}
547+
}
548+
549+
namespace Fish
550+
{
551+
public class Haddock { }
552+
553+
namespace Face
554+
{
555+
public class Haddock { }
556+
}
557+
}
558+
";
559+
560+
DiagnosticResult[] expected =
561+
{
562+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedWithinNamespace.DiagnosticId).WithLocation(8, 1),
563+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedWithinNamespace.DiagnosticId).WithLocation(10, 1),
564+
this.CSharpDiagnostic(SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.DiagnosticId).WithLocation(10, 1),
565+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedWithinNamespace.DiagnosticId).WithLocation(11, 1)
566+
};
567+
568+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
569+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
570+
}
571+
480572
/// <inheritdoc/>
481573
protected override IEnumerable<string> GetDisabledDiagnostics()
482574
{

0 commit comments

Comments
 (0)