You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR enhances decimal handling in the source generator and adds test coverage for nullable decimal arguments. The changes address issues with decimal literal generation in source-generated code and ensure proper handling of nullable decimal parameters.
Code Quality and Best Practices ✅
Strengths:
Modern C# patterns: Good use of pattern matching, null-conditional operators, and LINQ
Consistent formatting: Follows the established coding style with proper indentation and bracing
Comprehensive decimal handling: Handles both nullable and non-nullable decimal types consistently
Syntax preservation: Clever approach to preserve original decimal literal syntax from source code
Defensive programming: Includes null checks and bounds checking for array access
Areas for improvement:
The decimal handling logic is duplicated across multiple code paths (lines 103-133, 146-174, 187-206). Consider extracting this into a helper method like TryGetDecimalLiteralText()
Complex nested conditionals could benefit from early returns to improve readability
Potential Bugs or Issues ⚠️
Minor concerns:
Array bounds safety: Lines 109, 152, 189 check syntaxIndex < syntaxArguments.Count but there's potential for race conditions if the collection changes
String manipulation edge case: TrimEnd('d', 'D', 'f', 'F', 'l', 'L', 'u', 'U', 'm', 'M') could over-trim if a decimal value legitimately ends with these characters (e.g., a property name)
Syntax handling: The syntaxIndex-- backup mechanism (lines 125, 166, 197) could lead to inconsistent state if error conditions occur
Performance Considerations 🚀
Good practices:
Efficient LINQ usage with appropriate filtering
Minimal string allocations with StringBuilder-like CodeWriter
Early returns to avoid unnecessary processing
Potential optimizations:
The syntax arguments parsing is done for every argument - could be cached
Multiple calls to ToString() on the same expression could be optimized
Security Concerns ✅
No security issues identified
Proper input validation and sanitization
No reflection usage that could lead to security vulnerabilities
Test Coverage 📋
Positive aspects:
New test file NullableArgumentsTest.cs specifically targets the decimal/nullable scenario
Tests both non-null and null values for nullable decimal parameters
Follows TUnit's testing patterns correctly
Suggestions for enhanced coverage:
Consider adding test cases for:
Different decimal literal formats (scientific notation, very large/small values)
Mixed parameter types (decimal, decimal?, other types)
Edge cases like decimal.MaxValue, decimal.MinValue
String literals that should be parsed as decimals
Architecture Compliance ✅
The changes align well with TUnit's architecture:
Dual execution modes: The fix applies to source generation, maintaining behavioral parity
Performance focus: Avoids reflection and generates efficient code
Modern C# usage: Leverages pattern matching and modern language features
Recommendations
Refactor decimal handling: Extract the repeated decimal literal processing logic into a private helper method
Add more test cases: Include edge cases for decimal handling
Consider snapshot tests: Given this affects source generator output, verify that source generator snapshot tests pass
Documentation: Consider adding XML comments to explain the decimal literal preservation logic
Final Assessment ✅
This is a solid fix that addresses decimal handling issues while maintaining code quality and performance. The implementation is thorough and the test coverage targets the specific issue. The code follows TUnit's established patterns and guidelines.
Approval recommended with the suggestion to consider the refactoring opportunities for maintainability.
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
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.
Fixes #3170