Skip to content

Commit a58863b

Browse files
Rewrite future => Future (fix scala#139)
1 parent 7ff8323 commit a58863b

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

scalafix/input/src/main/scala/fix/FutureSrc.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rule = "scala:fix.CrossCompat"
33
*/
44
package fix
55

6-
import scala.concurrent.{Future, ExecutionContext}
6+
import scala.concurrent.{Future, ExecutionContext, future}
77
import java.lang.Throwable
88

99
class FutureSrc(fs: Future[Int])(implicit ec: ExecutionContext){
@@ -21,4 +21,11 @@ class FutureSrc(fs: Future[Int])(implicit ec: ExecutionContext){
2121
case x if x > 0 => println("x > 0")
2222
case x if x < 0 => println("x < 0")
2323
}(ec)
24+
25+
fs.map(identity).onFailure {
26+
case x => x
27+
}
28+
fs.map(identity).onSuccess { case x => x }
29+
30+
val f = future { 1 }
2431
}

scalafix/output/src/main/scala/fix/FutureSrc.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ class FutureSrc(fs: Future[Int])(implicit ec: ExecutionContext){
2323
case scala.util.Success(x) if x < 0 => println("x < 0")
2424
case _ => ()
2525
}(ec)
26+
27+
fs.map(identity).onComplete {
28+
case scala.util.Failure(x) => x
29+
case _ => ()
30+
}
31+
fs.map(identity).onComplete { case scala.util.Success(x) => x
32+
case _ => () }
33+
34+
val f = Future { 1 }
2635
}

scalafix/rules/src/main/scala/fix/Roughly.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ final case class Roughly(index: SemanticdbIndex, config: RoughlyConfig)
7171
ctx.tree.collect {
7272
case ap @ Term.ApplyInfix(_, mapValues(_), _, _) if strictMapValues =>
7373
ctx.addLeft(ap, "(") +
74-
ctx.addRight(ap, ").toMap")
74+
ctx.addRight(ap, ").toMap")
7575

7676
case ap @ Term.Apply(Term.Select(_, mapValues(_)), List(_)) if strictMapValues =>
7777
ctx.addRight(ap, ".toMap")
7878

7979
case ap @ Term.ApplyInfix(_, filterKeys(_), _, _) if strictFilterKeys =>
8080
ctx.addLeft(ap, "(") +
81-
ctx.addRight(ap, ").toMap")
81+
ctx.addRight(ap, ").toMap")
8282

8383
case ap @ Term.Apply(Term.Select(_, filterKeys(_)), List(_)) if strictFilterKeys =>
8484
ctx.addRight(ap, ".toMap")

scalafix/rules/src/main/scala/fix/Stable212Base.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,22 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
339339
ctx.addRight(cases.last, nl + indent + "case _ => ()")
340340
}
341341

342-
ctx.tree.collect {
343-
case Term.Apply(Term.Select(_, f @ `Future.onFailure`(_)),
344-
List(Term.PartialFunction(cases))) =>
345-
toOnCompletePF(f, cases, "scala.util.Failure")
342+
val toOnCompelete =
343+
ctx.tree.collect {
344+
case Term.Apply(Term.Select(_, f @ `Future.onFailure`(_)),
345+
List(Term.PartialFunction(cases))) =>
346+
toOnCompletePF(f, cases, "scala.util.Failure")
346347

347-
case Term.Apply(Term.Select(_, f @ `Future.onSuccess`(_)),
348-
List(Term.PartialFunction(cases))) =>
349-
toOnCompletePF(f, cases, "scala.util.Success")
348+
case Term.Apply(Term.Select(_, f @ `Future.onSuccess`(_)),
349+
List(Term.PartialFunction(cases))) =>
350+
toOnCompletePF(f, cases, "scala.util.Success")
351+
}.asPatch
350352

351-
}.asPatch
353+
val toFuture = ctx.replaceSymbols(
354+
"scala.concurrent.future" -> "scala.concurrent.Future"
355+
)
356+
357+
toOnCompelete + toFuture
352358
}
353359

354360
private def replaceSorted(ctx: RuleCtx): Patch = {

scalafix/tests/src/test/scala/fix/ScalafixTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ScalafixTests
1717
)
1818
) {
1919

20-
runAllTests()
20+
// runAllTests()
2121
// to run only one test:
22-
// testsToRun.filter(_.filename.toNIO.getFileName.toString == "Playground.scala" ).foreach(runOn)
22+
testsToRun.filter(_.filename.toNIO.getFileName.toString == "FutureSrc.scala").foreach(runOn)
2323
}

0 commit comments

Comments
 (0)