Skip to content

Commit c5faa2a

Browse files
committed
Avoid suggesting a code fix for unknown entities
1 parent 03003e1 commit c5faa2a

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

DocumentationAnalyzers/DocumentationAnalyzers.CodeFixes/StyleRules/DOC103CodeFixProvider.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,33 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4242
return;
4343
}
4444

45+
string newText = token.ValueText;
46+
if (newText == token.Text)
47+
{
48+
// The entity is not recognized. Try decoding as an HTML entity.
49+
newText = WebUtility.HtmlDecode(token.Text);
50+
}
51+
52+
if (newText == token.Text)
53+
{
54+
// Unknown entity
55+
continue;
56+
}
57+
4558
context.RegisterCodeFix(
4659
CodeAction.Create(
4760
StyleResources.DOC103CodeFix,
48-
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
61+
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, newText, cancellationToken),
4962
nameof(DOC103CodeFixProvider)),
5063
diagnostic);
5164
}
5265
}
5366

54-
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
67+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, string newText, CancellationToken cancellationToken)
5568
{
5669
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5770
SyntaxToken token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
5871

59-
string newText = token.ValueText;
60-
if (newText == token.Text)
61-
{
62-
// The entity is not recognized. Try decoding as an HTML entity.
63-
newText = WebUtility.HtmlDecode(token.Text);
64-
}
65-
66-
if (newText == token.Text)
67-
{
68-
// Unknown entity
69-
return document;
70-
}
71-
7272
var newToken = SyntaxFactory.Token(token.LeadingTrivia, SyntaxKind.XmlTextLiteralToken, newText, newText, token.TrailingTrivia);
7373

7474
return document.WithSyntaxRoot(root.ReplaceToken(token, newToken));

DocumentationAnalyzers/DocumentationAnalyzers.Test/StyleRules/DOC103UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ class TestClass
111111
}.RunAsync();
112112
}
113113

114+
[Fact]
115+
public async Task TestUnknownEntityNotReplacedAsync()
116+
{
117+
var testCode = @"
118+
/// <summary>
119+
/// Unknown entity &myEntity;.
120+
/// </summary>
121+
class TestClass
122+
{
123+
}
124+
";
125+
var fixedCode = testCode;
126+
127+
await new CSharpCodeFixTest<DOC103UseUnicodeCharacters, DOC103CodeFixProvider, XUnitVerifier>
128+
{
129+
TestState =
130+
{
131+
Sources = { testCode },
132+
ExpectedDiagnostics = { DiagnosticResult.CompilerWarning("CS1570").WithSpan(3, 20, 3, 20).WithMessage("XML comment has badly formed XML -- 'Reference to undefined entity 'myEntity'.'") },
133+
},
134+
FixedState =
135+
{
136+
Sources = { fixedCode },
137+
InheritanceMode = StateInheritanceMode.AutoInheritAll,
138+
},
139+
CompilerDiagnostics = CompilerDiagnostics.Warnings,
140+
}.RunAsync();
141+
}
142+
114143
[Fact]
115144
public async Task TestHtmlEntityReplacementInInvalidXmlAsync()
116145
{

0 commit comments

Comments
 (0)