Skip to content

Commit dde9766

Browse files
committed
Fixed multiline copyright comments.
1 parent c9fb5f0 commit dde9766

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1634UnitTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1414
/// </summary>
1515
public class SA1634UnitTests : FileHeaderTestBase
1616
{
17+
private string multiLineSettings;
18+
1719
/// <summary>
1820
/// Verifies that a file header without a copyright element will produce the expected diagnostic (none for the default case)
1921
/// </summary>
@@ -94,10 +96,46 @@ namespace Bar
9496
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
9597
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
9698
}
99+
/// <summary>
100+
/// Verifies that a file header with a copyright element will not produce a diagnostic message.
101+
/// </summary>
102+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
103+
[Fact]
104+
public async Task TestValidMultiLineFileHeaderWithCopyrightLastAsync()
105+
{
106+
var testCode = @"// <author>
107+
// John Doe
108+
// </author>
109+
// <copyright file=""Test0.cs"" company=""FooCorp"">
110+
// Copyright (c) FooCorp. All rights reserved.
111+
// Licence is FooBar MIT.
112+
// </copyright>
113+
114+
namespace Bar
115+
{
116+
}
117+
";
118+
this.multiLineSettings = @"
119+
{
120+
""settings"": {
121+
""documentationRules"": {
122+
""companyName"": ""FooCorp"",
123+
""copyrightText"": ""Copyright (c) FooCorp. All rights reserved.\nLicence is FooBar MIT.""
124+
}
125+
}
126+
}
127+
";
128+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
129+
}
97130

98131
protected override CodeFixProvider GetCSharpCodeFixProvider()
99132
{
100133
throw new System.NotImplementedException();
101134
}
135+
136+
protected override string GetSettings()
137+
{
138+
return this.multiLineSettings ?? base.GetSettings();
139+
}
102140
}
103141
}

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/FileHeaderAnalyzers.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,15 @@ private static void CheckCopyrightText(SyntaxTreeAnalysisContext context, Compil
315315
return;
316316
}
317317

318-
if (string.Equals(settings.DocumentationRules.CopyrightText, DocumentationSettings.DefaultCopyrightText, StringComparison.OrdinalIgnoreCase))
318+
var settingsCopyrightText = settings.DocumentationRules.CopyrightText;
319+
if (string.Equals(settingsCopyrightText, DocumentationSettings.DefaultCopyrightText, StringComparison.OrdinalIgnoreCase))
319320
{
320321
// The copyright text is meaningless until the company name is configured by the user.
321322
return;
322323
}
323324

324-
if (string.CompareOrdinal(copyrightText.Trim(' ', '\r', '\n'), settings.DocumentationRules.CopyrightText) != 0)
325+
// Remove any leading or trailing spaces and remove the extra space put in as a seperator.
326+
if (string.CompareOrdinal(copyrightText.Trim(' ', '\r', '\n').Replace("\n ","\n"), settingsCopyrightText.Trim(' ', '\r', '\n')) != 0)
325327
{
326328
var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
327329
context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, location));

0 commit comments

Comments
 (0)