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

improve DuplicateWildcardImport #143

Merged
merged 1 commit into from
Jan 16, 2024
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
18 changes: 14 additions & 4 deletions input/src/main/scala/fix/DuplicateWildcardImportTest.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
/*
rule = DuplicateWildcardImport
*/
package fix

import scala.util._
import scala.util.Success
import scala.util.Failure
import scala.util.Try
import scala.util.Random
import scala.collection.immutable.*
import scala.collection.immutable.BitSet
import scala.collection.immutable.IntMap

/*
rule = DuplicateWildcardImport
*/
object DuplicateWildcardImportTest
trait DuplicateWildcardImportTest {
def x1: Success[Int]
def x2: Failure[Int]
def x3: Try[Int]
def x4: Random
def x5: BitSet
def x6: IntMap[String]
}
11 changes: 9 additions & 2 deletions output/src/main/scala/fix/DuplicateWildcardImportTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package fix

import scala.util._
import scala.collection.immutable.*


object DuplicateWildcardImportTest
trait DuplicateWildcardImportTest {
def x1: Success[Int]
def x2: Failure[Int]
def x3: Try[Int]
def x4: Random
def x5: BitSet
def x6: IntMap[String]
}
52 changes: 24 additions & 28 deletions rules/src/main/scala/fix/DuplicateWildcardImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import metaconfig.ConfDecoder
import metaconfig.Configured
import metaconfig.generic.Surface
import scala.meta.Import
import scala.meta.Importee
import scala.meta.Importer
import scala.meta.Pkg
import scala.meta.Source
import scala.meta.Token
Expand Down Expand Up @@ -35,41 +37,35 @@ class DuplicateWildcardImport(conf: DuplicateWildcardImportConfig) extends Synta

override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case t: Source =>
// ignore `import a.b.{x => y}`
val exclude = {
Seq("{", "=>", "}") ++ {
if (conf.isScala3) {
Seq(" as ")
} else {
Nil
}
}
def isRename(s: Importer) = {
s.importees.exists(_.is[Importee.Rename])
}
def isWildcard(s: String): Boolean = {
s.endsWith("._") || (conf.isScala3 && s.endsWith(".*"))
def isWildcard(s: Importer): Boolean = {
s.importees.exists(_.is[Importee.Wildcard])
}

t.stats.collect { case p: Pkg =>
p.stats
}.flatten.collect { case i: Import =>
i
}.groupBy(i => i.toString.split('.').init.mkString("."))
.values
.filter(x => x.size > 1 && x.exists(a => isWildcard(a.toString)))
.flatMap {
_.filterNot { x =>
val s = x.toString
isWildcard(s) || exclude.exists(s contains _)
}.map { x =>
Seq(
Patch.removeTokens(x.tokens),
Patch.removeTokens(
doc.tree.tokens.dropWhile(_.pos.start < x.tokens.last.pos.end).headOption.filter(_.is[Token.LF])
)
).asPatch
}
i -> i.importers
}.collect { case (i, x :: Nil) =>
i -> x
}.groupBy { case (_, x) =>
x.ref.structure
}.values.filter { x =>
x.size > 1 && x.exists(a => isWildcard(a._2))
}.flatMap {
_.filterNot { x =>
isWildcard(x._2) || isRename(x._2)
}.map { x =>
Seq(
Patch.removeTokens(x._1.tokens),
Patch.removeTokens(
doc.tree.tokens.dropWhile(_.pos.start < x._1.tokens.last.pos.end).headOption.filter(_.is[Token.LF])
)
).asPatch
}
.asPatch
}.asPatch
}.asPatch
}
}