Skip to content

Commit 5dada85

Browse files
Rewrite: Move LazyList and Map.mapValue rules to separate rules (fix scala#56)
1 parent f823520 commit 5dada85

25 files changed

+114
-78
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ report it as described in the
5454
Run the following sbt task on your project:
5555

5656
~~~
57-
> scalafix github:scala/scala-collection-compat/NewCollections
57+
> scalafix github:scala/scala-collection-compat/Stable
5858
~~~

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

@@ -8,5 +8,4 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3, 3 -> 4)
99
(set + (2, 3)).toString
1010
set + (2, 3) - 4
11-
map.mapValues(_ + 1)
12-
}
11+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.Stable"
33
*/
44
package fix
55

@@ -14,7 +14,7 @@ object Collectionstrawman_v0_Tuple2Zipped {
1414
ys).zipped
1515
/* a */(/* b */ xs /* c */, /* d */ ys /* e */)/* f */./* g */zipped/* h */
1616
(coll(1), coll(2)).zipped
17-
(List(1, 2, 3), Stream.from(1)).zipped
17+
(List(1, 2, 3), Array(1)).zipped
1818
}
1919
def coll(x: Int): List[Int] = ???
2020
}
@@ -30,7 +30,7 @@ object Collectionstrawman_v0_Tuple3Zipped {
3030
zs).zipped
3131
/* a */(/* b */ xs /* c */, /* d */ ys /* e */, /* f */ zs /* g */)/* h */./* i */zipped/* j */
3232
(coll(1), coll(2), coll(3)).zipped
33-
(List(1, 2, 3), Set(1, 2, 3), Stream.from(1)).zipped
33+
(List(1, 2, 3), Set(1, 2, 3), Array(1)).zipped
3434
}
3535
def coll(x: Int): List[Int] = ???
3636
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
rule = "scala:fix.UnstableMapValues"
3+
*/
4+
package fix
5+
6+
class UnstableMapValuesSrc(map: Map[Int, Int]) {
7+
map.mapValues(_ + 1)
8+
}

scalafix/input/src/main/scala/fix/StreamSrc.scala renamed to scalafix/input/src/main/scala/fix/UnstableStreamToLazyListSrc.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
rule = "scala:fix.Scalacollectioncompat_newcollections"
2+
rule = "scala:fix.UnstableStreamToLazyList"
33
*/
44
package fix
55

6-
object StreamSrc {
6+
class UnstableStreamToLazyListSrc() {
77
val s = Stream(1, 2, 3)
88
s.append(List(4, 5, 6))
99
1 #:: 2 #:: 3 #:: Stream.Empty

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
package fix
25

36
import scala.collection.mutable

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
package fix
25

36
import scala.collection.mutable.{Map, Set}

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

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3) + (3 -> 4)
99
(set + 2 + 3).toString
1010
set + 2 + 3 - 4
11-
map.mapValues(_ + 1).toMap
1211
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
package fix
25

36
object TraversableSrc {

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
package fix
25

36
import scala.language.postfixOps
@@ -11,7 +14,7 @@ object Collectionstrawman_v0_Tuple2Zipped {
1114
ys)
1215
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */)/* f *//* g *//* h */
1316
coll(1).lazyZip(coll(2))
14-
List(1, 2, 3).lazyZip(LazyList.from(1))
17+
List(1, 2, 3).lazyZip(Array(1))
1518
}
1619
def coll(x: Int): List[Int] = ???
1720
}
@@ -27,7 +30,7 @@ object Collectionstrawman_v0_Tuple3Zipped {
2730
zs)
2831
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */).lazyZip(/* f */ zs /* g */)/* h *//* i *//* j */
2932
coll(1).lazyZip(coll(2)).lazyZip(coll(3))
30-
List(1, 2, 3).lazyZip(Set(1, 2, 3)).lazyZip(LazyList.from(1))
33+
List(1, 2, 3).lazyZip(Set(1, 2, 3)).lazyZip(Array(1))
3134
}
3235
def coll(x: Int): List[Int] = ???
3336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
4+
package fix
5+
6+
class UnstableMapValuesSrc(map: Map[Int, Int]) {
7+
map.mapValues(_ + 1).toMap
8+
}

