-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add letter casing mismatch validation and start triggering SYSLIB1021 #124521
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -248,6 +248,13 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt | |||
| keepMethod = false; | ||||
| } | ||||
|
|
||||
| bool casingValid = ValidateTemplatesCasingConsistency(lm.TemplateList); | ||||
| if (!casingValid) | ||||
| { | ||||
| Diag(DiagnosticDescriptors.InconsistentTemplateCasing, method.Identifier.GetLocation(), method.Identifier.ToString()); | ||||
| keepMethod = false; | ||||
| } | ||||
|
|
||||
| if (lm.Name[0] == '_') | ||||
| { | ||||
| // can't have logging method names that start with _ since that can lead to conflicting symbol names | ||||
|
|
@@ -788,6 +795,36 @@ private static bool ExtractTemplates(string? message, Dictionary<string, string> | |||
| return success; | ||||
| } | ||||
|
|
||||
| /// <summary> | ||||
| /// Validates that templates list does not contains templates with mixed casing like {hello} and {Hello} | ||||
| /// </summary> | ||||
| /// <returns>Value indicating lack of mixed casing within templates list</returns> | ||||
| private static bool ValidateTemplatesCasingConsistency(List<string> templates) | ||||
| { | ||||
| int count = templates.Count; | ||||
|
|
||||
| for (int i = 1; i < count; i++) | ||||
| { | ||||
| string templateI = templates[i]; | ||||
|
|
||||
| for (int j = 0; j < i; j++) | ||||
| { | ||||
| string templateJ = templates[j]; | ||||
|
|
||||
| bool matchIgnoreCase = StringComparer.OrdinalIgnoreCase.Compare(templateI, templateJ) == 0; | ||||
| bool matchExact = StringComparer.Ordinal.Compare(templateI, templateJ) == 0; | ||||
|
|
||||
| if (matchIgnoreCase && !matchExact) | ||||
| { | ||||
| return false; | ||||
| } | ||||
|
|
||||
|
||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -283,6 +283,21 @@ partial class C | |||||||||||||||||||||||||||||||||
| Assert.Equal(DiagnosticDescriptors.ShouldntMentionLoggerInMessage.Id, diagnostics[1].Id); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||||||||||||||
| public async Task MixedCasing() | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@" | ||||||||||||||||||||||||||||||||||
| partial class C | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""M1 {par1} {PAr1} {a}"")] | ||||||||||||||||||||||||||||||||||
| static partial void M1(ILogger logger, int par1, int a); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| "); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Assert.Single(diagnostics); | ||||||||||||||||||||||||||||||||||
| Assert.Equal(DiagnosticDescriptors.InconsistentTemplateCasing.Id, diagnostics[0].Id); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| [Fact] | |
| [Fact] | |
| public async Task DuplicateTemplateNameSameCasing_IsAllowed() | |
| { | |
| IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@" | |
| partial class C | |
| { | |
| [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""M1 {par1} {par1} {a}"")] | |
| static partial void M1(ILogger logger, int par1, int a); | |
| } | |
| "); | |
| Assert.Empty(diagnostics); | |
| } | |
| [Fact] |
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.
Grammar error in the comment: "does not contains" should be "does not contain".