Skip to content

Commit 13d6566

Browse files
Rewrite: refactor isTpe into an extractor
1 parent ae843d4 commit 13d6566

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package fix
55

66
import scala.collection
77
import scala.collection.immutable
8-
import scala.collection.mutable.{Map, Set} // Challenge to make shure the scoping is correct
8+
import scala.collection.mutable.{Map, Set} // Challenge to make sure the scoping is correct
99

1010
class SetMapSrc(iset: immutable.Set[Int],
1111
cset: collection.Set[Int],

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package fix
55

66
import scala.collection
77
import scala.collection.immutable
8-
import scala.collection.mutable.{Map, Set} // Challenge to make shure the scoping is correct
8+
import scala.collection.mutable.{Map, Set} // Challenge to make sure the scoping is correct
99

1010
class SetMapSrc(iset: immutable.Set[Int],
1111
cset: collection.Set[Int],

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,22 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
225225
// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
226226
// if we have a simple identifier, we can look at his definition at query it's type
227227
// this should be improved in future version of scalameta
228-
def isTpe(symbol: Symbol, tree: Tree, ctx: RuleCtx): Boolean =
229-
ctx.index.denotation(tree).map(_.names.headOption.exists(_.symbol == symbol)).getOrElse(false)
228+
object TypeMatcher {
229+
def apply(symbol: Symbol)(implicit index: SemanticdbIndex): TypeMatcher =
230+
new TypeMatcher(symbol)(index)
231+
}
232+
233+
final class TypeMatcher(symbol: Symbol)(implicit index: SemanticdbIndex) {
234+
def unapply(tree: Tree): Option[Unit] = {
235+
val typeMatches =
236+
index.denotation(tree)
237+
.map(_.names.headOption.exists(_.symbol == symbol))
238+
.getOrElse(false)
239+
if (typeMatches) Some(()) else None
240+
}
241+
}
230242

231-
def isCollectionSet(tree: Tree, ctx: RuleCtx): Boolean =
232-
isTpe(Symbol("_root_.scala.collection.Set#"), tree, ctx)
243+
val CollectionSet: TypeMatcher = TypeMatcher(Symbol("_root_.scala.collection.Set#"))
233244

234245
def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
235246
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
@@ -247,10 +258,10 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
247258
}
248259

249260
ctx.tree.collect {
250-
case Term.ApplyInfix(lhs, op @ setPlus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
261+
case Term.ApplyInfix(CollectionSet(_), op @ setPlus(_), Nil, List(rhs)) =>
251262
rewriteOp(op, rhs, "+", "Set")
252263

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

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

0 commit comments

Comments
 (0)