-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit 04ab3ac)
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
input/src/main/scala-3/fix/UsingParamAnonymousConstructorTest.scala
This file contains 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,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 | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
output/src/main/scala-3/fix/UsingParamAnonymousConstructorTest.scala
This file contains 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,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
41
rules/src/main/scala/fix/UsingParamAnonymousConstructor.scala
This file contains 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,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 | ||
} | ||
} |