Skip to content

fixes(44110): allow "extract to inner function" refactoring inside function expression #44206

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

Merged
merged 1 commit into from
Jun 10, 2021

Conversation

OliverJAsh
Copy link
Contributor

@OliverJAsh OliverJAsh commented May 21, 2021

Fixes #44110

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label May 21, 2021
@ghost
Copy link

ghost commented May 21, 2021

CLA assistant check
All CLA requirements met.

@@ -17,4 +17,4 @@

goTo.select("start", "end");
verify.refactorAvailable("Extract Symbol", "function_scope_1");
verify.not.refactorAvailable("Extract Symbol", "function_scope_2");
verify.refactorAvailable("Extract Symbol", "function_scope_2");

This comment was marked as outdated.

functionErrorsPerScope.push(
isFunctionLikeDeclaration(scope) && scope.kind !== SyntaxKind.FunctionDeclaration
? [createDiagnosticForNode(scope, Messages.cannotExtractToOtherFunctionLike)]
: []);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there was a good reason not to allow extractions for functions which aren't declarations…

@OliverJAsh OliverJAsh marked this pull request as ready for review May 21, 2021 22:01
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@sandersn sandersn requested a review from jessetrinity May 21, 2021 22:07
@OliverJAsh OliverJAsh changed the title Allow "extract to inner function" refactoring inside function expression fixes(44110): allow "extract to inner function" refactoring inside function expression May 25, 2021
@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels May 25, 2021
@jessetrinity
Copy link
Contributor

I can think of a couple reasons arrow functions might have been avoided.

Why no extract to arrow function? It would double the number of available refactors at a given extraction location. It would also have to be extracted to the right location to avoid runtime errors, but since we would know that it would only be called once (at the new location) the refactor on its own seems fine.

Why no convert to arrow function? The original declaration would have to show up before all of it's call sites or the refactor would introduce a runtime error, and I think the preference in general is to push declarations to the bottom of a scope. It might be doable, but I don't think it would be obvious to people why the refactor would only show up on function declarations some of the time if we restricted the cases.

These are guesses on my part and may be bad reasons. Either way, I can't think of why we can't offer a function declaration inside of an expression.

@OliverJAsh
Copy link
Contributor Author

I can't think of why we can't offer a function declaration inside of an expression.

Do you think we could move forward with this PR then, or do you want to wait for more feedback first?

@jessetrinity
Copy link
Contributor

yes, just looking over the tests :)

@jessetrinity
Copy link
Contributor

To be clear this PR also allows extract to inner function to work for class methods and constructors as well. Is there a case for wanting to extract an inner function in a constructor? This seems like it would be a weird choice to me.

@OliverJAsh
Copy link
Contributor Author

Is there a case for wanting to extract an inner function in a constructor? This seems like it would be a weird choice to me.

I wouldn't like to make assumptions about where these tools would be useful. If you have code in a scope anywhere, you might want to extract it—whether that's to an inner function or a module level function really depends on the author's use case and style preferences. Maybe they want to extract it to an inner function so it's easier to move later on, if it's needed elsewhere.

@jessetrinity
Copy link
Contributor

Fair point. It should be easy enough to restrict those cases if we decide later that offering them is too much clutter.

@jessetrinity jessetrinity merged commit 69cc9ba into microsoft:main Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

"Extract to inner function" refactoring not available inside function expression
3 participants