Skip to content

Commit

Permalink
Rewrite collection.Set.+ restrict type of lhs
Browse files Browse the repository at this point in the history
scalafix allow us to match on SetLike.+ but we cannot check the type of the expression on the lhs

We can work arround this limitation if the lhs is a identifier (simple case). This allow us
to document the expected type on the lhs and prepare the implementation once scalameta/scalameta#1212 is resolved
  • Loading branch information
MasseGuillaume committed Jun 25, 2018
1 parent 62d982c commit ae843d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 4 additions & 2 deletions scalafix/input/src/main/scala/fix/SetMapSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class SetMapSrc(iset: immutable.Set[Int],
imap + (2 -> 3, 3 -> 4)
(iset + (2, 3)).toString
iset + (2, 3) - 4
iset + 1 - 2
cset + 1 - 2
iset + 1
iset - 2
cset + 1
cset - 2
cmap + (2 -> 3) + ((4, 5))
}
6 changes: 4 additions & 2 deletions scalafix/output/src/main/scala/fix/SetMapSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class SetMapSrc(iset: immutable.Set[Int],
imap + (2 -> 3) + (3 -> 4)
(iset + 2 + 3).toString
iset + 2 + 3 - 4
iset + 1 - 2
cset ++ _root_.scala.collection.Set(1) -- _root_.scala.collection.Set(2)
iset + 1
iset - 2
cset ++ _root_.scala.collection.Set(1)
cset -- _root_.scala.collection.Set(2)
cmap ++ _root_.scala.collection.Map(2 -> 3) ++ _root_.scala.collection.Map((4, 5))
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
}.asPatch
}

// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
// if we have a simple identifier, we can look at his definition at query it's type
// this should be improved in future version of scalameta
def isTpe(symbol: Symbol, tree: Tree, ctx: RuleCtx): Boolean =
ctx.index.denotation(tree).map(_.names.headOption.exists(_.symbol == symbol)).getOrElse(false)

def isCollectionSet(tree: Tree, ctx: RuleCtx): Boolean =
isTpe(Symbol("_root_.scala.collection.Set#"), tree, ctx)

def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
val col = "_root_.scala.collection." + col0
Expand All @@ -238,10 +247,10 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
}

ctx.tree.collect {
case Term.ApplyInfix(_, op @ setPlus(_), Nil, List(rhs)) =>
case Term.ApplyInfix(lhs, op @ setPlus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
rewriteOp(op, rhs, "+", "Set")

case Term.ApplyInfix(lhs, op @ setMinus(_), Nil, List(rhs)) =>
case Term.ApplyInfix(lhs, op @ setMinus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
rewriteOp(op, rhs, "-", "Set")

case Term.ApplyInfix(lhs, op @ mapPlus(_), Nil, List(rhs)) =>
Expand Down

0 comments on commit ae843d4

Please sign in to comment.