From 85dc898ad6776d0cd5c51ba7e1f347a63b773e4b Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Sun, 10 May 2020 02:10:37 +0200 Subject: [PATCH] take into account config file BEFORE checking if rules are syntactic RemoveUnused was called without what's required for a semantic rule, as this default rule from the custom config was not picked up early enough, causing the choice to take the syntactic-only code path to be wrong. --- src/main/scala/scalafix/sbt/ScalafixPlugin.scala | 12 +++++------- .../sbt-scalafix/custom-config/.scalafixxx.conf | 3 +++ src/sbt-test/sbt-scalafix/custom-config/build.sbt | 6 ++++++ .../sbt-scalafix/custom-config/project/plugins.sbt | 2 ++ .../src/main/scala/example/Example.scala | 8 ++++++++ src/sbt-test/sbt-scalafix/custom-config/test | 1 + 6 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/sbt-test/sbt-scalafix/custom-config/.scalafixxx.conf create mode 100644 src/sbt-test/sbt-scalafix/custom-config/build.sbt create mode 100644 src/sbt-test/sbt-scalafix/custom-config/project/plugins.sbt create mode 100644 src/sbt-test/sbt-scalafix/custom-config/src/main/scala/example/Example.scala create mode 100644 src/sbt-test/sbt-scalafix/custom-config/test diff --git a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index d0b940d6..3b956fa8 100644 --- a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -143,12 +143,14 @@ object ScalafixPlugin extends AutoPlugin { if (shell0.rules.isEmpty && shell0.extra == List("--help")) { scalafixHelp } else { + val scalafixConf = scalafixConfig.value.map(_.toPath) val (shell, mainArgs0) = scalafixArgsFromShell( shell0, scalafixDependencies.in(ThisBuild).value, scalafixResolvers.in(ThisBuild).value ) val mainArgs = mainArgs0 + .withConfig(jutil.Optional.ofNullable(scalafixConf.orNull)) .withRules(shell.rules.asJava) .safeWithArgs(shell.extra) val rulesThatWillRun = mainArgs.safeRulesThatWillRun() @@ -175,8 +177,7 @@ object ScalafixPlugin extends AutoPlugin { runArgs( filesToFix(shellArgs, config).value, mainArgs, - streams.value.log, - scalafixConfig.value + streams.value.log ) } @@ -202,8 +203,7 @@ object ScalafixPlugin extends AutoPlugin { runArgs( files, args, - streams.value.log, - scalafixConfig.value + streams.value.log ) } } else { @@ -223,11 +223,9 @@ object ScalafixPlugin extends AutoPlugin { private def runArgs( paths: Seq[Path], mainArgs: ScalafixArguments, - logger: Logger, - config: Option[File] + logger: Logger ): Unit = { val finalArgs = mainArgs - .withConfig(jutil.Optional.ofNullable(config.map(_.toPath).orNull)) .withPaths(paths.asJava) if (paths.nonEmpty) { diff --git a/src/sbt-test/sbt-scalafix/custom-config/.scalafixxx.conf b/src/sbt-test/sbt-scalafix/custom-config/.scalafixxx.conf new file mode 100644 index 00000000..8025b84e --- /dev/null +++ b/src/sbt-test/sbt-scalafix/custom-config/.scalafixxx.conf @@ -0,0 +1,3 @@ +rules = [ + RemoveUnused +] \ No newline at end of file diff --git a/src/sbt-test/sbt-scalafix/custom-config/build.sbt b/src/sbt-test/sbt-scalafix/custom-config/build.sbt new file mode 100644 index 00000000..a20ace61 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/custom-config/build.sbt @@ -0,0 +1,6 @@ +val V = _root_.scalafix.sbt.BuildInfo + +scalaVersion := V.scala212 +addCompilerPlugin(scalafixSemanticdb) +scalacOptions ++= Seq("-Yrangepos", "-Ywarn-unused") +scalafixConfig := Some(file(".scalafixxx.conf")) diff --git a/src/sbt-test/sbt-scalafix/custom-config/project/plugins.sbt b/src/sbt-test/sbt-scalafix/custom-config/project/plugins.sbt new file mode 100644 index 00000000..2d3b4d3b --- /dev/null +++ b/src/sbt-test/sbt-scalafix/custom-config/project/plugins.sbt @@ -0,0 +1,2 @@ +resolvers += Resolver.sonatypeRepo("public") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version")) diff --git a/src/sbt-test/sbt-scalafix/custom-config/src/main/scala/example/Example.scala b/src/sbt-test/sbt-scalafix/custom-config/src/main/scala/example/Example.scala new file mode 100644 index 00000000..7534485f --- /dev/null +++ b/src/sbt-test/sbt-scalafix/custom-config/src/main/scala/example/Example.scala @@ -0,0 +1,8 @@ +package example + +import java.util.Map +import scala.concurrent.Future + +object Example { + implicit val str = null.asInstanceOf[Map.Entry[Int, String]] +} diff --git a/src/sbt-test/sbt-scalafix/custom-config/test b/src/sbt-test/sbt-scalafix/custom-config/test new file mode 100644 index 00000000..13e22248 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/custom-config/test @@ -0,0 +1 @@ +> scalafix \ No newline at end of file