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

support Scala 3 style wildcard import in RemoveUnused #1664

Merged
merged 1 commit into from
Sep 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ class RemoveUnused(config: RemoveUnusedConfig)
// unused renamed may still impact compilation by shadowing an identifier.
// See https://github.com/scalacenter/scalafix/issues/614
Patch.replaceTree(to, "_").atomic
case i if isUnusedImport(importPosition(i)) =>
case i
if isUnusedImport
.exists(unused =>
unused.start <= importPosition(i).start && importPosition(
i
).end <= unused.end
) =>
Patch.removeImportee(i).atomic
}.asPatch
case i: Defn if isUnusedTerm(i.pos) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,73 @@ class ScalafixArgumentsSuite extends AnyFunSuite with DiffAssertions {

assert(obtainedError == ScalafixFileEvaluationError.ParseError)
}

test("Scala 3 style wildcard import", SkipWindows) {
// https://github.com/scalacenter/scalafix/issues/1663

if (scala.util.Properties.versionNumberString.startsWith("2.11")) {
cancel()
}

val cwd = StringFS
.string2dir(
"""|/src/Main.scala
|import scala.collection.mutable
|import scala.util._
|import scala.io.*
|
|object Main
""".stripMargin,
charset
)
.toNIO
val d = cwd.resolve("out")
val target = cwd.resolve("target")
val src = cwd.resolve("src")
Files.createDirectories(d)
val main = src.resolve("Main.scala")

val scalacOptions = Array[String](
"-Xsource:3",
"-Yrangepos",
removeUnused,
s"-Xplugin:${SemanticdbPlugin.semanticdbPluginPath()}",
"-Xplugin-require:semanticdb",
"-classpath",
s"${scalaLibrary.mkString(":")}",
s"-P:semanticdb:sourceroot:$src",
s"-P:semanticdb:targetroot:$target",
"-d",
d.toString,
main.toString
)

val _ = scala.tools.nsc.Main.process(scalacOptions)
val result = api
.withRules(
Collections.singletonList(removeUnsuedRule().name.toString())
)
.withClasspath((scalaLibrary.map(_.toNIO) :+ target).asJava)
.withScalacOptions(Collections.singletonList(removeUnused))
.withScalaVersion("2.13.8")
.withPaths(Seq(main).asJava)
.withSourceroot(src)
.evaluate()

val error = result.getError
assert(!error.isPresent)
assert(result.isSuccessful)
assert(result.getFileEvaluations.length == 1)
val fileEvaluation = result.getFileEvaluations.head
assert(fileEvaluation.isSuccessful)
val expected =
"""|
|object Main
|""".stripMargin
val obtained = fileEvaluation.previewPatches.get()
assertNoDiff(obtained, expected)
}

test("withScalaVersion: non-parsable scala version") {
val run = Try(api.withScalaVersion("213"))
assert(run.isFailure)
Expand Down