nicoespeon / abracadabra

Automated refactorings for VS Code (JS & TS) ✨ It's magic ✨
https://marketplace.visualstudio.com/items?itemName=nicoespeon.abracadabra
MIT License
798 stars 48 forks source link

Refactor observable subscribe arguments #592

Closed clabough closed 2 years ago

clabough commented 2 years ago

Please add the ability to refactor multiple subscribe arguments into one argument object. I have many existing observables using multiple arguments, but that has been deprecated. It would be very nice if there was an automated refactor for this conversion.

See https://rxjs.dev/deprecations/subscribe-arguments

Most often people just have this format in their code.

of([1,2,3]).subscribe((success) => {...}, (error) => {...} );

which would convert to

of([1,2,3]).subscribe({ next: (success) => {...}, error: (error) => {...}} );

nicoespeon commented 2 years ago

Hey @clabough, thanks for suggesting!

It sounds like what you need would rather be a "codemod". A way to refactor code from an old pattern to a new one. With a codemod, you can do that at scale on your codebase. Also, it's quite specific to RxJS, so I would look with these keywords.

Here is an example I could find: https://github.com/sergi/rxjs-codemod

It seems to be an old one, but it's a good example of what you could craft yourself, if there is no such codemod already. It's using JS CodeShift, which is a great tool to do such scripts.

That being said, it's too specific to RxJS to be part of Abracadabra. Writing a dedicated codemod would be the best approach IMO.

Here is another example of a codemod for RxJS: https://gist.github.com/Timebutt/9ddf4b28b8ec986ab8cab7e79713ddf9