Skip to content

Commit 31c4ece

Browse files
author
Sviatoslav Bychkov
committed
Filtered out log calls with invalid template parameter names
1 parent 0b3f52c commit 31c4ece

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### Title: ADR-06 Skip log calls with invalid template parameter names
2+
### Status: Accepted
3+
### Context:
4+
5+
Template message parameters are parsed to generate logging code dynamically.
6+
The template parameters' names are critical for synchronizing the log message format with the corresponding parameters in the code.
7+
If a parameter name is invalid, the generated code may produce unexpected results or fail to align with the user's intended behavior.
8+
9+
### Decision:
10+
11+
I will not process log calls if the template message parameters contain invalid names.
12+
Instead, I will filter these calls and report a diagnostic warning indicating the invalid parameter name.
13+
14+
### Consequences:
15+
16+
* **Positive**:
17+
* Users will be notified of issues in their template messages.
18+
* The generator remains simple and reliable, reducing potential bugs and unexpected behavior.
19+
20+
* **Negative**:
21+
* Users must fix their invalid parameter names before the log calls are processed. This might require additional effort upfront but is offset by the long-term benefits of correctness.
22+
23+
### Alternatives Considered
24+
25+
Automatically rename invalid parameter names to conform to a valid format (e.g., replacing spaces with underscores).
26+
27+
**Rejected Because**:
28+
* It introduces unexpected behavior for the user.
29+
* It adds some complexity to the generator and increases the risk of synchronization (template parameters <-> template message) errors.

src/AutoLoggerMessageGenerator.UnitTests/Utilities/IdentifierHelperTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class IdentifierHelperTests
1010
[InlineData("_Valid__Name___", "_Valid__Name___")]
1111
[InlineData("🔥Invalid🔥Emoji🔥", "__Invalid__Emoji__")]
1212
[InlineData("123Name", "_123Name")]
13-
[InlineData("Name#With$Special%Chars", "Name_With_Special_Chars")]
13+
[InlineData("Name#With$Special%Chars", "Name_With_Special_Chars")]
1414
public void ToValidCSharpMethodName_ShouldSanitizeCorrectly(string input, string expected)
1515
{
1616
var result = IdentifierHelper.ToValidCSharpMethodName(input);
@@ -25,4 +25,23 @@ public void ToValidCSharpMethodName_ShouldThrow_WhenInputIsNull(string? input)
2525
var action = () => IdentifierHelper.ToValidCSharpMethodName(input);
2626
action.Should().ThrowExactly<ArgumentNullException>();
2727
}
28+
29+
[Theory]
30+
[InlineData("validName", true)]
31+
[InlineData("_validName", true)]
32+
[InlineData("ValidName123", true)]
33+
[InlineData("a", true)]
34+
[InlineData("invalid name", false)]
35+
[InlineData("123Invalid", false)]
36+
[InlineData("invalid-name", false)]
37+
[InlineData("e.invalid", false)]
38+
[InlineData("i🔥nvalid", false)]
39+
[InlineData("", false)]
40+
[InlineData(null, false)]
41+
public void IsValidCSharpParameterName_WithGivenParameterName_ShouldReturnTheGivenResult(string parameterName,
42+
bool isValid)
43+
{
44+
var result = IdentifierHelper.IsValidCSharpParameterName(parameterName);
45+
result.Should().Be(isValid);
46+
}
2847
}

src/AutoLoggerMessageGenerator/Extractors/LogCallParametersExtractor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ internal partial class LogCallParametersExtractor(LogPropertiesCheck? logPropert
1919
.Select(TransformParameterName)
2020
.ToArray();
2121

22+
// TODO: Report a diagnostic message in the separate analyzer that some parameter names are "invalid"
23+
if (!templateParametersNames.All(IdentifierHelper.IsValidCSharpParameterName))
24+
return null;
25+
2226
var argumentParameterPrefix = ParameterName.TrimStart('@');
2327
var methodParameters = methodSymbol.Parameters
2428
.Where(c => c.Name.StartsWith(argumentParameterPrefix))

src/AutoLoggerMessageGenerator/Utilities/IdentifierHelper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AutoLoggerMessageGenerator.Utilities;
55

6-
internal static class IdentifierHelper
6+
internal static partial class IdentifierHelper
77
{
88
public static string ToValidCSharpMethodName(string? input)
99
{
@@ -16,4 +16,10 @@ public static string ToValidCSharpMethodName(string? input)
1616

1717
return sanitizedInput;
1818
}
19+
20+
public static bool IsValidCSharpParameterName(string name) =>
21+
!string.IsNullOrEmpty(name) && IsValidCSharpParameterNameRegex().IsMatch(name);
22+
23+
[GeneratedRegex(@"^@?[a-zA-Z_][a-zA-Z0-9_]*$")]
24+
private static partial Regex IsValidCSharpParameterNameRegex();
1925
}

0 commit comments

Comments
 (0)