Skip to content

Commit

Permalink
Handle primitives plus string
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 18, 2019
1 parent 6829a8f commit 7319678
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
23 changes: 15 additions & 8 deletions input/src/main/scala/fix/scala213/Any2StringAdd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ abstract class Any2StringAdd {
//
def unit1 = unit + s
def bool1 = bool + s
def byte1 = byte + s // TODO
def short1 = short + s // TODO
def char1 = char + s // TODO
def int1 = int + s // TODO
def long1 = long + s // TODO
def float1 = float + s // TODO
def double1 = double + s // TODO
def byte1 = byte + s
def byte2 = byte + byte
def short1 = short + s
def short2 = short + short
def char1 = char + s
def char2 = char + char
def int1 = int + s
def int2 = int + int
def long1 = long + s
def long2 = long + long
def float1 = float + s
def float2 = float + float
def double1 = double + s
def double2 = double + double

// With infix operators, make sure to use parens
def parens1 = Nil ++ Nil + s
def parens2 = int + int + s // TODO
def parens2 = int + int + s
def parens3 = {Nil ++ Nil} + s
}
23 changes: 15 additions & 8 deletions output/src/main/scala/fix/scala213/Any2StringAdd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ abstract class Any2StringAdd {
//
def unit1 = unit.toString + s
def bool1 = bool.toString + s
def byte1 = byte + s // TODO
def short1 = short + s // TODO
def char1 = char + s // TODO
def int1 = int + s // TODO
def long1 = long + s // TODO
def float1 = float + s // TODO
def double1 = double + s // TODO
def byte1 = byte.toString + s
def byte2 = byte + byte
def short1 = short.toString + s
def short2 = short + short
def char1 = char.toString + s
def char2 = char + char
def int1 = int.toString + s
def int2 = int + int
def long1 = long.toString + s
def long2 = long + long
def float1 = float.toString + s
def float2 = float + float
def double1 = double.toString + s
def double2 = double + double

// With infix operators, make sure to use parens
def parens1 = (Nil ++ Nil).toString + s
def parens2 = int + int + s // TODO
def parens2 = (int + int).toString + s
def parens3 = {Nil ++ Nil}.toString + s
}
22 changes: 16 additions & 6 deletions rewrites/src/main/scala/fix/scala213/Any2StringAdd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ import scalafix.v1._
import scala.meta._

object Any2StringAdd {
val any2stringaddPlus = SymbolMatcher.exact("scala/Predef.any2stringadd#`+`().")
val plusString = SymbolMatcher.exact(
"scala/Predef.any2stringadd#`+`().",
"scala/Byte#`+`().",
"scala/Short#`+`().",
"scala/Char#`+`().",
"scala/Int#`+`().",
"scala/Long#`+`().",
"scala/Float#`+`().",
"scala/Double#`+`().",
)
}

final class Any2StringAdd extends SemanticRule("fix.scala213.Any2StringAdd") {
import Any2StringAdd._

override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case any2stringaddPlus(Term.ApplyInfix(lhs, _, _, _)) =>
lhs match {
case _: Term.Name | _: Term.Select | _: Term.Block => Patch.addRight(lhs, ".toString")
case _ => Patch.addLeft(lhs, "(") + Patch.addRight(lhs, ").toString")
}
case plusString(Term.ApplyInfix(lhs, _, _, _)) => addToString(lhs)
}.asPatch
}

private def addToString(term: Term) = term match {
case _: Term.Name | _: Term.Select | _: Term.Block => Patch.addRight(term, ".toString")
case _ => Patch.addLeft(term, "(") + Patch.addRight(term, ").toString")
}
}

0 comments on commit 7319678

Please sign in to comment.