This repository was archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Create two distinct scalafix rules targeting 2.12 and 2.13 #352
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_Stream.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| rule = "scala:fix.Collectionstrawman_v0" | ||
| */ | ||
| package fix | ||
|
|
||
| object Collectionstrawman_v0_Stream { | ||
| Stream(1, 2, 3) | ||
| 1 #:: 2 #:: 3 #:: Stream.Empty | ||
| val isEmpty: Stream[_] => Boolean = { | ||
| case Stream.Empty => true | ||
| case x #:: xs => false | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_Traversable.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| rule = "scala:fix.Collectionstrawman_v0" | ||
| */ | ||
| package fix | ||
|
|
||
| object Collectionstrawman_v0_Traversable { | ||
| def foo(xs: Traversable[(Int, String)], ys: List[Int]): Unit = { | ||
| xs.to[List] | ||
| xs.to[Set] | ||
| xs.toIterator | ||
| ys.iterator | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_TupleNZipped.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| rule = "scala:fix.Collectionstrawman_v0" | ||
| */ | ||
| package fix | ||
|
|
||
| import scala.language.postfixOps | ||
| object Collectionstrawman_v0_Tuple2Zipped { | ||
| def zipped(xs: List[Int], ys: List[Int]): Unit = { | ||
| (xs, ys).zipped | ||
| (xs,ys).zipped | ||
| ((xs, ys) zipped) | ||
| (((xs) , (ys)).zipped) | ||
| (xs, // foo | ||
| ys).zipped | ||
| /* a */(/* b */ xs /* c */, /* d */ ys /* e */)/* f */./* g */zipped/* h */ | ||
| (coll(1), coll(2)).zipped | ||
| (List(1, 2, 3), Stream.from(1)).zipped | ||
| } | ||
| def coll(x: Int): List[Int] = ??? | ||
| } | ||
|
|
||
| object Collectionstrawman_v0_Tuple3Zipped { | ||
| def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = { | ||
| (xs, ys, zs).zipped | ||
| (xs,ys,zs).zipped | ||
| ((xs, ys, zs) zipped) | ||
| (((xs) , (ys) , (zs)).zipped) | ||
| (xs, // foo | ||
| ys, // bar | ||
| zs).zipped | ||
| /* a */(/* b */ xs /* c */, /* d */ ys /* e */, /* f */ zs /* g */)/* h */./* i */zipped/* j */ | ||
| (coll(1), coll(2), coll(3)).zipped | ||
| (List(1, 2, 3), Set(1, 2, 3), Stream.from(1)).zipped | ||
| } | ||
| def coll(x: Int): List[Int] = ??? | ||
| } |
13 changes: 13 additions & 0 deletions
13
scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| rule = "scala:fix.Collectionstrawman_v0" | ||
| */ | ||
| package fix | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| class Collectionstrawman_v0_copyToBuffer(xs: List[Int], b: mutable.Buffer[Int]) { | ||
|
|
||
| xs.copyToBuffer(b) | ||
| (xs ++ xs).copyToBuffer(b) | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_Stream.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package fix | ||
|
|
||
| object Collectionstrawman_v0_Stream { | ||
| LazyList(1, 2, 3) | ||
| 1 #:: 2 #:: 3 #:: LazyList.Empty | ||
| val isEmpty: LazyList[_] => Boolean = { | ||
| case LazyList.Empty => true | ||
| case x #:: xs => false | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_Traversable.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package fix | ||
|
|
||
| object Collectionstrawman_v0_Traversable { | ||
| def foo(xs: Iterable[(Int, String)], ys: List[Int]): Unit = { | ||
| xs.to(List) | ||
| xs.to(Set) | ||
| xs.iterator() | ||
| ys.iterator() | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_TupleNZipped.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package fix | ||
|
|
||
| import scala.language.postfixOps | ||
| object Collectionstrawman_v0_Tuple2Zipped { | ||
| def zipped(xs: List[Int], ys: List[Int]): Unit = { | ||
| xs.lazyZip(ys) | ||
| xs.lazyZip(ys) | ||
| (xs.lazyZip(ys) ) | ||
| ((xs).lazyZip((ys))) | ||
| xs.lazyZip(// foo | ||
| ys) | ||
| /* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */)/* f *//* g *//* h */ | ||
| coll(1).lazyZip(coll(2)) | ||
| List(1, 2, 3).lazyZip(LazyList.from(1)) | ||
| } | ||
| def coll(x: Int): List[Int] = ??? | ||
| } | ||
|
|
||
| object Collectionstrawman_v0_Tuple3Zipped { | ||
| def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = { | ||
| xs.lazyZip(ys).lazyZip(zs) | ||
| xs.lazyZip(ys).lazyZip(zs) | ||
| (xs.lazyZip(ys).lazyZip(zs) ) | ||
| ((xs).lazyZip((ys)).lazyZip((zs))) | ||
| xs.lazyZip(// foo | ||
| ys).lazyZip(// bar | ||
| zs) | ||
| /* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */).lazyZip(/* f */ zs /* g */)/* h *//* i *//* j */ | ||
| coll(1).lazyZip(coll(2)).lazyZip(coll(3)) | ||
| List(1, 2, 3).lazyZip(Set(1, 2, 3)).lazyZip(LazyList.from(1)) | ||
| } | ||
| def coll(x: Int): List[Int] = ??? | ||
| } |
10 changes: 10 additions & 0 deletions
10
scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package fix | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| class Collectionstrawman_v0_copyToBuffer(xs: List[Int], b: mutable.Buffer[Int]) { | ||
|
|
||
| b ++= xs | ||
| b ++= xs ++ xs | ||
|
|
||
| } |
105 changes: 105 additions & 0 deletions
105
scalafix/2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package fix | ||
|
|
||
| import scalafix._ | ||
| import scalafix.syntax._ | ||
| import scalafix.util._ | ||
| import scala.meta._ | ||
|
|
||
| case class Collectionstrawman_v0(index: SemanticdbIndex) | ||
| extends SemanticRule(index, "Collectionstrawman_v0") { | ||
|
|
||
| def replaceSymbols(ctx: RuleCtx): Patch = { | ||
| ctx.replaceSymbols( | ||
| "scala.Stream" -> "scala.LazyList", | ||
| "scala.collection.immutable.Stream" -> "scala.collection.immutable.LazyList", | ||
| "scala.Traversable" -> "scala.Iterable", | ||
| "scala.collection.Traversable" -> "scala.collection.Iterable", | ||
| "scala.TraversableOnce" -> "scala.IterableOnce", | ||
| "scala.collection.TraversableOnce" -> "scala.collection.IterableOnce" | ||
| ) | ||
| } | ||
|
|
||
| val toTpe = SymbolMatcher.normalized( | ||
| Symbol("_root_.scala.collection.TraversableLike.to.") | ||
| ) | ||
| val iterator = SymbolMatcher.normalized( | ||
| Symbol("_root_.scala.collection.LinearSeqLike.iterator."), | ||
| Symbol("_root_.scala.collection.TraversableLike.toIterator.") | ||
| ) | ||
| val tupleZipped = SymbolMatcher.normalized( | ||
| Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."), | ||
| Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.") | ||
| ) | ||
|
|
||
| def replaceToList(ctx: RuleCtx) = | ||
| ctx.tree.collect { | ||
| case iterator(t: Name) => | ||
| ctx.replaceTree(t, "iterator()") | ||
| case toTpe(n: Name) => | ||
| (for { | ||
| name <- n.tokens.lastOption | ||
| open <- ctx.tokenList.find(name)(t => t.is[Token.LeftBracket]) | ||
| close <- ctx.matchingParens.close(open.asInstanceOf[Token.LeftBracket]) | ||
| } yield | ||
| ctx.replaceToken(open, "(") + | ||
| ctx.replaceToken(close, ")") | ||
| ).asPatch | ||
| }.asPatch | ||
|
|
||
| def replaceTupleZipped(ctx: RuleCtx) = | ||
| ctx.tree.collect { | ||
| case tupleZipped(Term.Select(Term.Tuple(args), name)) => | ||
| val removeTokensPatch = | ||
| (for { | ||
| zipped <- name.tokens.headOption | ||
| closeTuple <- ctx.tokenList.leading(zipped).find(_.is[Token.RightParen]) | ||
| openTuple <- ctx.matchingParens.open(closeTuple.asInstanceOf[Token.RightParen]) | ||
| maybeDot = ctx.tokenList.slice(closeTuple, zipped).find(_.is[Token.Dot]) | ||
| } yield { | ||
| ctx.removeToken(openTuple) + | ||
| maybeDot.map(ctx.removeToken).asPatch + | ||
| ctx.removeToken(zipped) | ||
| }).asPatch | ||
|
|
||
| def removeSurroundingWhiteSpaces(tk: Token) = | ||
| (ctx.tokenList.trailing(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken) ++ | ||
| ctx.tokenList.leading(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken)).asPatch | ||
|
|
||
| val commas = | ||
| for { | ||
| (prev, next) <- args.zip(args.tail) | ||
| tokensBetweenArgs = ctx.tokenList.slice(prev.tokens.last, next.tokens.head) | ||
| comma <- tokensBetweenArgs.find(_.is[Token.Comma]) | ||
| } yield comma | ||
|
|
||
| val replaceCommasPatch = commas match { | ||
| case head :: tail => | ||
| ctx.replaceToken(head, ".lazyZip(") + | ||
| removeSurroundingWhiteSpaces(head) ++ | ||
| tail.map { comma => | ||
| ctx.replaceToken(comma, ").lazyZip(") + | ||
| removeSurroundingWhiteSpaces(comma) | ||
| } | ||
| case _ => Patch.empty | ||
| } | ||
|
|
||
| removeTokensPatch + replaceCommasPatch | ||
| }.asPatch | ||
|
|
||
| val copyToBuffer = SymbolMatcher.normalized( | ||
| Symbol("_root_.scala.collection.TraversableOnce.copyToBuffer.") | ||
| ) | ||
|
|
||
| def replaceCopyToBuffer(ctx: RuleCtx): Patch = | ||
| ctx.tree.collect { | ||
| case t @ q"${copyToBuffer(Term.Select(collection, _))}($buffer)" => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pro tip, you can put the @ val q"$qual.${q"foo"}(..$arg)" = q"qual.foo(1, 2)"
qual: Term = Term.Name("qual")
arg: List[Term] = List(Lit.Int(1), Lit.Int(2))
People have different tastes however, I personally prefer Term.Select :) |
||
| ctx.replaceTree(t, q"$buffer ++= $collection".syntax) | ||
| }.asPatch | ||
|
|
||
| override def fix(ctx: RuleCtx): Patch = { | ||
| replaceToList(ctx) + | ||
| replaceSymbols(ctx) + | ||
| replaceTupleZipped(ctx) + | ||
| replaceCopyToBuffer(ctx) | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
scalafix/2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package fix | ||
|
|
||
| import scala.meta._ | ||
| import scalafix.testkit._ | ||
| import scalafix._ | ||
|
|
||
| class Collectionstrawman_Tests | ||
| extends SemanticRuleSuite( | ||
| SemanticdbIndex.load(Classpath(AbsolutePath(BuildInfo.inputClassdirectory))), | ||
| AbsolutePath(BuildInfo.inputSourceroot), | ||
| Seq(AbsolutePath(BuildInfo.outputSourceroot)) | ||
| ) { | ||
| override def assertNoDiff(a: String, b: String, c: String) = { | ||
| super.assertNoDiff(a, b, c) | ||
| } | ||
| runAllTests() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, a side-effect of the changes in this file is that the project can be imported without errors in IntelliJ.