Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 5f5ff52

Browse files
committed
Fix issue if a file leading trivia starts with the copyright header
(previously had to be equal to the header).
1 parent 9c46f31 commit 5f5ff52

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CopyrightHeaderRuleTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ class C
157157

158158
var expected = @"// test
159159
160+
class C
161+
{
162+
}";
163+
Verify(source, expected);
164+
}
165+
166+
[Fact]
167+
public void CSharpHeaderBeginsWithTargetHeader()
168+
{
169+
_options.CopyrightHeader = ImmutableArray.Create("// test", "// test2");
170+
var source = @"// test
171+
// test2
172+
// file summary
173+
174+
class C
175+
{
176+
}";
177+
178+
var expected = @"// test
179+
// test2
180+
// file summary
181+
160182
class C
161183
{
162184
}";

src/Microsoft.DotNet.CodeFormatting/Rules/CopyrightHeaderRule.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISynta
1818
private abstract class CommonRule
1919
{
2020
/// <summary>
21-
/// This is the normalized copyright header that has no comment delimeters.
21+
/// This is the normalized copyright header that has no comment delimiters.
2222
/// </summary>
2323
private readonly ImmutableArray<string> _header;
2424

@@ -43,7 +43,18 @@ internal SyntaxNode Process(SyntaxNode syntaxNode)
4343
private bool HasCopyrightHeader(SyntaxNode syntaxNode)
4444
{
4545
var existingHeader = GetExistingHeader(syntaxNode.GetLeadingTrivia());
46-
return _header.SequenceEqual(existingHeader);
46+
return SequnceStartsWith(_header, existingHeader);
47+
}
48+
49+
private bool SequnceStartsWith(ImmutableArray<string> header, List<string> existingHeader)
50+
{
51+
// Only try if the existing header is at least as long as the new copyright header
52+
if (existingHeader.Count >= header.Count())
53+
{
54+
return !header.Where((headerLine, i) => existingHeader[i] != headerLine).Any();
55+
}
56+
57+
return false;
4758
}
4859

4960
private SyntaxNode AddCopyrightHeader(SyntaxNode syntaxNode)

0 commit comments

Comments
 (0)