Maui icon indicating copy to clipboard operation
Maui copied to clipboard

Added fix to support spaces between the method name and its `()` to prevent a false positive MCTME001 error

Open SotoiGhost opened this issue 2 years ago • 4 comments

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) or Championed (feature/proposal)
  • [ ] Has tests (if omitted, state reason in description)
  • [ ] Has samples (if omitted, state reason in description)
  • [x] Rebased on top of main at time of PR
  • [x] Changes adhere to coding standard
  • [ ] Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

SotoiGhost avatar Oct 26 '23 06:10 SotoiGhost

@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.

image

@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 avatar Nov 04 '23 19:11 SotoiGhost

@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.

pictos avatar Nov 06 '23 18:11 pictos

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 👍

bijington avatar Nov 06 '23 19:11 bijington

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. 😃

vhugogarcia avatar Feb 26 '24 19:02 vhugogarcia