Skip to content

Commit 6b5b7fb

Browse files
Rewrite: refactor isTpe into an extractor
1 parent 0d63ea8 commit 6b5b7fb

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

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

233244
def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
234245
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
@@ -246,10 +257,10 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
246257
}
247258

248259
ctx.tree.collect {
249-
case Term.ApplyInfix(lhs, op @ setPlus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
260+
case Term.ApplyInfix(CollectionSet(_), op @ setPlus(_), Nil, List(rhs)) =>
250261
rewriteOp(op, rhs, "+", "Set")
251262

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

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

0 commit comments

Comments
 (0)