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

Add scalafix for contramap #1937

Merged
merged 6 commits into from
Oct 10, 2017
Merged

Conversation

LukaJCB
Copy link
Member

@LukaJCB LukaJCB commented Sep 29, 2017

This PR provides an automatic fix for #1850, which should provide a seamless upgrading experience :)

case class ContraMapToLMap(index: SemanticdbIndex)
extends SemanticRule(index, "UseLMapInsteadOfContraMap") {

//val contraMap = Symbol("_root_.cats.functor.Contravariant.Ops.contramap")
Copy link
Contributor

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 }) {
Copy link
Contributor

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?

Copy link
Contributor

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

@kailuowang
Copy link
Contributor

This is awesome! @LukaJCB . I would say this fixes #1850

@LukaJCB
Copy link
Member Author

LukaJCB commented Sep 29, 2017

Btw, it might be good to test these with CI in the future :)

@kailuowang
Copy link
Contributor

It's not? How did you test it locally? Is it hard to add it to travis?

@LukaJCB
Copy link
Member Author

LukaJCB commented Sep 29, 2017

It's a different sbt project, let me see what I can do :)

@codecov-io
Copy link

codecov-io commented Sep 29, 2017

Codecov Report

Merging #1937 into master will increase coverage by 0.02%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
kernel/src/main/scala/cats/kernel/Order.scala 81.08% <0%> (-5.41%) ⬇️
laws/src/main/scala/cats/laws/discipline/Eq.scala 90.9% <0%> (-2.2%) ⬇️
core/src/main/scala/cats/InvariantMonoidal.scala 0% <0%> (ø) ⬆️
...nel/src/main/scala/cats/kernel/instances/map.scala 100% <0%> (ø) ⬆️
core/src/main/scala/cats/Show.scala 100% <0%> (ø) ⬆️
...in/scala/cats/laws/discipline/BifunctorTests.scala 100% <0%> (ø) ⬆️
core/src/main/scala/cats/Functor.scala 100% <0%> (ø) ⬆️
.../src/main/scala/cats/kernel/instances/option.scala 100% <0%> (ø) ⬆️
core/src/main/scala/cats/Composed.scala 95.83% <0%> (ø) ⬆️
.../src/main/scala/cats/kernel/instances/vector.scala 100% <0%> (ø) ⬆️
... and 43 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e33b29...eebcf02. Read the comment docs.

.travis.yml Outdated
@@ -30,6 +30,11 @@ matrix:
before_install:
- export PATH=${PATH}:./vendor/bundle

before_script:
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

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 :)

@LukaJCB LukaJCB added this to the 1.0.0-RC1 milestone Oct 1, 2017
kailuowang
kailuowang previously approved these changes Oct 3, 2017
@kailuowang
Copy link
Contributor

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.

@LukaJCB
Copy link
Member Author

LukaJCB commented Oct 4, 2017

Seems like a good idea 👍 I'm not sure how to go forward with that though, you have any pointers?

@kailuowang
Copy link
Contributor

@LukaJCB, change here https://github.com/typelevel/cats/blob/master/scalafix/build.sbt#L17
to point to 1.0.0-SNAPSHOT instead of 1.0.0-MF.
Then in the travis build script add another step "sbt publishLocal" so it's

sbt ;coreJVM/publishLocal;freeJVM/publishLocal
cd scalafix
sbt tests/test
cd ..

kailuowang
kailuowang previously approved these changes Oct 5, 2017
Copy link
Contributor

@gabro gabro left a 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, _) =>
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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

@LukaJCB
Copy link
Member Author

LukaJCB commented Oct 5, 2017

I simplified a lot thanks to @gabro! 🎉

Copy link
Contributor

@edmundnoble edmundnoble left a 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
Copy link
Contributor

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!

@LukaJCB
Copy link
Member Author

LukaJCB commented Oct 10, 2017

Merging with two sign offs :)

@LukaJCB LukaJCB merged commit b3244a8 into typelevel:master Oct 10, 2017
@kailuowang kailuowang modified the milestone: 1.0.0-RC1 Oct 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants