Skip to content
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 @@ -9,6 +9,7 @@ internal static class CSSErrorCodes
public const string UnrecognizedBlockType = "CSS002";
public const string MissingClassNameAfterDot = "CSS008";
public const string MissingOpeningBrace = "CSS023";
public const string MissingPropertyName = "CSS024";
public const string MissingPropertyValue = "CSS025";
public const string MissingSelectorAfterCombinator = "CSS029";
public const string MissingSelectorBeforeCombinatorCode = "CSS031";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace Microsoft.CodeAnalysis.Razor.Diagnostics;

using RazorDiagnosticFactory = AspNetCore.Razor.Language.RazorDiagnosticFactory;
using RazorSyntaxNodeOrToken = AspNetCore.Razor.Language.Syntax.SyntaxNodeOrToken;
using SyntaxNode = AspNetCore.Razor.Language.Syntax.SyntaxNode;

/// <summary>
Expand Down Expand Up @@ -99,7 +98,7 @@ private static LspDiagnostic[] FilterHTMLDiagnostics(
var syntaxTree = codeDocument.GetRequiredSyntaxTree();
var sourceText = codeDocument.Source.Text;

var processedAttributes = new Dictionary<TextSpan, bool>();
using var _ = DictionaryPool<TextSpan, bool>.GetPooledObject(out var processedAttributes);

var filteredDiagnostics = unmappedDiagnostics
.Where(d =>
Expand Down Expand Up @@ -244,6 +243,7 @@ private static bool ShouldFilterHtmlDiagnosticBasedOnErrorCode(LspDiagnostic dia
CSSErrorCodes.MissingOpeningBrace or
CSSErrorCodes.MissingClassNameAfterDot or
CSSErrorCodes.MissingSelectorAfterCombinator or
CSSErrorCodes.MissingPropertyName or
CSSErrorCodes.MissingPropertyValue or
CSSErrorCodes.MissingSelectorBeforeCombinatorCode => IsAtCSharpTransitionInStyleBlock(diagnostic, sourceText, syntaxTree),
HtmlErrorCodes.UnexpectedEndTagErrorCode => IsHtmlWithBangAndMatchingTags(diagnostic, sourceText, syntaxTree),
Expand Down Expand Up @@ -428,17 +428,16 @@ private static bool InAttributeContainingCSharp(
return false;
}

var markupAttributeNode = owner.FirstAncestorOrSelf<RazorSyntaxNode>(static n =>
n is MarkupAttributeBlockSyntax ||
n is MarkupTagHelperAttributeSyntax ||
n is MarkupMiscAttributeContentSyntax);
var markupAttributeValue = owner.FirstAncestorOrSelf<RazorSyntaxNode>(static n =>
(n.Parent is MarkupAttributeBlockSyntax block && n == block.Value) ||
n is MarkupTagHelperAttributeValueSyntax or MarkupMiscAttributeContentSyntax);

if (markupAttributeNode is not null)
if (markupAttributeValue is not null)
{
if (!processedAttributes.TryGetValue(markupAttributeNode.Span, out var doesAttributeContainNonMarkup))
if (!processedAttributes.TryGetValue(markupAttributeValue.Span, out var doesAttributeContainNonMarkup))
{
doesAttributeContainNonMarkup = CheckIfAttributeContainsNonMarkupNodes(markupAttributeNode);
processedAttributes.Add(markupAttributeNode.Span, doesAttributeContainNonMarkup);
doesAttributeContainNonMarkup = CheckIfAttributeContainsNonMarkupNodes(markupAttributeValue);
processedAttributes.Add(markupAttributeValue.Span, doesAttributeContainNonMarkup);
}

return doesAttributeContainNonMarkup;
Expand All @@ -448,16 +447,16 @@ n is MarkupTagHelperAttributeSyntax ||

static bool CheckIfAttributeContainsNonMarkupNodes(RazorSyntaxNode attributeNode)
{
// Only allow markup, generic & (non-razor comment) token nodes
var containsNonMarkupNodes = attributeNode.DescendantNodesAndTokens().Any(IsNotMarkupNodeOrCommentToken);

return containsNonMarkupNodes;
return attributeNode.DescendantNodes().Any(IsNotMarkupOrCommentNode);
}

static bool IsNotMarkupNodeOrCommentToken(RazorSyntaxNodeOrToken nodeOrToken)
static bool IsNotMarkupOrCommentNode(SyntaxNode node)
{
return !(nodeOrToken.IsToken && nodeOrToken.AsToken().Kind == SyntaxKind.RazorCommentTransition) &&
!(nodeOrToken.IsNode && nodeOrToken.AsNode() is MarkupBlockSyntax or MarkupSyntaxNode or GenericBlockSyntax);
return !(node is
MarkupBlockSyntax or
MarkupSyntaxNode or
GenericBlockSyntax or
RazorCommentBlockSyntax);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,40 @@ public Task FilterPropertyValueInCss()
}]);
}

[Fact]
public Task FilterPropertyNameInCss()
{
TestCode input = """
<div style="{|CSS024:/|}****/"></div>
<div style="@(someBool ? "width: 100%" : "width: 50%")">

</div>

@code
{
private bool someBool = false;
}
""";

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

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