-
Notifications
You must be signed in to change notification settings - Fork 240
Add warning waves infrastructure to Razor compiler #13016
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...piler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CodeRenderingContextTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Linq; | ||
| using Microsoft.AspNetCore.Razor.Language.Intermediate; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration; | ||
|
|
||
| public class CodeRenderingContextTest | ||
| { | ||
| [Theory] | ||
| [InlineData(0u, false)] | ||
| [InlineData(11u, false)] | ||
| [InlineData(12u, true)] | ||
| [InlineData(9999u, true)] | ||
| public void GetDiagnostics_FiltersWarningsByLevel(uint warningLevel, bool expectDiagnostic) | ||
| { | ||
| // Arrange | ||
| var descriptor = new RazorDiagnosticDescriptor("RZTest", "Test warning for '{0}'", RazorDiagnosticSeverity.Warning, warningLevel: 12); | ||
| var diagnostic = RazorDiagnostic.Create(descriptor, SourceSpan.Undefined, "param"); | ||
|
|
||
| var documentNode = new DocumentIntermediateNode(); | ||
| documentNode.AddDiagnostic(diagnostic); | ||
|
|
||
| var options = RazorCodeGenerationOptions.Default.WithRazorWarningLevel(warningLevel); | ||
| using var context = new CodeRenderingContext( | ||
| RuntimeNodeWriter.Instance, | ||
| TestRazorSourceDocument.Create(), | ||
| documentNode, | ||
| options); | ||
|
|
||
| // Act | ||
| var diagnostics = context.GetDiagnostics(); | ||
|
|
||
| // Assert | ||
| if (expectDiagnostic) | ||
| { | ||
| Assert.Contains(diagnostics, d => d.Id == "RZTest"); | ||
| } | ||
| else | ||
| { | ||
| Assert.DoesNotContain(diagnostics, d => d.Id == "RZTest"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDiagnostics_AlwaysOnDiagnostics_NotFilteredAtAnyLevel() | ||
| { | ||
| // Arrange — level 0 diagnostics are always on | ||
| var descriptor = new RazorDiagnosticDescriptor("RZAlways", "Always on: '{0}'", RazorDiagnosticSeverity.Warning); | ||
| var diagnostic = RazorDiagnostic.Create(descriptor, SourceSpan.Undefined, "param"); | ||
|
|
||
| var documentNode = new DocumentIntermediateNode(); | ||
| documentNode.AddDiagnostic(diagnostic); | ||
|
|
||
| var options = RazorCodeGenerationOptions.Default.WithRazorWarningLevel(0); | ||
| using var context = new CodeRenderingContext( | ||
| RuntimeNodeWriter.Instance, | ||
| TestRazorSourceDocument.Create(), | ||
| documentNode, | ||
| options); | ||
|
|
||
| // Act | ||
| var diagnostics = context.GetDiagnostics(); | ||
|
|
||
| // Assert — level 0 diagnostic should be present even at warning level 0 | ||
| Assert.Contains(diagnostics, d => d.Id == "RZAlways"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(10u)] | ||
| [InlineData(11u)] | ||
| [InlineData(12u)] | ||
| [InlineData(13u)] | ||
| public void GetDiagnostics_MultipleLevels_FiltersCorrectly(uint warningLevel) | ||
| { | ||
| // Arrange — diagnostics at levels 0, 11, 12, and 13 | ||
| var alwaysOn = RazorDiagnostic.Create( | ||
| new RazorDiagnosticDescriptor("RZ0", "Always", RazorDiagnosticSeverity.Warning), | ||
| SourceSpan.Undefined); | ||
| var level11 = RazorDiagnostic.Create( | ||
| new RazorDiagnosticDescriptor("RZ11", "Level 11", RazorDiagnosticSeverity.Warning, warningLevel: 11), | ||
| SourceSpan.Undefined); | ||
| var level12 = RazorDiagnostic.Create( | ||
| new RazorDiagnosticDescriptor("RZ12", "Level 12", RazorDiagnosticSeverity.Warning, warningLevel: 12), | ||
| SourceSpan.Undefined); | ||
| var level13 = RazorDiagnostic.Create( | ||
| new RazorDiagnosticDescriptor("RZ13", "Level 13", RazorDiagnosticSeverity.Warning, warningLevel: 13), | ||
| SourceSpan.Undefined); | ||
|
|
||
| var documentNode = new DocumentIntermediateNode(); | ||
| documentNode.AddDiagnostic(alwaysOn); | ||
| documentNode.AddDiagnostic(level11); | ||
| documentNode.AddDiagnostic(level12); | ||
| documentNode.AddDiagnostic(level13); | ||
|
|
||
| var options = RazorCodeGenerationOptions.Default.WithRazorWarningLevel(warningLevel); | ||
| using var context = new CodeRenderingContext( | ||
| RuntimeNodeWriter.Instance, | ||
| TestRazorSourceDocument.Create(), | ||
| documentNode, | ||
| options); | ||
|
|
||
| // Act | ||
| var diagnostics = context.GetDiagnostics(); | ||
|
|
||
| // Assert — always-on is always present | ||
| Assert.Contains(diagnostics, d => d.Id == "RZ0"); | ||
|
|
||
| // Each level is present only when warningLevel >= that level | ||
| Assert.Equal(warningLevel >= 11, diagnostics.Any(d => d.Id == "RZ11")); | ||
| Assert.Equal(warningLevel >= 12, diagnostics.Any(d => d.Id == "RZ12")); | ||
| Assert.Equal(warningLevel >= 13, diagnostics.Any(d => d.Id == "RZ13")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,19 @@ public sealed record RazorDiagnosticDescriptor | |
| public string MessageFormat { get; } | ||
| public RazorDiagnosticSeverity Severity { get; } | ||
|
|
||
| /// <summary> | ||
| /// The warning level at which this diagnostic is reported. Diagnostics with a warning level | ||
| /// greater than the configured <c>RazorWarningLevel</c> are suppressed. | ||
| /// A value of <c>0</c> means the diagnostic is always reported regardless of the configured level. | ||
| /// </summary> | ||
| public uint WarningLevel { get; } | ||
|
|
||
| public RazorDiagnosticDescriptor(string id, string messageFormat, RazorDiagnosticSeverity severity) | ||
| : this(id, messageFormat, severity, warningLevel: 0) | ||
| { | ||
| } | ||
|
|
||
| public RazorDiagnosticDescriptor(string id, string messageFormat, RazorDiagnosticSeverity severity, uint warningLevel) | ||
| { | ||
| if (string.IsNullOrEmpty(id)) | ||
| { | ||
|
|
@@ -28,9 +40,9 @@ public RazorDiagnosticDescriptor(string id, string messageFormat, RazorDiagnosti | |
| Id = id; | ||
| MessageFormat = messageFormat; | ||
| Severity = severity; | ||
| WarningLevel = warningLevel; | ||
| } | ||
|
|
||
| private string DebuggerToString() => $""" | ||
| Error "{Id}": "{MessageFormat}" | ||
| """; | ||
| private string DebuggerToString() | ||
| => $"""Error "{Id}" (level {WarningLevel}): "{MessageFormat}" """; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the extra space at the end intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't the change that I was expecting. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SelectAndOrderByAsArray?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, was SelectAndOrderByAsArray not a thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind the old question, that was dumb. But, the new question is:
Is OrderByAsArray stable in netstandard2.0? Does it matter in this context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the stability is the same as the original, so never mind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, it feels like this method is pretty high traffic. In which case, maybe your original change is better.