Update Extract Method to support top-level statements#44453
Update Extract Method to support top-level statements#44453sharwell merged 2 commits intodotnet:masterfrom
Conversation
There was a problem hiding this comment.
this block i don't quite understand. what's the case where the destinatino member is a statement itself? who is adding statements to a statement itself?
There was a problem hiding this comment.
Top-level statements of a script are not part of a statement container. In this case, we have to create the container and move the original statement into it along with the new statements.
There was a problem hiding this comment.
can you give a concrete example of the type of statement we are adding, and the statement it is being added to? it's not clear to me.
There was a problem hiding this comment.
This behavior supports the following test:
If we didn't wrap the generated code in a block, the generated function would be a method declaration as opposed to a local function declaration.
There was a problem hiding this comment.
would it make sense to disable extract-local-function here instead? this really doesn't seem like a desirable change to me. Plus, the addition of the block seems like it can actually screw things up. For example, what if we had var v = [|1 + 1|];. Would we make a block that only contained the variable and the local-func, causing references to 'v' to not work?
There was a problem hiding this comment.
It would make sense to disable, but that was a bit more complicated and only impacts script scenarios. I'll file a follow-up issue for it.
df672de to
6573bd1
Compare
6573bd1 to
99397da
Compare
|
@CyrusNajmabadi for review |
| }"; | ||
| } | ||
|
|
||
| local = NewMethod(); |
There was a problem hiding this comment.
Why did this ordering change? Should we offer 2 fixes here: 1. to insert in-place and 2. to insert at end of the current block?
There was a problem hiding this comment.
This scenario is only valid for C# scripting, so I'm not sure it matters. It would be covered by https://github.com/dotnet/roslyn/pull/44453/files#r428859532.
There was a problem hiding this comment.
It's going to be much easier to deal with these details when the tests are converted to the new test library.
|
@CyrusNajmabadi let me know if you think this one is good to merge |
| { | ||
| return (accessorDeclaration.Body == null) ? destinationMember : Cast<TDeclarationNode>(accessorDeclaration.AddBodyStatements(StatementGenerator.GenerateStatements(statements).ToArray())); | ||
| } | ||
| else if (destinationMember is CompilationUnitSyntax compilationUnit && options is null) |
There was a problem hiding this comment.
why the check for options is null?
There was a problem hiding this comment.
This method had an existing caller that passed in CompilationUnitSyntax with options, and relied on the AddStatementsWorker fallback to handle those options. Right now I forget what caller that was.
There was a problem hiding this comment.
➡️ Will submit a follow-up to clarify the condition.
| else if (destinationMember is CompilationUnitSyntax compilationUnit && options is null) | ||
| { | ||
| // Insert the new global statement(s) at the end of any current global statements | ||
| var insertionIndex = compilationUnit.Members.LastIndexOf(memberDeclaration => memberDeclaration.IsKind(SyntaxKind.GlobalStatement)) + 1; |
There was a problem hiding this comment.
i would love it we doc'ed that LastIndexOf returns -1 on failure. that would make me feel much safer about this code :)
There was a problem hiding this comment.
➡️ Will submit a follow-up to clarify.
| { | ||
| // Insert the new global statement(s) at the end of any current global statements | ||
| var insertionIndex = compilationUnit.Members.LastIndexOf(memberDeclaration => memberDeclaration.IsKind(SyntaxKind.GlobalStatement)) + 1; | ||
| var wrappedStatements = StatementGenerator.GenerateStatements(statements).Select(generated => SyntaxFactory.GlobalStatement(generated)).ToArray(); |
There was a problem hiding this comment.
.ToArray seems unnecessary. though it looks like there's a lot of that in this mtehod :)
There was a problem hiding this comment.
Partially pattern matching with the rest of the method, but also easier to deal with when reading and debugging through.
CyrusNajmabadi
left a comment
There was a problem hiding this comment.
Overall i'm ok with this. A couple of tiny clarity questions. but they're not blocking (unless hte answers are very bizarre :))
Fixes #44260