Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport 0.4.x #243

Merged
merged 10 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions input/src/main/scala-3/fix/ForTupleMatchTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
rule = ForTupleMatch
*/
package fix

trait ForTupleMatchTest {
def x1: List[(Int, String)]
def x2: List[(Int, String, Boolean)]

def f0: List[(String, Int)] = for {
a <- x2
(b, c, _) = a
} yield (c, b)

def f1: List[(String, Int)] = for {
a <- x1
(b, c) = a
} yield (c, b)

def f2: List[((Int, String), Int)] = for {
a <- x1
(b, c) = a
} yield (a, b)

def f3: Unit = for {
a <- x1
(b, c) = a
} {
println(b)
}

def f4: Unit = for {
a <- x1
(b, c) = a
} {
println(a._2)
}
}
18 changes: 18 additions & 0 deletions input/src/main/scala-3/fix/MatchParenthesesTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
rule = MatchParentheses
*/
package fix

trait MatchParenthesesTest {
def f1(x: List[Int]): Int = (x match {
case Nil => ","
case _ => x.mkString(",")
}).length

def g(s: String): String

def f2(x: List[Int]): Int = g(x match {
case Nil => ","
case _ => x.mkString(",")
}).length
}
14 changes: 14 additions & 0 deletions input/src/main/scala-3/fix/SameParamOverloadingTest2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rule = SameParamOverloading
*/
package fix

import scala.reflect.ClassTag

object SameParamOverloadingTest2 {

abstract class A1 {
def x1[A](a: A)(using ClassTag[A]): Int // assert: SameParamOverloading
def x1[A](b: A): String // assert: SameParamOverloading
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
rule = UsingParamAnonymousConstructor
*/
package fix

trait UsingParamAnonymousConstructorTest {
class A1(using x1: Int)

class A2(using x1: Int) {
def f: Int = x1
}

trait B1(using x1: Int)

trait B2(using x1: Int) {
def f: Int = x1
}
}
9 changes: 9 additions & 0 deletions input/src/main/scala-3/fix/UsingParamAnonymousTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
rule = UsingParamAnonymous
*/
package fix

trait UsingParamAnonymousTest {
def f1(using a1: Int, a2: Int): Int = List(a1, 2).sum
def f2(using a1: Int, a2: String): Int = summon[Int] + summon[String].size
}
9 changes: 9 additions & 0 deletions input/src/main/scala-3/fix/WithLeftWithRightTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
rule = WithLeftWithRight
*/
package fix

trait WithLeftWithRightTest {
def x1: Either[String, Int] = Right[String, Int](2)
def x2: Either[String, Int] = Left[String, Int]("a")
}
1 change: 1 addition & 0 deletions input/src/main/scala/fix/CollectHeadOptionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package fix

object CollectHeadOptionTest {
def x1: Option[String] = List(1, 2, 3).collect { case n if n % 2 == 0 => n.toString }.headOption
def x2(f: PartialFunction[Int, String]): Option[String] = List(1, 2, 3).collect(f).headOption
}
35 changes: 35 additions & 0 deletions output/src/main/scala-3/fix/ForTupleMatchTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fix

trait ForTupleMatchTest {
def x1: List[(Int, String)]
def x2: List[(Int, String, Boolean)]

def f0: List[(String, Int)] = for {
(b, c, _) <- x2

} yield (c, b)

def f1: List[(String, Int)] = for {
(b, c) <- x1

} yield (c, b)

def f2: List[((Int, String), Int)] = for {
a <- x1
(b, c) = a
} yield (a, b)

def f3: Unit = for {
(b, c) <- x1

} {
println(b)
}

def f4: Unit = for {
a <- x1
(b, c) = a
} {
println(a._2)
}
}
15 changes: 15 additions & 0 deletions output/src/main/scala-3/fix/MatchParenthesesTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fix

trait MatchParenthesesTest {
def f1(x: List[Int]): Int = x .match {
case Nil => ","
case _ => x.mkString(",")
}.length

def g(s: String): String

def f2(x: List[Int]): Int = g(x match {
case Nil => ","
case _ => x.mkString(",")
}).length
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fix

trait UsingParamAnonymousConstructorTest {
class A1(using Int)

class A2(using x1: Int) {
def f: Int = x1
}

trait B1(using Int)

trait B2(using x1: Int) {
def f: Int = x1
}
}
6 changes: 6 additions & 0 deletions output/src/main/scala-3/fix/UsingParamAnonymousTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fix

trait UsingParamAnonymousTest {
def f1(using a1: Int, a2: Int): Int = List(a1, 2).sum
def f2(using Int, String): Int = summon[Int] + summon[String].size
}
6 changes: 6 additions & 0 deletions output/src/main/scala-3/fix/WithLeftWithRightTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fix

trait WithLeftWithRightTest {
def x1: Either[String, Int] = Right(2).withLeft[String]
def x2: Either[String, Int] = Left("a").withRight[Int]
}
1 change: 1 addition & 0 deletions output/src/main/scala/fix/CollectHeadOptionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package fix

object CollectHeadOptionTest {
def x1: Option[String] = List(1, 2, 3).collectFirst{ case n if n % 2 == 0 => n.toString }
def x2(f: PartialFunction[Int, String]): Option[String] = List(1, 2, 3).collectFirst(f)
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.4
sbt.version=1.10.7
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1")

addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.10.0")
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.10.1")

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.12.2")

addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.1")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")

Expand Down
7 changes: 6 additions & 1 deletion rules/src/main/scala/fix/CollectHeadOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class CollectHeadOption extends SyntacticRule("CollectHeadOption") {
),
Term.Name("headOption")
) =>
Patch.replaceTree(t, s"${obj}.collectFirst${func}")
func match {
case _: Term.PartialFunction =>
Patch.replaceTree(t, s"${obj}.collectFirst${func}")
case _ =>
Patch.replaceTree(t, s"${obj}.collectFirst(${func})")
}
}
}.asPatch
}
38 changes: 38 additions & 0 deletions rules/src/main/scala/fix/ForTupleMatch.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fix

