Skip to content

Commit

Permalink
add --triggered support to scalafixOnCompile
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukeoe committed Oct 10, 2020
1 parent 8284c35 commit ed730be
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ object ScalafixPlugin extends AutoPlugin {
"but can be disabled with `scalafixCaching := false`."
)

val scalafixArgsOnCompile: TaskKey[Seq[String]] =
taskKey[Seq[String]](
"Pass extra arguments to scalafix only when scalafix is triggred by compilation. " +
"Only --triggered argument is passed by default."
)

val scalafixCaching: SettingKey[Boolean] =
settingKey[Boolean](
"Cache scalafix invocations (off by default, on if scalafixOnCompile := true)."
Expand Down Expand Up @@ -107,7 +113,7 @@ object ScalafixPlugin extends AutoPlugin {
compile.value // evaluated first, before the potential scalafix evaluation
if (scalafixOnCompile.value)
scalafix
.toTask("")
.toTask(scalafixArgsOnCompile.value.mkString(" ", " ", ""))
.map(_ => oldCompile)
else Def.task(oldCompile)
}.value,
Expand Down Expand Up @@ -184,6 +190,7 @@ object ScalafixPlugin extends AutoPlugin {
override lazy val globalSettings: Seq[Def.Setting[_]] = Seq(
scalafixConfig := None, // let scalafix-cli try to infer $CWD/.scalafix.conf
scalafixOnCompile := false,
scalafixArgsOnCompile := Seq("--triggered"),
scalafixResolvers :=
// Repository.defaults() defaults to Repository.ivy2Local() and Repository.central(). These can be overridden per
// env variable, e.g., export COURSIER_REPOSITORIES="ivy2Local|central|sonatype:releases|jitpack|https://corporate.com/repo".
Expand Down
9 changes: 8 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/.scalafix.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
rules = [DisableSyntax, RemoveUnused]
DisableSyntax.noNulls = true
DisableSyntax.noNulls = true

DisableSyntax.noVars = true
RemoveUnused.privates = false
triggered = {
DisableSyntax.noVars = false
RemoveUnused.privates = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Variables {
private var a = 1
def b: Int = return 2
}
7 changes: 6 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inThisBuild(
scalaVersion := Versions.scala212,
scalacOptions ++= List(
"-Yrangepos",
"-Ywarn-unused-import"
"-Ywarn-unused"
)
)
)
Expand All @@ -21,3 +21,8 @@ lazy val rewrite = project
addCompilerPlugin(scalafixSemanticdb)
)
.settings(scalafixConfigSettings(IntegrationTest): _*)

lazy val args = project
.settings(
addCompilerPlugin(scalafixSemanticdb)
)
13 changes: 12 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/test
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@

# check configuration granularity for scalafixOnCompile
> set scalafixOnCompile.in(lint, Test) := false
> lint/test:compile
> lint/test:compile

# check that triggered rules are respected on compilation, and ignored on explicit invocation.
# `private var` is detected by `DisableSyntax.noVars = true` on explicit invocation,
# and removed by `triggered.RemoveUnused.privates = true` on compilation.
-> args/test:scalafix
> args/test:compile
> args/test:scalafix

# check extra scalafix arguments
> set scalafixArgsOnCompile.in(args, Test) += "--settings DisableSyntax.noReturns = true"
-> args/test:compile

0 comments on commit ed730be

Please sign in to comment.