scalafix/output/src/main/scala/fix/StreamSrc.scala renamed to scalafix/output/src/main/scala/fix/UnstableStreamToLazyListSrc.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
2+
3+
14
package fix
25

3-
object StreamSrc {
6+
class UnstableStreamToLazyListSrc() {
47
val s = LazyList(1, 2, 3)
58
s.lazyAppendedAll(List(4, 5, 6))
69
1 #:: 2 #:: 3 #:: LazyList.Empty
+3-55
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import scalafix.syntax._
55
import scalafix.util._
66
import scala.meta._
77

8-
case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
9-
extends SemanticRule(index, "Scalacollectioncompat_newcollections") {
8+
case class Stable(index: SemanticdbIndex) extends SemanticRule(index, "Stable") {
109

1110
// Two rules triggers the same rewrite TraversableLike.to and CanBuildFrom
1211
// we keep track of what is handled in CanBuildFrom and guard against TraversableLike.to
@@ -26,36 +25,9 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
2625
close <- ctx.matchingParens.close(open)
2726
} yield (open, close)
2827

29-
// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
30-
// WARNING: TOTAL HACK
31-
// this is only to unblock us until Term.tpe is available: https://github.com/scalameta/scalameta/issues/1212
32-
// if we have a simple identifier, we can look at his definition at query it's type
33-
// this should be improved in future version of scalameta
34-
object TypeMatcher {
35-
def apply(symbols: Symbol*)(implicit index: SemanticdbIndex): TypeMatcher =
36-
new TypeMatcher(symbols: _*)(index)
37-
}
38-
39-
final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
40-
def unapply(tree: Tree): Boolean = {
41-
index.denotation(tree)
42-
.exists(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
43-
}
44-
}
45-
46-
val CollectionMap: TypeMatcher = TypeMatcher(
47-
Symbol("_root_.scala.collection.immutable.Map#"),
48-
Symbol("_root_.scala.collection.mutable.Map#"),
49-
Symbol("_root_.scala.Predef.Map#")
50-
)
51-
52-
val CollectionSet: TypeMatcher = TypeMatcher(Symbol("_root_.scala.collection.Set#"))
53-
5428
def replaceSymbols(ctx: RuleCtx): Patch = {
5529
ctx.replaceSymbols(
5630
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
57-
"scala.Stream" -> "scala.LazyList",
58-
"scala.collection.immutable.Stream" -> "scala.collection.immutable.LazyList",
5931
"scala.Traversable" -> "scala.Iterable",
6032
"scala.collection.Traversable" -> "scala.collection.Iterable",
6133
"scala.TraversableOnce" -> "scala.IterableOnce",
@@ -125,10 +97,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
12597
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
12698
)
12799

128-
val mapMapValues =
129-
SymbolMatcher.exact(
130-
Symbol("_root_.scala.collection.immutable.MapLike#mapValues(Lscala/Function1;)Lscala/collection/immutable/Map;.")
131-
)
100+
132101

133102
val retainSet =
134103
SymbolMatcher.normalized(
@@ -232,16 +201,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
232201
ctx.replaceTree(t, q"$buffer ++= $collection".syntax)
233202
}.asPatch
234203

235-
val streamAppend = SymbolMatcher.normalized(
236-
Symbol("_root_.scala.collection.immutable.Stream.append.")
237-
)
238-
239-
def replaceStreamAppend(ctx: RuleCtx): Patch =
240-
ctx.tree.collect {
241-
case streamAppend(t: Name) =>
242-
ctx.replaceTree(t, "lazyAppendedAll")
243-
}.asPatch
244-
245204
def replaceSetMapPlus2(ctx: RuleCtx): Patch = {
246205
def rewritePlus(ap: Term.ApplyInfix, lhs: Term, op: Term.Name, rhs1: Term, rhs2: Term): Patch = {
247206
val tokensToReplace =
@@ -311,15 +270,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
311270
}.asPatch
312271
}
313272

314-
315-
316-
def replaceMapMapValues(ctx: RuleCtx): Patch = {
317-
ctx.tree.collect {
318-
case ap @ Term.Apply(Term.Select(_, mapMapValues(_)), List(_)) =>
319-
ctx.addRight(ap, ".toMap")
320-
}.asPatch
321-
}
322-
323273
object CanBuildFromNothing {
324274
def apply(paramss: List[List[Term.Param]], body: Term, ctx: RuleCtx): Patch = {
325275
paramss.flatten.collect{
@@ -507,15 +457,13 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
507457
replaceSymbols(ctx) +
508458
replaceTupleZipped(ctx) +
509459
replaceCopyToBuffer(ctx) +
510-
replaceStreamAppend(ctx) +
511460
replaceMutableMap(ctx) +
512461
replaceMutableSet(ctx) +
513462
replaceSymbolicFold(ctx) +
514463
replaceSetMapPlus2(ctx) +
515464
replaceMutSetMapPlus(ctx) +
516465
replaceMutMapUpdated(ctx) +
517466
replaceArrayBuilderMake(ctx) +
518-
replaceIterableSameElements(ctx) +
519-
replaceMapMapValues(ctx)
467+
replaceIterableSameElements(ctx)
520468
}
521469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fix
2+
3+
import scalafix._
4+
import scalafix.syntax._
5+
import scalafix.util._
6+
import scala.meta._
7+
8+
9+
/*
10+
* This rules is marked unstable since Map.mapValues was lazy
11+
*/
12+
case class UnstableMapValues(index: SemanticdbIndex) extends SemanticRule(index, "UnstableMapValues") {
13+
val mapMapValues =
14+
SymbolMatcher.exact(
15+
Symbol("_root_.scala.collection.immutable.MapLike#mapValues(Lscala/Function1;)Lscala/collection/immutable/Map;.")
16+
)
17+
18+
override def fix(ctx: RuleCtx): Patch = {
19+
ctx.tree.collect {
20+
case ap @ Term.Apply(Term.Select(_, mapMapValues(_)), List(_)) =>
21+
ctx.addRight(ap, ".toMap")
22+
}.asPatch
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fix
2+
3+
import scalafix._
4+
import scalafix.syntax._
5+
import scalafix.util._
6+
import scala.meta._
7+
8+
/*
9+
* This rules is marked unstable since Stream is not strictly equivalent to LazyList.
10+
* LazyList has a lazy head but not Stream
11+
*/
12+
case class UnstableStreamToLazyList(index: SemanticdbIndex) extends SemanticRule(index, "UnstableStreamToLazyList") {
13+
14+
val streamAppend = SymbolMatcher.normalized(
15+
Symbol("_root_.scala.collection.immutable.Stream.append.")
16+
)
17+
18+
def replaceStreamAppend(ctx: RuleCtx): Patch = {
19+
ctx.tree.collect {
20+
case streamAppend(t: Name) =>
21+
ctx.replaceTree(t, "lazyAppendedAll")
22+
}.asPatch
23+
}
24+
25+
def replaceSymbols(ctx: RuleCtx): Patch = {
26+
ctx.replaceSymbols(
27+
"scala.Stream" -> "scala.LazyList",
28+
"scala.collection.immutable.Stream" -> "scala.collection.immutable.LazyList"
29+
)
30+
}
31+
32+
override def fix(ctx: RuleCtx): Patch = {
33+
replaceStreamAppend(ctx) + replaceSymbols(ctx)
34+
}
35+
}

0 commit comments

Comments
 (0)