From 7c4580b75c86e50da910bdecae82fb47d53b9f53 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Fri, 19 Jan 2018 20:11:30 +0100 Subject: [PATCH] Add copyToBuffer rule --- .../fix/Collectionstrawman_v0_copyToBuffer.scala | 13 +++++++++++++ .../fix/Collectionstrawman_v0_copyToBuffer.scala | 10 ++++++++++ .../src/main/scala/fix/Collectionstrawman_v0.scala | 13 ++++++++++++- .../test/scala/fix/Collectionstrawman_Tests.scala | 1 - 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala create mode 100644 scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala diff --git a/scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala b/scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala new file mode 100644 index 0000000000..a75c4fbeb7 --- /dev/null +++ b/scalafix/2.13/input/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala @@ -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) + +} diff --git a/scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala b/scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala new file mode 100644 index 0000000000..8615a47af8 --- /dev/null +++ b/scalafix/2.13/output/src/main/scala/fix/Collectionstrawman_v0_copyToBuffer.scala @@ -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 + +} diff --git a/scalafix/2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala b/scalafix/2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala index 17204f058c..191c6beade 100644 --- a/scalafix/2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala +++ b/scalafix/2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala @@ -86,9 +86,20 @@ case class Collectionstrawman_v0(index: SemanticdbIndex) 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)" => + ctx.replaceTree(t, q"$buffer ++= $collection".syntax) + }.asPatch + override def fix(ctx: RuleCtx): Patch = { replaceToList(ctx) + replaceSymbols(ctx) + - replaceTupleZipped(ctx) + replaceTupleZipped(ctx) + + replaceCopyToBuffer(ctx) } } diff --git a/scalafix/2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala b/scalafix/2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala index 023382b63b..83e8edb824 100644 --- a/scalafix/2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala +++ b/scalafix/2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala @@ -11,7 +11,6 @@ class Collectionstrawman_Tests Seq(AbsolutePath(BuildInfo.outputSourceroot)) ) { override def assertNoDiff(a: String, b: String, c: String) = { - println(a) super.assertNoDiff(a, b, c) } runAllTests()