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 @@ -340,10 +340,11 @@ public IEnumerable<SyntaxNode> AncestorsAndSelf()
}
}

#nullable enable
/// <summary>
/// Gets the first node of type TNode that matches the predicate.
/// </summary>
public TNode FirstAncestorOrSelf<TNode>(Func<TNode, bool> predicate = null)
public TNode? FirstAncestorOrSelf<TNode>(Func<TNode, bool>? predicate = null)
where TNode : SyntaxNode
{
for (var node = this; node != null; node = node.Parent)
Expand All @@ -356,6 +357,7 @@ public TNode FirstAncestorOrSelf<TNode>(Func<TNode, bool> predicate = null)

return default;
}
#nullable disable

/// <summary>
/// Gets a list of descendant nodes in prefix document order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static bool IsCSharpInStyleBlock(Diagnostic diagnostic, SourceText sourceText, R
var element = owner.FirstAncestorOrSelf<MarkupElementSyntax>(n => n.StartTag?.Name.Content == "style");
var csharp = owner.FirstAncestorOrSelf<CSharpCodeBlockSyntax>();

return element.Body.Any(c => c is CSharpCodeBlockSyntax) || csharp is not null;
return element?.Body.Any(c => c is CSharpCodeBlockSyntax) ?? false || csharp is not null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Is the ?? false necessary?

Suggested change
return element?.Body.Any(c => c is CSharpCodeBlockSyntax) ?? false || csharp is not null;
return element?.Body.Any(c => c is CSharpCodeBlockSyntax) ?? (csharp is not null);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I find that confusing, and would have to spend a few minutes trying to understand the logic.

}

// Ideally this would be solved instead by not emitting the "!" at the HTML backing file,
Expand All @@ -255,6 +255,10 @@ static bool IsAnyFilteredTooFewElementsError(Diagnostic diagnostic, SourceText s
}

var element = owner.FirstAncestorOrSelf<MarkupElementSyntax>();
if (element is null)
Comment on lines 257 to +258
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var element = owner.FirstAncestorOrSelf<MarkupElementSyntax>();
if (element is null)
if (owner.FirstAncestorOrSelf<MarkupElementSyntax>() is not { } element)

{
return false;
}

if (element.StartTag?.Name.Content != "html")
{
Expand All @@ -280,8 +284,8 @@ static bool IsHtmlWithBangAndMatchingTags(Diagnostic diagnostic, SourceText sour
}

var element = owner.FirstAncestorOrSelf<MarkupElementSyntax>();
var startNode = element.StartTag;
var endNode = element.EndTag;
var startNode = element?.StartTag;
var endNode = element?.EndTag;

if (startNode is null || endNode is null)
{
Expand All @@ -290,9 +294,9 @@ static bool IsHtmlWithBangAndMatchingTags(Diagnostic diagnostic, SourceText sour
}

var haveBang = startNode.Bang is not null && endNode.Bang is not null;
var namesEquivilant = startNode.Name.Content == endNode.Name.Content;
var namesEquivalent = startNode.Name.Content == endNode.Name.Content;

return haveBang && namesEquivilant;
return haveBang && namesEquivalent;
}

static bool IsAnyFilteredInvalidNestingError(Diagnostic diagnostic, SourceText sourceText, RazorSyntaxTree syntaxTree, ILogger logger)
Expand Down Expand Up @@ -334,7 +338,7 @@ static bool IsInvalidNestingFromBody(Diagnostic diagnostic, SourceText sourceTex
return false;
}

return diagnostic.Message.EndsWith("cannot be nested inside element 'html'.") && body.StartTag?.Bang is not null;
return diagnostic.Message.EndsWith("cannot be nested inside element 'html'.") && body?.StartTag?.Bang is not null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,45 @@ public async Task Handle_FilterDiagnostics_CSharpInsideStyleBlock()
Assert.Empty(response.Diagnostics);
}

[Fact]
public async Task Handle_FilterDiagnostics_CSharpInsideStyleBlockSpace_NotStyleTag()
{
// Arrange
var documentPath = new Uri("C:/path/to/document.cshtml");
var codeDocument = CreateCodeDocumentWithCSharpProjection(
"<stile> @DateTime.Now </stile>",
"var __o = DateTime.Now",
new[] {
new SourceMapping(
new SourceSpan(4, 12),
new SourceSpan(10, 12))
});
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var diagnosticsService = new RazorTranslateDiagnosticsService(_mappingService, LoggerFactory);
var diagnosticsEndpoint = new RazorTranslateDiagnosticsEndpoint(diagnosticsService, LoggerFactory);
var request = new RazorDiagnosticsParams()
{
Kind = RazorLanguageKind.Html,
Diagnostics = new[] {
new VSDiagnostic() {
Range = new Range { Start = new Position(0, 7),End = new Position(0, 7) },
Code = CSSErrorCodes.MissingSelectorBeforeCombinatorCode,
Severity = DiagnosticSeverity.Warning
}
},
RazorDocumentUri = documentPath,
};
var expectedRange = new Range { Start = new Position(0, 8), End = new Position(0, 15) };
var requestContext = CreateRazorRequestContext(documentContext);

// Act
var response = await Task.Run(() => diagnosticsEndpoint.HandleRequestAsync(request, requestContext, default));

// Assert
Assert.NotNull(response.Diagnostics);
Assert.Equal(CSSErrorCodes.MissingSelectorBeforeCombinatorCode, response.Diagnostics![0].Code);
}

[Fact]
public async Task Handle_FilterDiagnostics_CSharpWarning()
{
Expand Down