Skip to content

Commit

Permalink
fix SyntacticOrganizeImports
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jan 13, 2024
1 parent 6e489bd commit 63dadcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ import scala.collection.mutable
import scala.collection.mutable.SortedMap
import scala.collection.mutable.SortedSet

class SyntacticOrganizeImports
class SyntacticOrganizeImportsTest1
13 changes: 13 additions & 0 deletions input/src/main/scala/fix/SyntacticOrganizeImportsTest2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
rule = SyntacticOrganizeImports
*/
package fix

import scala.collection.immutable.SortedMap
import scala.collection.immutable.SortedSet
import scala.collection.immutable._
import scala.collection.mutable.SortedMap
import scala.collection.mutable.SortedSet
import scala.collection.mutable.*

class SyntacticOrganizeImportsTest2
18 changes: 17 additions & 1 deletion rules/src/main/scala/fix/SyntacticOrganizeImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ class SyntacticOrganizeImports extends SyntacticRule("SyntacticOrganizeImports")
loop(list, Nil).reverse
}

private def importToString(i: Import): String = {
val s = i.toString
if (i.importers.forall(_.importees.forall(!_.is[Importee.Wildcard]))) {
s
} else {
// for consistency OrganizeImports
// TODO https://github.com/scalacenter/scalafix/pull/1896 ?
val wildcardNewStyle = ".*"
if (s.endsWith(wildcardNewStyle)) {
s"${s.dropRight(wildcardNewStyle.length)}._"
} else {
s
}
}
}

override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case p: Pkg =>
// find top-level imports
Expand All @@ -50,7 +66,7 @@ class SyntacticOrganizeImports extends SyntacticRule("SyntacticOrganizeImports")
)
},
imports
.sortBy(_.toString)
.sortBy(importToString)
.zip(imports)
.find { case (x1, x2) =>
x1.toString != x2.toString
Expand Down

0 comments on commit 63dadcb

Please sign in to comment.