Skip to content
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

Create rewrite rule to upgrade from cats.data.Xor to Either #29

Closed
ShaneDelmore opened this issue Dec 11, 2016 · 6 comments
Closed

Create rewrite rule to upgrade from cats.data.Xor to Either #29

ShaneDelmore opened this issue Dec 11, 2016 · 6 comments
Assignees

Comments

@ShaneDelmore
Copy link
Contributor

ShaneDelmore commented Dec 11, 2016

Cats has removed Xor in 0.8 series and migrated to Either now that Either is right biased in Scala 2.12.

  • Rewrite usages of Xor to use Scala Either
  • Rewrite usages of Xor.Right/Left to Scala Right/Left
  • Rewrite usages of Xor methods that do not exist on Either to use Cats either enrichments
  • Rewrite usages of XorT to EitherT
  • Remove unused Xor imports
  • Add imports as needed for Either enrichments
@olafurpg
Copy link
Contributor

Quick pointers on how to get started with semantic rewrites:

  • ~scalafix-nsc/test runs the rewrite unit tests
  • you might want to start from this branch of my scalafix fork: https://github.com/olafurpg/scalafix/tree/no-postfix I've setup infrastructure there to run unit tests on a single rewrite. Just add a directory named Xor2Either (or whatever name you prefer) in core/src/main/resources and then you can write unit tests just like for the other rewrites.
  • if you need a method in SemanticApi, feel free to use add it and implement in NscSemanticApi. Don't hesitate to ask me about this part in particular as I have some ideas for useful methods that are not there yet. I might also be willing to implement the methods it if you're missing some functionality.
  • I would recommend you take a look at these three first, in that order:
    1. Rewrite usages of Xor to use Scala Either
    2. Rewrite usages of XorT to EitherT
    3. Rewrite usages of Xor methods that do not exist on Either to use Cats either enrichments

Unfortunately, I don't have more time tonight to take a look at this. But I'm happy to answer any questions tomorrow and in the coming week.

@ShaneDelmore
Copy link
Contributor Author

After doing some initial work yesterday I am leaning towards implementing base rewrites first, then combining them for specific refactorings. In this case the following seem helpful and re-usable:

  • RewriteType
  • RewriteMethod
  • IdempotentImport //naming things is hard
  • RemoveImportIfUnused

@olafurpg
Copy link
Contributor

Prototype implementation of this rewrite: master...olafurpg:xor

The trick is mainly twofold:

  • enable compiler option -Yrangepos to get start+end offsets in gtrees.
  • match against desugared version of each tree node

This approach seems to decently handle tricky cases like nested type parameters and it's agnostic to whether the user typed cats.data.Xor or Xor

@milessabin
Copy link

Bravo! Very keen to see how this turns out :-)

@ShaneDelmore
Copy link
Contributor Author

@milessabin This is actually in master now, but probably won't stay there long. My intention was only to add it as a proof of concept, longer term rewrites need to live in the libraries themselves.

@olafurpg
Copy link
Contributor

Correct. The implementation in master is there to serve as a test that such rewrites are possible to implement with scalafix. Scalafix 0.3.0 has now been released and I added some docs on how to get started with writing and running custom rewrites here: https://scalacenter.github.io/scalafix/#sbt-scalahost The docs are still slim on details, if you are interested to learn more don't hesitate to stop by on gitter.

I think we can close this issue.

stephenjudkins pushed a commit to stephenjudkins/scalafix that referenced this issue Jan 16, 2019
Skip validation if there are no sources to fix.
bjaglin pushed a commit to liancheng/scalafix that referenced this issue May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants