Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out CSS002 when it appears in an "@@" #11313

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.CodeAnalysis.Razor.Diagnostics;
// Note: This type should be kept in sync with WTE's ErrorCodes.cs
internal static class CSSErrorCodes
{
public const string UnrecognizedBlockType = "CSS002";
public const string MissingOpeningBrace = "CSS023";
public const string MissingSelectorAfterCombinator = "CSS029";
public const string MissingSelectorBeforeCombinatorCode = "CSS031";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private static bool ShouldFilterHtmlDiagnosticBasedOnErrorCode(LspDiagnostic dia

return str switch
{
CSSErrorCodes.UnrecognizedBlockType => IsEscapedAtSign(diagnostic, sourceText),
CSSErrorCodes.MissingOpeningBrace => IsCSharpInStyleBlock(diagnostic, sourceText, syntaxTree),
CSSErrorCodes.MissingSelectorAfterCombinator => IsCSharpInStyleBlock(diagnostic, sourceText, syntaxTree),
CSSErrorCodes.MissingSelectorBeforeCombinatorCode => IsCSharpInStyleBlock(diagnostic, sourceText, syntaxTree),
Expand All @@ -244,6 +245,34 @@ private static bool ShouldFilterHtmlDiagnosticBasedOnErrorCode(LspDiagnostic dia
_ => false,
};

static bool IsEscapedAtSign(LspDiagnostic diagnostic, SourceText sourceText)
{
// Filters out "Unrecognized block type" errors in CSS, which occur with something like this:
//
// <style>
// @@font - face
// {
// // contents
// }
// </style>
//
// The "@@" tells Razor that the user wants an "@" in the final Html, but the design time document
// for the Html has to line up with the source Razor file, so that doesn't happen in the IDE. When
// CSS gets the two "@"s, it raises the "Unrecognized block type" error.

if (!sourceText.TryGetAbsoluteIndex(diagnostic.Range.Start, out var absoluteIndex))
{
return false;
}

// It's much easier to just check the source text directly, rather than try to understand all of the
// possible shapes of the syntax tree here. We assume that since the diagnostics we're filtering out
// came from the CSS server, it's a CSS block.
return absoluteIndex > 0 &&
sourceText[absoluteIndex] == '@' &&
sourceText[absoluteIndex - 1] == '@';
}

static bool IsCSharpInStyleBlock(LspDiagnostic diagnostic, SourceText sourceText, RazorSyntaxTree syntaxTree)
{
// C# in a style block causes diagnostics because the HTML background document replaces C# with "~"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis.Razor.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -65,6 +66,47 @@ public Task Html()
}]);
}

[Fact]
public Task FilterEscapedAtFromCss()
{
TestCode input = """
<div>
<style>
@@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
{|CSS002:f|}oo
{
bar: baz;
}
</style>
</div>
""";

return VerifyDiagnosticsAsync(input,
htmlResponse: [new VSInternalDiagnosticReport
{
Diagnostics =
[
new Diagnostic
{
Code = CSSErrorCodes.UnrecognizedBlockType,
Range = SourceText.From(input.Text).GetRange(new TextSpan(input.Text.IndexOf("@@") + 1, 1))
},
new Diagnostic
{
Code = CSSErrorCodes.UnrecognizedBlockType,
Range = SourceText.From(input.Text).GetRange(new TextSpan(input.Text.IndexOf("f"), 1))
}
]
}]);
}

[Fact]
public Task CombinedAndNestedDiagnostics()
=> VerifyDiagnosticsAsync("""
Expand Down