Skip to content

Commit 9f2d274

Browse files
Rewrite CanBuildFrom => Factory without replaceTree
There is a conflict with the replaceSymbol Patch
1 parent 2896fb5 commit 9f2d274

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package fix
55

66
import scala.language.higherKinds
77

8+
import scala.collection.immutable
89
import collection.generic.CanBuildFrom
9-
import collection.immutable
1010

1111
object CanBuildFromSrc {
1212

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package fix
55

66
import scala.language.higherKinds
77

8-
import collection.immutable
8+
import scala.collection.immutable
9+
import scala.collection.immutable
910
import scala.collection.compat._
1011

1112
object CanBuildFromSrc {

scalafix/rules/src/main/scala/scala/fix/collection/CanBuildFrom.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ object CanBuildFromNothing {
9999
tpe @ Type.Apply(
100100
collectionCanBuildFrom(_),
101101
List(
102-
nothing(_),
102+
not @ nothing(_),
103103
t,
104104
toT
105105
)
106106
)
107107
),
108108
_
109109
) =>
110-
new CanBuildFromNothing(param, tpe, t, toT, stats, ctx, toTpe, handledTo)
110+
new CanBuildFromNothing(param, tpe, not, t, toT, stats, ctx, toTpe, handledTo)
111111
}
112112
.map(_.toFactory)
113113
.asPatch
@@ -124,6 +124,7 @@ object CanBuildFromNothing {
124124
// toT : CC[Int]
125125
case class CanBuildFromNothing(param: Name,
126126
tpe: Type.Apply,
127+
not: Type,
127128
t: Type,
128129
toT: Type,
129130
stats: List[Tree],
@@ -197,11 +198,15 @@ case class CanBuildFromNothing(param: Name,
197198

198199
// implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] =>
199200
// implicit cbf: Factory[Int, CC[Int]]
200-
val parameterType =
201-
ctx.replaceTree(
202-
tpe,
203-
Type.Apply(Type.Name("Factory"), List(t, toT)).syntax
204-
)
201+
val parameterType = {
202+
val comma = ctx.tokenList.trailing(not.tokens.last).find(_.is[Token.Comma]).get
203+
val spaceOpt = ctx.tokenList.trailing(comma).find(_.is[Token.Space])
204+
205+
ctx.removeTokens(not.tokens) +
206+
ctx.removeToken(comma) +
207+
spaceOpt.map(ctx.removeToken).getOrElse(Patch.empty) +
208+
ctx.replaceTree(tpe.tpe, "Factory")
209+
}
205210

206211
parameterType + cbfCalls + toCalls
207212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import scalafix.testkit._
77
class ScalafixTests extends scalafix.testkit.SemanticRuleSuite {
88

99
val only: Option[String] =
10-
// Some("TraversableSrc") // << to run only one test:
10+
// Some("Playground") // << to run only one test:
1111
None
1212

1313
def testOnly(file: String): Unit = {

0 commit comments

Comments
 (0)