Skip to content
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

Merged
merged 6 commits into from
May 26, 2022

Conversation

joperezr
Copy link
Member

@joperezr joperezr commented May 6, 2022

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:

  • Adding tests for the analyzer and fixer
  • Adding documentation into docs.microsoft.com around the new diagnostic. cc: @carlossanlop @buyaa-n do I need to do this before merging the PR into main? Or is it ok to merge and then subsequently add the diagnostic's documentation? Also, since this is the first analyzer I write, am I missing something apart from public documentation?

@ghost
Copy link

ghost commented May 6, 2022

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

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:

  • Adding tests for the analyzer and fixer
  • Adding documentation into docs.microsoft.com around the new diagnostic. cc: @carlossanlop @buyaa-n do I need to do this before merging the PR into main? Or is it ok to merge and then subsequently add the diagnostic's documentation? Also, since this is the first analyzer I write, am I missing something apart from public documentation?
Author: joperezr
Assignees: -
Labels:

area-System.Text.RegularExpressions

Milestone: -

Copy link
Member

@stephentoub stephentoub left a 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?

}

// Create the property bag.
ImmutableDictionary<string, string?> properties = ImmutableDictionary.CreateRange(new[]
Copy link

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.

Copy link

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);
Copy link

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.

@carlossanlop
Copy link
Member

Adding documentation into docs.microsoft.com around the new diagnostic. cc: @carlossanlop @buyaa-n do I need to do this before merging the PR into main?

@joperezr You can add documentation later, as long as it's merged before RC1 is snapped.

@carlossanlop
Copy link
Member

carlossanlop commented May 12, 2022

Here are the unit tests for LibraryImportGenerator, within the dotnet/runtime repo: https://github.com/dotnet/runtime/blob/bcded447f1558e7f891f3411f566db49f2ced6ec/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs

Edit: Nevermind, does not seem to be analyzer tests. But from our conversation, seems you already know how to solve the test challenge.

@joperezr
Copy link
Member Author

Still need to fix the top-level statements and address a couple of comments I missed.

@joperezr
Copy link
Member Author

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.

@joperezr joperezr merged commit 1e4f93b into dotnet:main May 26, 2022
joperezr added a commit to joperezr/runtime that referenced this pull request May 26, 2022
* 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
ericstj pushed a commit that referenced this pull request May 27, 2022
* 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
@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2022
@danmoseley
Copy link
Member

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

@stephentoub
Copy link
Member

stephentoub commented Dec 22, 2022

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analyzer/fixer for converting Regex use to source generator
5 participants