Skip to content

Commit f3ddc0e

Browse files
committed
Merge pull request #1536 from peteri/FixForMissingXmlEncodingInFileHeaderLogic
Added logic to escape characters in XmlComments in the fixer.
2 parents 0153640 + 6c65cd9 commit f3ddc0e

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,51 @@ namespace Bar
263263
* <summary>This is a test file.</summary>
264264
*/
265265
266+
namespace Bar
267+
{
268+
}
269+
";
270+
271+
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
272+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
273+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
274+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
275+
}
276+
277+
/// <summary>
278+
/// Verifies that a multi line file header containing characters that need xml escaping gets fixed correctly.
279+
/// </summary>
280+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
281+
[Fact]
282+
public async Task TestFileHeaderWithMultiLineCommentAndFieldsNeedingXmlEscapingAsync()
283+
{
284+
this.multiLineSettings = @"
285+
{
286+
""settings"": {
287+
""documentationRules"": {
288+
""companyName"": ""Foo & Bar \""quote\"" Corp"",
289+
""copyrightText"": ""copyright (c) {companyName}. All rights reserved.\n\nLine #3""
290+
}
291+
}
292+
}
293+
";
294+
295+
var testCode = @"// <author>FooCorp</author>
296+
// <summary>Foo &amp; Bar Corp Bar Class</summary>
297+
298+
namespace Bar
299+
{
300+
}
301+
";
302+
303+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""Foo &amp; Bar &quot;quote&quot; Corp"">
304+
// copyright (c) Foo &amp; Bar ""quote"" Corp. All rights reserved.
305+
//
306+
// Line #3
307+
// </copyright>
308+
// <author>FooCorp</author>
309+
// <summary>Foo &amp; Bar Corp Bar Class</summary>
310+
266311
namespace Bar
267312
{
268313
}

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/FileHeaderCodeFixProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.DocumentationRules
1010
using System.Text;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
using System.Xml.Linq;
1314
using Microsoft.CodeAnalysis;
1415
using Microsoft.CodeAnalysis.CodeActions;
1516
using Microsoft.CodeAnalysis.CodeFixes;
@@ -352,9 +353,13 @@ private static SyntaxTriviaList CreateNewHeader(string prefixWithLeadingSpaces,
352353

353354
private static string WrapInXmlComment(string prefixWithLeadingSpaces, string copyrightText, string filename, StyleCopSettings settings, string newLineText)
354355
{
356+
string encodedFilename = new XAttribute("t", filename).ToString().Substring(2).Trim('"');
357+
string encodedCompanyName = new XAttribute("t", settings.DocumentationRules.CompanyName).ToString().Substring(2).Trim('"');
358+
string encodedCopyrightText = new XText(copyrightText).ToString();
359+
355360
return
356-
$"{prefixWithLeadingSpaces} <copyright file=\"{filename}\" company=\"{settings.DocumentationRules.CompanyName}\">" + newLineText
357-
+ copyrightText + newLineText
361+
$"{prefixWithLeadingSpaces} <copyright file=\"{encodedFilename}\" company=\"{encodedCompanyName}\">" + newLineText
362+
+ encodedCopyrightText + newLineText
358363
+ prefixWithLeadingSpaces + " </copyright>";
359364
}
360365

0 commit comments

Comments
 (0)