-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Adding analyzer/fixer for the Regex Source Generator #68976
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions Issue DetailsFixes #68147 FYI: @meziantou Adding Roslyn analyzer and code fixer that will suggest the use of the Regex Source Generator whenever possible. Pending items before merging:
|
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.
@mavasani, could you help review this as well?
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/DiagnosticDescriptors.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/RegexAnalyzer.cs
Outdated
Show resolved
Hide resolved
} | ||
|
||
// Create the property bag. | ||
ImmutableDictionary<string, string?> properties = ImmutableDictionary.CreateRange(new[] |
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.
Is there a reason to add this property bag to the diagnostic instead of computing all the required information in the fixer itself? Given the diagnostic location, the fixer can fetch the required syntax node from the location using root.FindNode(span)
API. You can get the semantic model from the document using document.GetSemanticModelAsync
API and then use semanticModel.GetOperation(node)
API to get to the IInvocationOperation
.
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.
NOTE: If you feel your current approach is more maintainable and makes it more easier to understand for anyone new to this code, then it seems fine as well. We generally don't recommend using such a property bag unless needed as you are now adding an additional dependency between the analyzer and fixer internals, which might make it more harder to maintain or understand.
SyntaxNode nodeToFix = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: false); | ||
// Save the operation object from the nodeToFix before it gets replaced by the new method invocation. | ||
// We will later use this operation to get the parameters out and pass them into the RegexGenerator attribute. | ||
IOperation? operation = semanticModel.GetOperation(nodeToFix, cancellationToken); |
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.
So seems like you are already computing the operation to fix in the fixer. Wondering if it is easier to avoid the property bag on the diagnostic and just move all the computations to the fixer itself.
@joperezr You can add documentation later, as long as it's merged before RC1 is snapped. |
Here are the unit tests for Edit: Nevermind, does not seem to be analyzer tests. But from our conversation, seems you already know how to solve the test challenge. |
Still need to fix the top-level statements and address a couple of comments I missed. |
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorAnalyzer.cs
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs
Show resolved
Hide resolved
All of the failed checks are just cancelled builds for pipelines that shouldn't be running in the first place so the rest of the builds are green, so I'm merging now. |
* Adding analyzer/fixer for the Regex Source Generator * Adding some tests to the analyzer and fixer * Fix build and reference live ref pack * Address remaining feedback and fix top-level statement programs * Addressing PR Feedback * Disabling the tests for Mono
* Adding analyzer/fixer for the Regex Source Generator * Adding some tests to the analyzer and fixer * Fix build and reference live ref pack * Address remaining feedback and fix top-level statement programs * Addressing PR Feedback * Disabling the tests for Mono
@joperezr maybe I missed discussion of it, but what was the reasoning behind not removing RegexOptions.Compiled in the output of the fixer? It's ignored, so it just seems like noise. |
If the pattern doesn't support source code generation (e.g. IgnoreCase backteference), you still want the Regex ctor emitted as a backup strategy to use Compiled if the dev asked for it. |
Fixes #68147
FYI: @meziantou
Adding Roslyn analyzer and code fixer that will suggest the use of the Regex Source Generator whenever possible.
Pending items before merging: