Skip to content

Commit

Permalink
Add support for the 3 last versions of scala for each binary version …
Browse files Browse the repository at this point in the history
…for ExplicitResultType rule

We were failing if the scalaversion is not exactly the same than the one used to compile
explicitResultType rule. Now it can work with the 3 last versions, and we have tests to verify that it works as expected
  • Loading branch information
mlachkar committed Jun 30, 2020
1 parent 83961dd commit 26303fe
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
- uses: actions/checkout@v2
- uses: olafurpg/setup-scala@v7
- run: sbt ${{ matrix.command }}
ci-explicit-result-types-test:
name: ci-explicit-result-types-rule tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: olafurpg/setup-scala@v7
- run: sbt ci-explicit-result-types-rule
jdk11_212:
name: JDK11/scala_2.12 tests
runs-on: ubuntu-latest
Expand Down
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ lazy val unit = project
"outputSourceDirectories",
sourceDirectories.in(testsOutput, Compile).value
)
props.put("scalaVersion", scalaVersion.value)
props.put("scalacOptions", scalacOptions.value.mkString("|"))
props.put("scalaVersion", scalaVersion.in(testsInput, Compile).value)
props.put(
"scalacOptions",
scalacOptions.in(testsInput, Compile).value.mkString("|")
)
val out =
managedResourceDirectories.in(Test).value.head /
"scalafix-testkit.properties"
Expand Down
15 changes: 15 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sbt._

import scala.util.{Properties, Try}
/* scalafmt: { maxColumn = 120 }*/

object Dependencies {
Expand All @@ -11,6 +13,10 @@ object Dependencies {
def coursierV = "2.0.0-RC5-6"
def coursierInterfaceV = "0.0.22"
val currentScalaVersion = scala213
// we support 3 last binary versions of each scala Version, except for 2.11 because semanticdb-scalac_2.11.10 is missing.
val supportedScalaVersionforExplicitResultRule =
List(scala213, scala212).map(version => version -> supportedScalaVersions(version)).toMap ++
Map(scala211 -> List(scala211))

val jgit = "org.eclipse.jgit" % "org.eclipse.jgit" % "5.7.0.202003110725-r"

Expand All @@ -36,4 +42,13 @@ object Dependencies {
"com.chuusai" %% "shapeless" % "2.3.3",
scalacheck
)

private def supportedScalaVersions(scalaVersion: String): List[String] = {
val split = scalaVersion.split('.')
val binaryVersion = split.take(2).mkString(".")
val compilerVersion = Try(split.last.toInt).toOption
val supportedVersions =
compilerVersion.map(version => List.range(version - 2, version + 1).filter(_ >= 0)).getOrElse(Nil)
supportedVersions.map(v => s"$binaryVersion.$v")
}
}
16 changes: 16 additions & 0 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
"scalameta" -> scalametaV,
scalaVersion,
"supportedScalaVersions" -> supportedScalaVersions,
"supportedScalaVersionsForExplicitResultTypeRule" -> supportedScalaVersionforExplicitResultRule,
"scala211" -> scala211,
"scala212" -> scala212,
"scala213" -> scala213,
Expand Down Expand Up @@ -212,6 +213,21 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
s"unit/testOnly -- -l scalafix.internal.tests.utils.SkipWindows" ::
s
},
commands += Command.command("ci-explicit-result-types-rule") { s =>
supportedScalaVersionforExplicitResultRule
.flatMap {
case (k, versions) =>
versions.flatMap { v =>
List(
s"""set ThisBuild/scalaVersion := "$k"""",
s"""set testsInput/scalaVersion := "$v"""",
s"""set testsOutput/scalaVersion := "$v\"""",
s"unit/testOnly scalafix.tests.rule.RuleSuite -- -z explicitResult"
)
}
}
.foldRight(s)(_ :: _)
},
commands += Command.command("mima") { s =>
// Disabled until v0.6.0 stable
// "scalafix/mimaReportBinaryIssues" ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import scalafix.patch.Patch
import scalafix.v1._
import scalafix.util.TokenOps
import metaconfig.Configured

import scala.meta.internal.pc.ScalafixGlobal
import scalafix.internal.v1.LazyValue

import scala.util.control.NonFatal
import scala.util.Properties
import scala.util.{Properties, Try}
import scalafix.internal.compat.CompilerCompat._

final class ExplicitResultTypes(
Expand All @@ -19,10 +21,20 @@ final class ExplicitResultTypes(

def this() = this(ExplicitResultTypesConfig.default, LazyValue.now(None))

private def supportedScalaVersion = Properties.versionNumberString
private def supportedScalaVersions: List[String] = {
val scalaVersion = Properties.versionNumberString
val split = scalaVersion.split('.')
val binaryVersion = split.take(2).mkString(".")
val compilerVersion = Try(split.last.toInt).toOption
val supportedVersions = compilerVersion
.map(version => List.range(version - 2, version + 1).filter(_ >= 0))
.getOrElse(Nil)
supportedVersions.map(v => s"$binaryVersion.$v")
}

override def description: String =
"Inserts type annotations for inferred public members. " +
s"Only compatible with Scala $supportedScalaVersion."
s"Only compatible with Scala $supportedScalaVersions."
override def isRewrite: Boolean = true

override def afterComplete(): Unit = {
Expand Down Expand Up @@ -57,11 +69,21 @@ final class ExplicitResultTypes(
)
}
}
if (config.scalacClasspath.nonEmpty && config.scalaVersion != supportedScalaVersion) {
if (config.scalacClasspath.nonEmpty && !supportedScalaVersions.contains(
config.scalaVersion
)) {
val shortScalaVersion =
config.scalaVersion.split(".").take(2).mkString(".")
val shortRuleScalaVersion =
Properties.versionNumberString.split(".").take(2).mkString(".")
Configured.error(
s"The ExplicitResultTypes rule only supports the Scala version '$supportedScalaVersion'. " +
s"To fix this problem, either remove `ExplicitResultTypes` from .scalafix.conf or change the Scala version " +
s"in your build to match exactly '$supportedScalaVersion'."
s"Your scalaVersion is ${config.scalaVersion} but the ExplicitResultTypes rule is compiled with ${shortRuleScalaVersion}" +
s"and only supports these Scala versions '$supportedScalaVersions'. " +
(if (shortScalaVersion == shortRuleScalaVersion)
s"To fix this problem, either remove `ExplicitResultTypes` from .scalafix.conf or change the Scala version to be within '$supportedScalaVersions'"
else
s"To fix this problem, either remove `ExplicitResultTypes` from .scalafix.conf or " +
s"specify a `scalafixScalaBinaryVersion` key in your build.sbt equal to $shortScalaVersion")
)
} else {
config.conf // Support deprecated explicitReturnTypes config
Expand Down

0 comments on commit 26303fe

Please sign in to comment.