p42ai / js-assistant

120+ refactorings and code-assists for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=p42ai.refactor
MIT License
125 stars 8 forks source link

Refactor idea: Refactor function to module scope, but preserve the function itself (with/out arguments) #37

Closed aghArdeshir closed 2 years ago

aghArdeshir commented 2 years ago

Hi. Sometimes I use P42 for refactoring callbacks, so the code would be cleaner, but I always have to make a manual refactor to make it more performant. I want my callbacks to be preserved (instead of me

Initial: image

Result: image

Expected: image

I'm not too familiar with this whole parsing and automatic refactoring thing and I don't know the thing I expect is possible or not. And I'm aware with arguments it will be more and more complicated But I believe the simple cases can be detected and put as an option.

lgrammel commented 2 years ago

Hi @aghArdeshir - thanks for the refactoring idea!

I'm unsure which P42 refactoring you are currently using - my guess is that you use an "extract to function in module scope" refactoring from VS Code (which would explain why it's an arrow function wrapped in a function).

With P42, you could use "extract const" as follows:

image

This would give you:

image

It's a bit different from your expected result however, because the extracted function is not at the top level. Is this closer to what you were looking for?

aghArdeshir commented 2 years ago

Yup. Sorry :+1: THat makes sense