import scala.meta.Enumerator
import scala.meta.Pat
import scala.meta.Term
import scala.meta.XtensionCollectionLikeUI
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch

class ForTupleMatch extends SyntacticRule("ForTupleMatch") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case t: Term.For =>
t
case t: Term.ForYield =>
t
}.map { t =>
t.enums
.zip(t.enums.drop(1))
.collect {
case (
Enumerator.Generator(Pat.Var(tuple1: Term.Name), _),
tupleVal @ Enumerator.Val(
extracted: Pat.Tuple,
Term.Name(tuple2)
)
) if tuple1.value == tuple2 && (t.collect { case Term.Name(x) if x == tuple1.value => () }.size == 2) =>
Seq(
Patch.replaceTree(tuple1, extracted.toString),
Patch.removeTokens(tupleVal.tokens)
).asPatch
}
.asPatch
}.asPatch
}
}
34 changes: 34 additions & 0 deletions rules/src/main/scala/fix/MatchParentheses.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fix

import scala.meta.Term
import scala.meta.XtensionClassifiable
import scala.meta.XtensionCollectionLikeUI
import scala.meta.tokens.Token
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionOptionPatch
import scalafix.v1.XtensionSeqPatch

class MatchParentheses extends SyntacticRule("MatchParentheses") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case t: Term.Match =>
PartialFunction
.condOpt(doc.tokens.filter(_.pos.start >= t.pos.end).take(2)) { case Seq(r: Token.RightParen, _: Token.Dot) =>
PartialFunction
.condOpt(doc.tokens.filter(_.pos.end <= t.pos.start).takeRight(2)) {
case Seq(n, l: Token.LeftParen) if !n.is[Token.Ident] =>
Seq(
Patch.removeToken(l),
t.tokens.collectFirst { case m: Token.KwMatch =>
Patch.addLeft(m, ".")
}.asPatch,
Patch.removeToken(r)
).asPatch
}
.asPatch
}
.asPatch
}.asPatch
}
}
42 changes: 42 additions & 0 deletions rules/src/main/scala/fix/UsingParamAnonymous.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fix

import scala.meta.Defn
import scala.meta.Mod
import scala.meta.Term
import scala.meta.XtensionClassifiable
import scala.meta.XtensionCollectionLikeUI
import scala.meta.tokens.Token
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch

class UsingParamAnonymous extends SyntacticRule("UsingParamAnonymous") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case t: Defn.Def =>
t.paramClauses.filter { paramClause =>
paramClause.values.forall(
_.mods.exists(_.is[Mod.Using])
) && paramClause.values.forall(
!_.mods.exists(_.is[Mod.Inline])
)
}.map { paramClause =>
val countOneNames = t.collect { case x: Term.Name =>
x.value
}.groupBy(identity).filter(_._2.size == 1).keySet
if (paramClause.values.forall(a => countOneNames(a.name.value))) {
paramClause.values
.map(p =>
Seq(
Patch.removeTokens(p.name.tokens),
Patch.removeToken(p.tokens.find(_.is[Token.Colon]).get)
).asPatch
)
.asPatch
} else {
Patch.empty
}
}.asPatch
}.asPatch
}
}
41 changes: 41 additions & 0 deletions rules/src/main/scala/fix/UsingParamAnonymousConstructor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fix

import scala.meta.Mod
import scala.meta.Stat
import scala.meta.Term
import scala.meta.XtensionClassifiable
import scala.meta.XtensionCollectionLikeUI
import scala.meta.tokens.Token
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch

class UsingParamAnonymousConstructor extends SyntacticRule("UsingParamAnonymousConstructor") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case c: Stat.WithCtor =>
val t = c.ctor
t.paramClauses.filter { paramClause =>
paramClause.values.forall(_.mods.exists(_.is[Mod.Using])) && paramClause.values.forall(m =>
!m.mods.exists(_.is[Mod.Inline]) && !m.mods.exists(_.is[Mod.Annot])
)
}.map { paramClause =>
val countOneNames = c.collect { case x: Term.Name =>
x.value
}.groupBy(identity).filter(_._2.size == 1).keySet
if (paramClause.values.forall(a => countOneNames(a.name.value))) {
paramClause.values
.map(p =>
Seq(
Patch.removeTokens(p.name.tokens),
Patch.removeToken(p.tokens.find(_.is[Token.Colon]).get)
).asPatch
)
.asPatch
} else {
Patch.empty
}
}.asPatch
}.asPatch
}
}
Loading
Loading