Skip to content

Commit 2320a67

Browse files
authored
Filter all html diagnostics out if they're reported in a comment (#12002)
Noticed while I was testing an old formatting issue, that we would report diagnostics when Razor comments appear in CSS tags. In general, there is no reason to report a diagnostic in a comment.
2 parents 3e772db + f1153bf commit 2320a67

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Diagnostics/RazorTranslateDiagnosticsService.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private static LspDiagnostic[] FilterHTMLDiagnostics(
102102

103103
var filteredDiagnostics = unmappedDiagnostics
104104
.Where(d =>
105+
!InRazorComment(d, sourceText, syntaxTree) &&
105106
!InCSharpLiteral(d, sourceText, syntaxTree) &&
106107
!InAttributeContainingCSharp(d, sourceText, syntaxTree, processedAttributes) &&
107108
!AppliesToTagHelperTagName(d, sourceText, syntaxTree) &&
@@ -146,6 +147,18 @@ private LspDiagnostic[] MapDiagnostics(
146147
return mappedDiagnostics.ToArray();
147148
}
148149

150+
private static bool InRazorComment(
151+
LspDiagnostic d,
152+
SourceText sourceText,
153+
RazorSyntaxTree syntaxTree)
154+
{
155+
// If the diagnostic is within a Razor comment block, we don't want to show it.
156+
// Razor comments are not part of the Html document, so diagnostics within them stem from misinterpretation
157+
// of the "~" and comments that are generated by the compiler.
158+
return d.Range is not null &&
159+
syntaxTree.Root.FindNode(sourceText.GetTextSpan(d.Range), getInnermostNodeForTie: true) is RazorCommentBlockSyntax;
160+
}
161+
149162
private static bool InCSharpLiteral(
150163
LspDiagnostic d,
151164
SourceText sourceText,
@@ -496,7 +509,7 @@ private bool TryGetOriginalDiagnosticRange(LspDiagnostic diagnostic, RazorCodeDo
496509
// for mapping when a user reports not seeing an error they thought they should
497510
if (diagnostic.Severity == LspDiagnosticSeverity.Error)
498511
{
499-
this._logger.LogWarning($"Dropping diagnostic {diagnostic.Code}:{diagnostic.Message} at csharp range {diagnostic.Range}");
512+
_logger.LogWarning($"Dropping diagnostic {diagnostic.Code}:{diagnostic.Message} at csharp range {diagnostic.Range}");
500513
}
501514

502515
return false;

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostDocumentPullDiagnosticsTest.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,60 @@ public Task FilterEscapedAtFromCss()
109109
}]);
110110
}
111111

112+
[Fact]
113+
public Task FilterRazorCommentsFromCss()
114+
{
115+
TestCode input = """
116+
<div>
117+
118+
<style>
119+
@* This is a Razor comment *@
120+
</style>
121+
122+
</div>
123+
""";
124+
125+
return VerifyDiagnosticsAsync(input,
126+
htmlResponse: [new VSInternalDiagnosticReport
127+
{
128+
Diagnostics =
129+
[
130+
new Diagnostic
131+
{
132+
Code = CSSErrorCodes.MissingSelectorBeforeCombinatorCode,
133+
Range = SourceText.From(input.Text).GetRange(new TextSpan(input.Text.IndexOf("@*"), 1))
134+
}
135+
]
136+
}]);
137+
}
138+
139+
[Fact]
140+
public Task FilterRazorCommentsFromCss_Inside()
141+
{
142+
TestCode input = """
143+
<div>
144+
145+
<style>
146+
@* This is a Razor comment *@
147+
</style>
148+
149+
</div>
150+
""";
151+
152+
return VerifyDiagnosticsAsync(input,
153+
htmlResponse: [new VSInternalDiagnosticReport
154+
{
155+
Diagnostics =
156+
[
157+
new Diagnostic
158+
{
159+
Code = CSSErrorCodes.MissingSelectorBeforeCombinatorCode,
160+
Range = SourceText.From(input.Text).GetRange(new TextSpan(input.Text.IndexOf("Ra"), 1))
161+
}
162+
]
163+
}]);
164+
}
165+
112166
[Fact]
113167
public Task CombinedAndNestedDiagnostics()
114168
=> VerifyDiagnosticsAsync("""
@@ -170,6 +224,15 @@ private async Task VerifyDiagnosticsAsync(TestCode input, VSInternalDiagnosticRe
170224
Diagnostics = await endpoint.GetTestAccessor().HandleRequestAsync(document, DisposalToken)
171225
}];
172226

227+
Assert.NotNull(result);
228+
229+
if (result is [{ Diagnostics: null }])
230+
{
231+
// No diagnostics found, make sure none were expected
232+
AssertEx.Equal(input.OriginalInput, input.Text);
233+
return;
234+
}
235+
173236
var markers = result!.SelectMany(d => d.Diagnostics.AssumeNotNull()).SelectMany(d =>
174237
new[] {
175238
(index: inputText.GetTextSpan(d.Range).Start, text: $"{{|{d.Code!.Value.Second}:"),

0 commit comments

Comments
 (0)