Skip to content

Commit 17d3fd7

Browse files
authoredSep 29, 2024
Merge pull request #2087 from bjaglin/scalaversion-moreliberal
scalafix-interfaces: improve docs & be more liberal in parsing
2 parents 74f9fe0 + 733099f commit 17d3fd7

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed
 

‎scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java

+21-15
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ public interface Scalafix {
8686
* <p>
8787
* The custom classloader optionally provided with {@link ScalafixArguments#withToolClasspath} to compile and
8888
* classload external rules must have the classloader of the returned instance as ancestor to share a common
89-
* loaded instance of `scalafix-core`, and therefore have been compiled against the requested Scala binary version.
89+
* loaded instance of `scalafix-core`, and therefore have been compiled against the requested Scala version.
9090
*
91-
* @param requestedScalaVersion The Scala version ("3.3.4" for example) available in the classloader of the
91+
* @param requestedScalaVersion A full Scala version (i.e. "3.3.4") or a major.minor one (i.e. "3.3") to infer
92+
* the major.minor Scala version that should be available in the classloader of the
9293
* returned instance. To be able to run advanced semantic rules using the Scala
93-
* Presentation Compiler such as ExplicitResultTypes, this must match the version
94-
* that the target classpath was built with, as provided with
94+
* Presentation Compiler such as ExplicitResultTypes, this must be source-compatible
95+
* with the version that the target classpath is built with, as provided with
9596
* {@link ScalafixArguments#withScalaVersion}.
9697
* @return An implementation of the {@link Scalafix} interface.
9798
* @throws ScalafixException in case of errors during artifact resolution/fetching.
@@ -106,12 +107,13 @@ static Scalafix fetchAndClassloadInstance(String scalaBinaryVersion) throws Scal
106107
* <p>
107108
* The custom classloader optionally provided with {@link ScalafixArguments#withToolClasspath} to compile and
108109
* classload external rules must have the classloader of the returned instance as ancestor to share a common
109-
* loaded instance of `scalafix-core`, and therefore have been compiled against the requested Scala binary version.
110+
* loaded instance of `scalafix-core`, and therefore have been compiled against the requested Scala version.
110111
*
111-
* @param requestedScalaVersion The Scala version ("3.3.4" for example) available in the classloader of the
112+
* @param requestedScalaVersion A full Scala version (i.e. "3.3.4") or a major.minor one (i.e. "3.3") to infer
113+
* the major.minor Scala version that should be available in the classloader of the
112114
* returned instance. To be able to run advanced semantic rules using the Scala
113-
* Presentation Compiler such as ExplicitResultTypes, this must match the version
114-
* that the target classpath was built with, as provided with
115+
* Presentation Compiler such as ExplicitResultTypes, this must be source-compatible
116+
* with the version that the target classpath is built with, as provided with
115117
* {@link ScalafixArguments#withScalaVersion}.
116118
* @param repositories Maven/Ivy repositories to fetch the JARs from.
117119
* @return An implementation of the {@link Scalafix} interface.
@@ -120,17 +122,21 @@ static Scalafix fetchAndClassloadInstance(String scalaBinaryVersion) throws Scal
120122
static Scalafix fetchAndClassloadInstance(String requestedScalaVersion, List<Repository> repositories)
121123
throws ScalafixException {
122124

125+
String requestedScalaMajorMinorOrMajorVersion =
126+
requestedScalaVersion.replaceAll("^(\\d+\\.\\d+).*", "$1");
127+
123128
String scalaVersionKey;
124-
if (requestedScalaVersion.startsWith("2.12")) {
129+
if (requestedScalaMajorMinorOrMajorVersion.equals("2.12")) {
125130
scalaVersionKey = "scala212";
126-
} else if (requestedScalaVersion.startsWith("2.13")) {
131+
} else if (requestedScalaMajorMinorOrMajorVersion.equals("2.13") ||
132+
requestedScalaMajorMinorOrMajorVersion.equals("2")) {
127133
scalaVersionKey = "scala213";
128-
} else if (requestedScalaVersion.startsWith("3.0") ||
129-
requestedScalaVersion.startsWith("3.1") ||
130-
requestedScalaVersion.startsWith("3.2") ||
131-
requestedScalaVersion.startsWith("3.3")) {
134+
} else if (requestedScalaMajorMinorOrMajorVersion.equals("3.0") ||
135+
requestedScalaMajorMinorOrMajorVersion.equals("3.1") ||
136+
requestedScalaMajorMinorOrMajorVersion.equals("3.2") ||
137+
requestedScalaMajorMinorOrMajorVersion.equals("3.3")) {
132138
scalaVersionKey = "scala3LTS";
133-
} else if (requestedScalaVersion.startsWith("3")) {
139+
} else if (requestedScalaMajorMinorOrMajorVersion.startsWith("3")) {
134140
scalaVersionKey = "scala3Next";
135141
} else {
136142
throw new IllegalArgumentException("Unsupported scala version " + requestedScalaVersion);

‎scalafix-tests/integration/src/test/scala/scalafix/tests/interfaces/ScalafixSuite.scala

+66-2
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,80 @@ class ScalafixSuite extends AnyFunSuite {
5454
assert(help.contains("Usage: scalafix"))
5555
}
5656

57-
test("classload Scala 3 LTS as a fallback for pre-LTS versions") {
57+
test("fail to classload Scala 2.11 with full version") {
58+
assertThrows[IllegalArgumentException](
59+
Scalafix.fetchAndClassloadInstance("2.11.0", repositories)
60+
)
61+
}
62+
63+
test("fail to classload Scala 2.11 with minor version") {
64+
assertThrows[IllegalArgumentException](
65+
Scalafix.fetchAndClassloadInstance("2.11", repositories)
66+
)
67+
}
68+
69+
test("classload Scala 2.12 with full version") {
70+
val scalafixAPI =
71+
Scalafix.fetchAndClassloadInstance("2.12.20", repositories)
72+
assert(scalafixAPI.scalaVersion() == Versions.scala212)
73+
}
74+
75+
test("classload Scala 2.12 with major.minor version") {
76+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("2.12", repositories)
77+
assert(scalafixAPI.scalaVersion() == Versions.scala212)
78+
}
79+
80+
test("classload Scala 2.13 with full version") {
81+
val scalafixAPI =
82+
Scalafix.fetchAndClassloadInstance("2.13.15", repositories)
83+
assert(scalafixAPI.scalaVersion() == Versions.scala213)
84+
}
85+
86+
test("classload Scala 2.13 with major.minor version") {
87+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("2.13", repositories)
88+
assert(scalafixAPI.scalaVersion() == Versions.scala213)
89+
}
90+
91+
test("classload Scala 2.13 with major version") {
92+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("2", repositories)
93+
assert(scalafixAPI.scalaVersion() == Versions.scala213)
94+
}
95+
96+
test("classload Scala 3 LTS with full pre-LTS version") {
5897
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.0.0", repositories)
5998
assert(scalafixAPI.scalaVersion() == Versions.scala3LTS)
6099
}
61100

62-
test("classload Scala 3 Next as a fallback for post-LTS versions") {
101+
test("classload Scala 3 LTS with major.minor pre-LTS version") {
102+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.2", repositories)
103+
assert(scalafixAPI.scalaVersion() == Versions.scala3LTS)
104+
}
105+
106+
test("classload Scala 3 LTS with full LTS version") {
107+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.3.4", repositories)
108+
assert(scalafixAPI.scalaVersion() == Versions.scala3LTS)
109+
}
110+
111+
test("classload Scala 3 LTS with major.minor LTS version") {
112+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.3", repositories)
113+
assert(scalafixAPI.scalaVersion() == Versions.scala3LTS)
114+
}
115+
116+
test("classload Scala 3 Next with full post-LTS version") {
63117
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.4.0", repositories)
64118
assert(scalafixAPI.scalaVersion() == Versions.scala3Next)
65119
}
66120

121+
test("classload Scala 3 Next with major.minor post-LTS version") {
122+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3.5", repositories)
123+
assert(scalafixAPI.scalaVersion() == Versions.scala3Next)
124+
}
125+
126+
test("classload Scala 3 Next with major version") {
127+
val scalafixAPI = Scalafix.fetchAndClassloadInstance("3", repositories)
128+
assert(scalafixAPI.scalaVersion() == Versions.scala3Next)
129+
}
130+
67131
test("invalid class loader") {
68132
val cl = new URLClassLoader(Array(), null)
69133
val ex = intercept[ScalafixException] {

0 commit comments

Comments
 (0)
Please sign in to comment.