Added fix to support spaces between the method name and its `()` to prevent a false positive MCTME001 error
Description of Change
Added fix to support spaces between the method name and its () to prevent a false positive MCTME001 error
Linked Issues
- Fixes #1476
PR Checklist
- [x] Has a linked Issue, and the Issue has been
approved(bug) orChampioned(feature/proposal) - [ ] Has tests (if omitted, state reason in description)
- [ ] Has samples (if omitted, state reason in description)
- [x] Rebased on top of
mainat time of PR - [x] Changes adhere to coding standard
- [ ] Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls
Additional information
@brminnick I was trying to add the [GeneratedRegex] but I realized that the Analyzer project is a netstandard2.0 and the attribute is only available on .net7 and .net8.
@pictos I don't have experience with the Roslyn APIs but if you can point me into the right direction, I can make the changes, meanwhile, I'm trying to add unit testing. It's always good to learn new things! 😃
@SotoiGhost I don't have EOT to look into that... To not block this PR, we can merge it and in the future, I'll find time to investigate removing the regex.
We won't merge this until we have proven the Regex doesn't impact build performance. I have it on my list to look at 👍
We won't merge this until we have proven the Regex doesn't impact build performance. I have it on my list to look at 👍
.NET 8 includes a number of Regex performance improvements, including support for compiling regular expressions more efficiently and support for executing regular expressions more efficiently.
By using this pattern: @"\.UseMauiCommunityToolkit\s*\(\)" . This pattern breaks down as follows:
.: Escapes the dot since it’s a special character in Regex, ensuring it matches a literal dot. UseMauiCommunityToolkit: Matches the literal text “UseMauiCommunityToolkit”. \s*: Matches any whitespace character (like space, tab, etc.) zero or more times. This accounts for both no space and the space in “UseMauiCommunityToolkit ()”. (): Matches the literal parentheses.
This Regex is performant because it avoids unnecessary backtracking and matches both text options efficiently. Here’s how you might use it in code:
using System.Text.RegularExpressions;
string pattern = @"\.UseMauiCommunityToolkit\s*\(\)";
Regex regex = new Regex(pattern, RegexOptions.Compiled);
bool isValidOption1 = regex.IsMatch(".UseMauiCommunityToolkit()");
bool isValidOption2 = regex.IsMatch(".UseMauiCommunityToolkit ()");
This code snippet creates a compiled Regex object and checks if the two text options match the pattern. Both isValidOption1 and isValidOption2 should evaluate to true if the text options are as provided. The RegexOptions.Compiled option is set to this pattern since it will be used frequently, to further improve performance.
What do you all think? cc: @SotoiGhost
I just wanted to share my grain of salt on this PR. 😃