The Scalafix rule for forbidding specified packages, classes, methods, and enums to be used in the codebase.
In fact, it works with any symbol that is not a literal.
This was originally built as a part of PlayFast, an opinionated production-ready PlayFramework template that makes you productive. It is used for forbidding java.time.Instant in favor of framework.Instant (our own Instant proxy implementation) because java.time.Instant is tricky to mock in PlayFramework's test due the multi-threaded nature of it.
Please consider starring the repository if you use it. Thank you!
- Add the Scalafix ForbiddenSymbol jar to your
build.sbt:
ThisBuild / scalafixDependencies += "io.github.tanin47" %% "scalafix-forbidden-symbol" % "1.0.0"
- Add the rule to your
.scalafix.conf:
rules = [
ForbiddenSymbol
]
- Specify the forbidden classes as shown below:
ForbiddenSymbol.symbols = [
"java.time", // Package
"java.time.Instant", // Class
"java.time.Instant.now", // Method
"java.time.temporal.ChronoUnit.YEARS" // Enum
]
-
It would be good to test it out by using a forbidden class and run
sbt scalafixAll. You should see an error. -
If you would like to disable this Scalafix rule on a certain line in your codebase, you can use
// scalafix:off ForbiddenSymbol.
Run sbt ~tests/test
- Publish locally with:
sbt publishLocal. - Test that the local version works. We can test with https://github.com/tanin47/playfast
- Build the publishable JAR by running
sbt clean publishSigned - Upload to Maven Central (Sonatype) by running
sbt sonaUpload
