-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add scalafix for contramap #1937
Conversation
case class ContraMapToLMap(index: SemanticdbIndex) | ||
extends SemanticRule(index, "UseLMapInsteadOfContraMap") { | ||
|
||
//val contraMap = Symbol("_root_.cats.functor.Contravariant.Ops.contramap") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dead code?
ctx.tree.collect { | ||
case Term.Apply(fun, _) => | ||
if (contraMatcher.matches(fun) && | ||
fun.children.headOption.flatMap(index.denotation).exists{ x => println(x.name == unApplyName); x.name == unApplyName }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this println
intentionally left?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This denotation trick is nice
Btw, it might be good to test these with CI in the future :) |
It's not? How did you test it locally? Is it hard to add it to travis? |
It's a different sbt project, let me see what I can do :) |
Codecov Report
@@ Coverage Diff @@
## master #1937 +/- ##
=========================================
+ Coverage 95.57% 95.6% +0.02%
=========================================
Files 248 252 +4
Lines 4454 4570 +116
Branches 126 120 -6
=========================================
+ Hits 4257 4369 +112
- Misses 197 201 +4
Continue to review full report at Codecov.
|
.travis.yml
Outdated
@@ -30,6 +30,11 @@ matrix: | |||
before_install: | |||
- export PATH=${PATH}:./vendor/bundle | |||
|
|||
before_script: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding this to https://github.com/typelevel/cats/blob/master/scripts/travis-publish.sh might be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Just want to test for now :)
3ac35a6
to
f6c024f
Compare
d30db27
to
08c85a7
Compare
Another PR added some rules that only applies to the yet to release RC1. So we need to let the output depends on a snapshot of head, need to publish the head snapshot locally first at CI. |
Seems like a good idea 👍 I'm not sure how to go forward with that though, you have any pointers? |
@LukaJCB, change here https://github.com/typelevel/cats/blob/master/scalafix/build.sbt#L17 sbt ;coreJVM/publishLocal;freeJVM/publishLocal
cd scalafix
sbt tests/test
cd .. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left some minor comments on Scalafix api usage, I also suspect the denotation lookup could be easier (still not sure).
EDIT: as discussed on gitter, this is a simpler alternative for matching contramap
in the desired cases:
ctx.tree.collect {
case Term.Apply(Term.Select(f, contraMatcher(contramap)), _) if f.denotation.exists(_.name == unApplyName) =>
ctx.replaceTree(contramap, "lmap")
}.asPatch
val unApplyName = "catsUnapply2left" | ||
|
||
ctx.tree.collect { | ||
case Term.Apply(fun, _) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SymbolMatches have an unapply
method, so you can use it like
case Term.Apply(contraMatcher(fun), _) =>
case Term.Apply(fun, _) => | ||
if (contraMatcher.matches(fun) && | ||
fun.children.headOption.flatMap(index.denotation).exists(_.name == unApplyName )) { | ||
fun.children.find(contraMatcher.matches).map(tree => ctx.replaceTree(tree, "lmap")).getOrElse(Patch.empty) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.getOrElse(Patch.empty)
can become .asPatch
} else { | ||
Patch.empty | ||
} | ||
case _ => Patch.empty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for this case, since this is a collect
I simplified a lot thanks to @gabro! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it
@@ -34,6 +35,11 @@ free_js="$sbt_cmd validateFreeJS" | |||
js="$core_js && $free_js && $kernel_js" | |||
jvm="$sbt_cmd coverage validateJVM coverageReport && codecov" | |||
|
|||
sbt ;coreJVM/publishLocal;freeJVM/publishLocal | |||
cd scalafix | |||
sbt tests/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expected this to add a lot to build time, but apparently not!
Merging with two sign offs :) |
This PR provides an automatic fix for #1850, which should provide a seamless upgrading experience :)