Add a code refactoring to convert local function to method#16808
Add a code refactoring to convert local function to method#16808sharwell merged 23 commits intodotnet:masterfrom
Conversation
|
Data-flow analysis fails for no appearant reason. Any thoughts? |
|
@agocke May know more about data-flow. |
|
Since data-flow analysis on local functions is back, I synced the PR and added more tests. This is still a work-in-progress (see the opening description). Any early feedback is appreciated, though. thanks. /cc @sharwell |
|
object M(object arg)
{
object Local() => arg;
arg = null;
return Local();
}-> object M(object arg)
{
arg = null;
return Local(arg);
}
private static object Local(object arg) => arg;Do we want to not suggest the refactoring in this case? Thoughts? |
|
It's a refactoring. So i feel they should be available to the user as their absence can be confusing. Since we're not going to be doin ga fix-all here it's not a problem for me. I do think that it would make sense to give a warning. |
DustinCampbell
left a comment
There was a problem hiding this comment.
Just took a brief look through.
There was a problem hiding this comment.
I'd love to see the type parameters reduced to just the ones that are used.
There was a problem hiding this comment.
What about parameters from enclosing methods that are captured?
There was a problem hiding this comment.
I expect this return those too! I'll add a test.
There was a problem hiding this comment.
I'd recommend typeParameterList since there's a method.Decl.ParameterList property.
There was a problem hiding this comment.
Again, typeParameterList is probably better for more readable code.
486d5f7 to
f53bcb2
Compare
|
@sharwell @CyrusNajmabadi would love to hear your feedback on this. made progress but the code needs serious cleanup. thanks. |
|
Looking. |
There was a problem hiding this comment.
Not a huge fan of this. Specifically, that it makes this code different than the rest of our features. I'd prefer another PR that changes the features layer to use this pattern FWIW.
There was a problem hiding this comment.
perhaps offer on the name of the local function as well.
There was a problem hiding this comment.
I'll add test for [||]LocalName(), Local[||]Name(), [|LocalName|]() and LocalName[||](). And negative case for other places.
|
@jcouv This is no longer blocked. Can you please remove the label so I can get a review? Thanks. |
|
Removed blocked label. |
| <value>Convert to regular method</value> | ||
| </data> | ||
| <data name="Convert_to_method" xml:space="preserve"> | ||
| <value>Convert to method</value> |
There was a problem hiding this comment.
what is the difference between a method and a regular method?
There was a problem hiding this comment.
Wait, I recall reverting to "Convert to method". The previous string is not supposed to be there.
| return; | ||
| } | ||
|
|
||
| context.RegisterRefactoring(new MyCodeAction(c => UpdateDocumentAsync(root, document, parentBlock, localFunction, c))); |
|
@jinujoseph @sharwell What is the state of this PR? I haven't heard back from the team. please let me know if this needs any further work. |
sharwell
left a comment
There was a problem hiding this comment.
This PR has an extraneous value is the resources file that needs to be removed, but is otherwise ready once tests pass and any merge conflicts are addressed.
| <data name="Convert_to_for" xml:space="preserve"> | ||
| <value>Convert to 'for'</value> | ||
| </data> | ||
| <data name="Convert_to_regular_method" xml:space="preserve"> |
There was a problem hiding this comment.
❗️ Need to remove this extraneous value
# Conflicts: # src/Features/CSharp/Portable/CSharpFeaturesResources.resx
|
There's an oddity with flow analysis and structs in which they are decided to be written into even if they are not (e.g. when they are only dotted-off). @sharwell, do you know why is that the case? |
|
cc @mavasani |
Instance properties and methods in a value type can reassign |
Fixes #16288