diff --git a/input/src/main/scala/fix/DuplicateWildcardImportTest.scala b/input/src/main/scala/fix/DuplicateWildcardImportTest.scala index 8c5df34..1ffec23 100644 --- a/input/src/main/scala/fix/DuplicateWildcardImportTest.scala +++ b/input/src/main/scala/fix/DuplicateWildcardImportTest.scala @@ -1,3 +1,6 @@ +/* +rule = DuplicateWildcardImport + */ package fix import scala.util._ @@ -5,8 +8,15 @@ 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] +} diff --git a/output/src/main/scala/fix/DuplicateWildcardImportTest.scala b/output/src/main/scala/fix/DuplicateWildcardImportTest.scala index 96788e9..acec023 100644 --- a/output/src/main/scala/fix/DuplicateWildcardImportTest.scala +++ b/output/src/main/scala/fix/DuplicateWildcardImportTest.scala @@ -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] +} diff --git a/rules/src/main/scala/fix/DuplicateWildcardImport.scala b/rules/src/main/scala/fix/DuplicateWildcardImport.scala index 34db125..6178178 100644 --- a/rules/src/main/scala/fix/DuplicateWildcardImport.scala +++ b/rules/src/main/scala/fix/DuplicateWildcardImport.scala @@ -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 @@ -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 } }