From df7ec0ec6ad693b10f14cc5ccd272e63b14f8016 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Sat, 21 Sep 2024 14:09:38 +0200 Subject: [PATCH] Escape special chars (RCS1181) (#1534) --- ChangeLog.md | 1 + ...nvertCommentToDocumentationCommentTests.cs | 31 +++++++++++++++++++ ...ommentToDocumentationCommentRefactoring.cs | 6 +++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 481a33d5b7..a3bf6d322e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1521)) - Fix analyzer [RCS1181](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181) ([PR](https://github.com/dotnet/roslynator/pull/1526)) - Fix analyzer [RCS0005](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0005) ([PR](https://github.com/dotnet/roslynator/pull/1533)) +- Fix analyzer [RCS1181](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181) ([PR](https://github.com/dotnet/roslynator/pull/1534)) ## [4.12.5] - 2024-09-13 diff --git a/src/Tests/Analyzers.Tests/RCS1181ConvertCommentToDocumentationCommentTests.cs b/src/Tests/Analyzers.Tests/RCS1181ConvertCommentToDocumentationCommentTests.cs index 161872caca..4efc5ad076 100644 --- a/src/Tests/Analyzers.Tests/RCS1181ConvertCommentToDocumentationCommentTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1181ConvertCommentToDocumentationCommentTests.cs @@ -196,6 +196,37 @@ void M() "); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConvertCommentToDocumentationComment)] + public async Task Test_CommentContainsXmlSpecialChars() + { + await VerifyDiagnosticAndFixAsync(""" +namespace N +{ + /// + /// x + /// + class C + { + int P { get; set; } [|// Must be >= 0 & <= 5.|] + } +} +""", """ +namespace N +{ + /// + /// x + /// + class C + { + /// + /// Must be >= 0 & <= 5. + /// + int P { get; set; } + } +} +"""); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConvertCommentToDocumentationComment)] public async Task TestNoDiagnostic_DocumentationComment() { diff --git a/src/Workspaces.Common/CSharp/Refactorings/ConvertCommentToDocumentationCommentRefactoring.cs b/src/Workspaces.Common/CSharp/Refactorings/ConvertCommentToDocumentationCommentRefactoring.cs index fb3997155b..01e9f7eea5 100644 --- a/src/Workspaces.Common/CSharp/Refactorings/ConvertCommentToDocumentationCommentRefactoring.cs +++ b/src/Workspaces.Common/CSharp/Refactorings/ConvertCommentToDocumentationCommentRefactoring.cs @@ -3,6 +3,7 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Linq; +using System.Net; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -61,7 +62,10 @@ public static Task RefactorAsync( Debug.Assert(trailingTrivia.Contains(trivia)); - comments = ImmutableArray.Create(_leadingSlashesRegex.Replace(trivia.ToString(), "")); + string commentText = _leadingSlashesRegex.Replace(trivia.ToString(), ""); + commentText = WebUtility.HtmlEncode(commentText); + + comments = ImmutableArray.Create(commentText); SyntaxToken newToken = token.WithTrailingTrivia(trailingTrivia.Skip(trailingTrivia.IndexOf(trivia) + 1));