Skip to content
Permalink
Browse files

Started scheduler and broken bracket (for good reasons)

  • Loading branch information...
amarrella committed Nov 4, 2018
1 parent d616c53 commit f3d65a01a73760adcb72797d14f89c05e23ef2bd
@@ -0,0 +1,23 @@
/*
rule = Fs2v010Tov1
*/
package fix

import cats.effect._
import fs2._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

abstract class SchedulerTimer[F[_]: Effect] {

val scheduler: Scheduler
val duration = 1.second
scheduler.effect.sleep[F](duration)
scheduler.sleep[F](duration)
scheduler.sleep_[F](duration)
scheduler.awakeEvery[F](duration)
scheduler.retry(Effect[F].unit, duration, _ => duration, 1)
Stream.eval(Effect[F].unit).through(scheduler.debounce(duration))
scheduler.effect.delayCancellable(Effect[F].unit, duration)
scheduler.delay(Stream.eval(Effect[F].unit), duration)
}
@@ -0,0 +1,20 @@
package fix

import cats.effect._
import fs2._

import scala.concurrent.duration._

abstract class SchedulerTimer[F[_]: Effect] {

val scheduler: Timer[F]
val duration = 1.second
scheduler.sleep(duration)
Stream.sleep[F](duration)
Stream.sleep_[F](duration)
Stream.awakeEvery[F](duration)
Stream.retry(Effect[F].unit, duration, _ => duration, 1)
Stream.eval(Effect[F].unit).debounce(duration)
Concurrent[F].race(Effect[F].unit, s.sleep(duration))
Stream.eval(Effect[F].unit).delayBy(duration)
}
@@ -6,19 +6,13 @@ import scala.meta._

object BracketRules {
def unapply(t: Tree): Option[Patch] =
Some(
t.children
.flatMap(traverse)
.asPatch)
t.children.flatMap(traverse) match {
case Nil => None
case r => Some(r.asPatch)
}

def traverse(t: Tree): Option[Patch] =
def traverse(t: Tree): Option[Patch] = //TODO: Find a better way of doing defn brackets
t match {
case d: Defn.Val =>
Some(replaceBracket(d.rhs))
case d: Defn.Def =>
Some(replaceBracket(d.body))
case d: Defn.Var =>
d.rhs.map(replaceBracket)
case b: Term.ForYield =>
Some(b.enums.collect {
case e: Enumerator.Generator => replaceBracket(e.rhs)
@@ -79,13 +73,4 @@ object BracketRules {
case Term.Function(params, body) =>
Term.Function(params, traverseBracket(body).asInstanceOf[Term])
}

def containsBracket(p: Tree): Boolean =
p.collect {
case Term.Apply(Term.Apply(Term.Select(_, Term.Name("bracket")), _), _) =>
true
case _ =>
false
}
.exists(identity)
}
@@ -5,9 +5,12 @@ import scala.meta._

class Fs2v010Tov1 extends SemanticRule("Fs2v010Tov1") {

override def fix(implicit doc: SemanticDocument): Patch =
override def fix(implicit doc: SemanticDocument): Patch = //TODO: Maybe do something nicer than this
doc.tree.collect {
case StreamAppRules(patch) => patch
case BracketRules(patch) => patch
}.asPatch + doc.tree.collect {
case BracketRules(patch) => patch
}.asPatch + doc.tree.collect {
case SchedulerRules(patch) => patch
}.asPatch
}
@@ -0,0 +1,83 @@
package fix
import scalafix.v1._
import scala.meta._

object SchedulerRules {

def unapply(t: Tree)(implicit doc: SemanticDocument): Option[Patch] =
t match {
case t @ scheduler(_: Type.Name) =>
Some(Patch.replaceTree(t, timer(Type.Name("F")).toString())) //TODO: Use actual effect
case Term.ApplyType(scheduler(_), List(f)) =>
Some(Patch.replaceTree(t, timer(f).toString()))
case sched @ Term.Apply(
Term.ApplyType(Term.Select(Term.Select(s, Term.Name("effect")), Term.Name("sleep")), _),
List(d)) => //TODO: use symbol matcher to make sure we are changing fs2 scheduler
val timerSleep = Term.Apply(Term.Select(s, Term.Name("sleep")), List(d))
Some(Patch.replaceTree(sched, timerSleep.toString()))
case sched @ Term
.Apply(Term.ApplyType(Term.Select(s, Term.Name("sleep")), List(f)), List(d)) =>
val stream = Term.Apply(
Term.ApplyType(Term.Select(Term.Name("Stream"), Term.Name("sleep")), List(f)),
List(d))
Some(Patch.replaceTree(sched, stream.toString()))
case sched @ Term
.Apply(Term.ApplyType(Term.Select(s, Term.Name("sleep_")), List(f)), List(d)) =>
val stream = Term.Apply(
Term.ApplyType(Term.Select(Term.Name("Stream"), Term.Name("sleep_")), List(f)),
List(d))
Some(Patch.replaceTree(sched, stream.toString()))
case sched @ Term
.Apply(Term.ApplyType(Term.Select(s, Term.Name("awakeEvery")), List(f)), List(d)) =>
val stream = Term.Apply(
Term.ApplyType(Term.Select(Term.Name("Stream"), Term.Name("awakeEvery")), List(f)),
List(d))
Some(Patch.replaceTree(sched, stream.toString()))
case sched @ Term
.Apply(Term.Select(s, Term.Name("retry")), params) =>
val stream = Term.Apply(Term.Select(Term.Name("Stream"), Term.Name("retry")), params)
Some(Patch.replaceTree(sched, stream.toString()))
case sched @ Term.Apply(
Term.Select(
Term.Apply(
Term.Select(Term.Name("Stream"), Term.Name("eval")),
List(
Term.Select(
Term.ApplyType(Term.Name("Effect"), List(Type.Name("F"))),
Term.Name("unit")
)
)
),
Term.Name("through")
),
List(
Term.Apply(
Term.Select(Term.Name("scheduler"), Term.Name("debounce")),
List(Term.Name("duration"))
)
)
) => //TODO
println(sched.structure)
None
case _ => None
}

def stream(f: Type, a: Term): Term =
f match {
case Type.Name("Pure") =>
Term.Apply(Term.Select(Term.Name("Stream"), Term.Name("emit")), List(a))
case _ =>
Term.Apply(
Term.Select(Term.Name("Stream"), Term.Name("eval")),
List(
Term.Apply(
Term.Select(Term.ApplyType(Term.Name("Applicative"), List(f)), Term.Name("pure")),
List(a)))
)
}

def timer(f: Type) = Type.Apply(Type.Name("Timer"), List(f))

val scheduler = SymbolMatcher.normalized("fs2/Scheduler")

}

0 comments on commit f3d65a0

Please sign in to comment.
You can’t perform that action at this time.