Skip to content

Commit

Permalink
Pass scalaVersion to Scalafix as needed for Scalafix 0.13.0 (#205)
Browse files Browse the repository at this point in the history
- Run all integration tests
- deprecate `scalafixScalaBinaryVersion` in favor of `scalaVersion`
- deprecate `fixAction` overloads which take scalaBinaryVersion as a parameter

Pull Request: #205

Commits:

* scalaVersion to Scalafix as needed for Scalafix 0.13.0

* Fix fix-2.12 test

* Use Scala 3.5 specific feature in test

Co-authored-by: Brice Jaglin <bjaglin@gmail.com>

* Remove debug print

Co-authored-by: Brice Jaglin <bjaglin@gmail.com>

* Add deprecated overload instead of using `@nowarn`

* Fix custom-rule test

* Fix tests

* Remove debug print

---------

Co-authored-by: Brice Jaglin <bjaglin@gmail.com>
  • Loading branch information
lolgab and bjaglin authored Sep 30, 2024
1 parent 7515573 commit 54b8d07
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
8 changes: 8 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ trait ITestCross extends MillIntegrationTestModule with Cross.Module[String] {
TestInvocation.Targets(Seq("__.fix")),
TestInvocation.Targets(Seq("verify"))
),
PathRef(sources().head.path / "fix-2.12") -> Seq(
TestInvocation.Targets(Seq("__.fix")),
TestInvocation.Targets(Seq("verify"))
),
PathRef(sources().head.path / "fix-3.5") -> Seq(
TestInvocation.Targets(Seq("__.fix")),
TestInvocation.Targets(Seq("verify"))
),
PathRef(sources().head.path / "check") -> Seq(
TestInvocation.Targets(Seq("__.fix", "--check"))
),
Expand Down
4 changes: 2 additions & 2 deletions itest/src/custom-rule/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import mill.scalalib._
import os._

object project extends ScalaModule with ScalafixModule {
def scalaVersion = "2.13.12"
def scalaVersion = "2.12.17"
def semanticDbEnablePluginScalacOptions = super.semanticDbEnablePluginScalacOptions() ++ Seq("-P:semanticdb:synthetics:on")
def scalafixIvyDeps = Agg(ivy"org.scala-lang.modules::scala-collection-migrations:2.11.0")
def scalafixIvyDeps = Agg(ivy"org.scala-lang.modules::scala-collection-migrations:2.12.0")
}

def verify() =
Expand Down
5 changes: 4 additions & 1 deletion itest/src/fix-2.12/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import os._
object project extends ScalaModule with ScalafixModule {
def scalaVersion = "2.12.17"
def scalacOptions = Seq("-Ywarn-unused")
def ivyDeps = Agg(ivy"org.scala-lang.modules::scala-collection-compat:2.12.0")
}

def verify() =
T.command {
val fixedScala = read(pwd / "project" / "src" / "Fix.scala")
val expected = """object Fix {
val expected = """
|
|object Fix {
| def procedure(): Unit = {}
|}
|""".stripMargin
Expand Down
3 changes: 3 additions & 0 deletions itest/src/fix-3.5/.scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rules = [
ExplicitResultTypes
]
21 changes: 21 additions & 0 deletions itest/src/fix-3.5/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import $file.plugins
import com.goyeau.mill.scalafix.ScalafixModule
import mill._
import mill.scalalib._
import os._

object project extends ScalaModule with ScalafixModule {
def scalaVersion = "3.5.1"
}

def verify() =
T.command {
val fixedScala = read(pwd / "project" / "src" / "Fix.scala")
val expected = """object Fix {
| // use a 3.5.x-only feature to fail if a Scala 3 LTS compiler is used
| // https://www.scala-lang.org/blog/2024/08/22/scala-3.5.0-released.html#support-for-binary-integer-literals
| def myComplexMethod: Map[Int, String] = 1.to(0B1010).map(i => i -> i.toString).toMap
|}
|""".stripMargin
assert(fixedScala == expected)
}
5 changes: 5 additions & 0 deletions itest/src/fix-3.5/project/src/Fix.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Fix {
// use a 3.5.x-only feature to fail if a Scala 3 LTS compiler is used
// https://www.scala-lang.org/blog/2024/08/22/scala-3.5.0-released.html#support-for-binary-integer-literals
def myComplexMethod = 1.to(0B1010).map(i => i -> i.toString).toMap
}
38 changes: 32 additions & 6 deletions mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import mill.define.Command

import scalafix.interfaces.Scalafix
import scalafix.interfaces.ScalafixError.*

import scala.compat.java8.OptionConverters.*
import scala.jdk.CollectionConverters.*

trait ScalafixModule extends ScalaModule {
def scalafixConfig: T[Option[os.Path]] = T(None)
def scalafixIvyDeps: T[Agg[Dep]] = Agg.empty[Dep]
def scalafixConfig: T[Option[os.Path]] = T(None)
def scalafixIvyDeps: T[Agg[Dep]] = Agg.empty[Dep]
@deprecated("Scalafix now follows scalaVersion", since = "0.4.2")
def scalafixScalaBinaryVersion: T[String] = "2.12"

/** Run Scalafix.
Expand All @@ -27,7 +29,6 @@ trait ScalafixModule extends ScalaModule {
filesToFix(sources()).map(_.path),
classpath = (compileClasspath() ++ localClasspath() ++ Seq(semanticDbData())).iterator.toSeq.map(_.path),
scalaVersion(),
scalafixScalaBinaryVersion(),
scalacOptions(),
scalafixIvyDeps(),
scalafixConfig(),
Expand All @@ -38,6 +39,7 @@ trait ScalafixModule extends ScalaModule {
}

object ScalafixModule {
@deprecated("Use overload without scalaBinaryVersion and with wd instead", since = "0.4.2")
def fixAction(
log: Logger,
repositories: Seq[Repository],
Expand All @@ -55,7 +57,6 @@ object ScalafixModule {
sources,
classpath,
scalaVersion,
scalaBinaryVersion,
scalacOptions,
scalafixIvyDeps,
scalafixConfig,
Expand All @@ -69,7 +70,6 @@ object ScalafixModule {
sources: Seq[os.Path],
classpath: Seq[os.Path],
scalaVersion: String,
scalaBinaryVersion: String,
scalacOptions: Seq[String],
scalafixIvyDeps: Agg[Dep],
scalafixConfig: Option[os.Path],
Expand All @@ -78,7 +78,7 @@ object ScalafixModule {
): Result[Unit] =
if (sources.nonEmpty) {
val scalafix = Scalafix
.fetchAndClassloadInstance(scalaBinaryVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
.newArguments()
.withParsedArguments(args.asJava)
.withWorkingDirectory(wd.toNIO)
Expand Down Expand Up @@ -124,4 +124,30 @@ object ScalafixModule {
if (os.isDir(pathRef.path)) os.walk(pathRef.path).filter(file => os.isFile(file) && (file.ext == "scala"))
else Seq(pathRef.path)
} yield PathRef(file)

@deprecated("Use overload without scalaBinaryVersion instead", since = "0.4.2")
def fixAction(
log: Logger,
repositories: Seq[Repository],
sources: Seq[os.Path],
classpath: Seq[os.Path],
scalaVersion: String,
scalaBinaryVersion: String,
scalacOptions: Seq[String],
scalafixIvyDeps: Agg[Dep],
scalafixConfig: Option[os.Path],
args: Seq[String],
wd: os.Path
): Result[Unit] = fixAction(
log,
repositories,
sources,
classpath,
scalaVersion,
scalacOptions,
scalafixIvyDeps,
scalafixConfig,
args,
wd
)
}

0 comments on commit 54b8d07

Please sign in to comment.