Guest User

Untitled

a guest
May 28th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. package fix
  2.  
  3. import scalafix._
  4. import scala.meta._
  5.  
  6. case object Viewbounds_v1_0 extends Rule("Viewbounds_v1_0") {
  7.  
  8. def eliminateViewBound(defn: Defn.Def): Option[Defn.Def] = {
  9.  
  10. def makeimplicit(i: Int, from: Name, to: Type): Term.Param = {
  11. val funname = s"viewbound_$i"
  12. val expr = s"implicit viewbound_$i: $from => $to"
  13. expr.parse[Term.Param].get
  14. }
  15.  
  16. def isImplParameterList(params: List[Term.Param]): Boolean = params.exists {
  17. case Term.Param(mods, _, _, _) => mods.exists(_.is[Mod.Implicit])
  18. }
  19.  
  20. defn match {
  21. case Defn.Def(x, name, typeparams, parameterlists, ascription, body) => {
  22. val (regular, impl) = parameterlists.span(pl => !isImplParameterList(pl))
  23. val (nobounds, names, bounds) = typeparams.map {
  24. case Type.Param(mods, name, tparams, typebounds, viewbounds, contextbounds) => (Type.Param(mods, name, tparams, typebounds, Nil, contextbounds), name, viewbounds)
  25. }.unzip3
  26.  
  27. val b = names.zip(bounds).flatMap{
  28. case (name, targets) => targets.map(to => (name, to))
  29. }
  30.  
  31. val convertedparams = b.zipWithIndex.map{ case ((from, to), i) => makeimplicit(i, from, to)}
  32. if (convertedparams.nonEmpty) {
  33. val totalimplicits = impl.flatten ::: convertedparams
  34. val paramlists = regular :+ totalimplicits
  35. Some(Defn.Def(x, name, nobounds, paramlists, ascription, body))
  36. } else None
  37. }
  38. }
  39. }
  40.  
  41. override def description: String =
  42. "Rewrite that replaces viewbounds with an implicit conversion parameter"
  43.  
  44. override def fix(ctx: RuleCtx): Patch = {
  45. ctx.tree.collect {
  46. case defn: Defn.Def => {
  47. val replacement = eliminateViewBound(defn)
  48. replacement.map(repl => ctx.replaceTree(defn, repl.toString)).getOrElse(Patch.empty)
  49. }
  50. }.asPatch
  51. }
  52.  
  53. }
Add Comment
Please, Sign In to add comment