Skip to content

Commit

Permalink
UsingParamAnonymousConstructor
Browse files Browse the repository at this point in the history
(cherry picked from commit 04ab3ac)
  • Loading branch information
xuwei-k committed Jan 3, 2025
1 parent a7af0dc commit bcf990c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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
}
}
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
}
}
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
}
}

0 comments on commit bcf990c

Please sign in to comment.