Skip to content

Commit 72db928

Browse files
Make sure .to rewrite is idempontent
1 parent 67c8912 commit 72db928

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ lazy val scalafixInput = project
6666
scalaVersion := scalafixScala212,
6767
scalafixSourceroot := sourceDirectory.in(Compile).value
6868
)
69+
.dependsOn(compatJVM)
6970

7071
lazy val scalafixOutput212 = project
7172
.in(file("scalafix/output212"))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
rule = "scala:fix.CrossCompat"
3+
*/
4+
package fix
5+
6+
import scala.collection.compat._
7+
object IdempotencySrc {
8+
List(1).to(Set)
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
4+
package fix
5+
6+
import scala.collection.compat._
7+
object IdempotencySrc {
8+
List(1).to(Set)
9+
}

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

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,33 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
185185
else Patch.empty
186186
}
187187

188-
def extractCollection(toCol: Tree): String = {
189-
toCol match {
190-
case Term.ApplyType(q"scala.Predef.fallbackStringCanBuildFrom", _) =>
191-
"scala.collection.immutable.IndexedSeq"
192-
case Term.ApplyType(Term.Select(coll,_), _) =>
193-
coll.syntax
194-
case Term.Apply(Term.ApplyType(Term.Select(coll, _), _), _) =>
195-
coll.syntax
196-
case Term.Select(coll,_) =>
197-
coll.syntax
198-
case _ => {
199-
throw new Exception(
200-
s"""|cannot extract collection from .to
201-
|
202-
|---------------------------------------------
203-
|syntax:
204-
|${toCol.syntax}
205-
|
206-
|---------------------------------------------
207-
|structure:
208-
|${toCol.structure}""".stripMargin
209-
)
210-
}
211-
}
212-
}
188+
def extractCollection(toCol: Tree): String = {
189+
toCol match {
190+
case Term.ApplyType(q"scala.Predef.fallbackStringCanBuildFrom", _) =>
191+
"scala.collection.immutable.IndexedSeq"
192+
case Term.ApplyType(Term.Select(coll,_), _) =>
193+
coll.syntax
194+
case Term.Apply(Term.ApplyType(Term.Select(coll, _), _), _) =>
195+
coll.syntax
196+
case Term.Select(coll,_) =>
197+
coll.syntax
198+
case coll: Type.Name =>
199+
coll.syntax
200+
case _ => {
201+
throw new Exception(
202+
s"""|cannot extract collection from .to
203+
|
204+
|---------------------------------------------
205+
|syntax:
206+
|${toCol.syntax}
207+
|
208+
|---------------------------------------------
209+
|structure:
210+
|${toCol.structure}""".stripMargin
211+
)
212+
}
213+
}
214+
}
213215

214216
def replaceToList(ctx: RuleCtx): Patch = {
215217
ctx.tree.collect {
@@ -221,13 +223,24 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
221223
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
222224
}.asPatch
223225

224-
case Term.Select(_, to @ toTpe(_)) =>
225-
val synth = ctx.index.synthetics.find(_.position.end == to.pos.end)
226-
synth.map{ s =>
227-
val Term.Apply(_, List(toCol)) = s.text.parse[Term].get
228-
val col = extractCollection(toCol)
229-
ctx.addRight(to, "(" + col + ")")
230-
}.getOrElse(Patch.empty)
226+
case t @ Term.Select(_, to @ toTpe(n: Name)) if !handledTo.contains(n) =>
227+
// we only want f.to, not f.to(X)
228+
val applied =
229+
t.parent match {
230+
case Some(_:Term.Apply) => true
231+
case _ => false
232+
}
233+
234+
if (!applied) {
235+
val synth = ctx.index.synthetics.find(_.position.end == to.pos.end)
236+
synth.map{ s =>
237+
val res = s.text.parse[Term].get
238+
val Term.Apply(_, List(toCol)) = res
239+
val col = extractCollection(toCol)
240+
ctx.addRight(to, "(" + col + ")")
241+
}.getOrElse(Patch.empty)
242+
} else Patch.empty
243+
231244
}.asPatch
232245
}
233246

0 commit comments

Comments
 (0)