Skip to content

Commit

Permalink
refactor: Decouple scala version checks from exact version list (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnnei authored Feb 27, 2024
1 parent 25a5f32 commit 3bf81ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sksamuel.scapegoat.inspections.unneccesary

import com.sksamuel.scapegoat.{isScala21312, Inspection, InspectionContext, Inspector, Levels}
import com.sksamuel.scapegoat.{isScala21312OrLater, Inspection, InspectionContext, Inspector, Levels}

/**
* @author
Expand All @@ -15,7 +15,7 @@ class UnnecessaryConversion
"Calling e.g. toString on a String or toList on a List is completely unnecessary and it's an equivalent to identity."
) {

override def isEnabled: Boolean = !isScala21312
override def isEnabled: Boolean = !isScala21312OrLater

def inspector(ctx: InspectionContext): Inspector = new Inspector(ctx) {
override def postTyperTraverser: context.Traverser = new context.Traverser {
Expand Down
18 changes: 14 additions & 4 deletions src/main/scala/com/sksamuel/scapegoat/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.sksamuel

import scala.util.Try

package object scapegoat {
val scalaVersion: String = util.Properties.versionNumberString
private val shortScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".")
private val scalaVersion: String = util.Properties.versionNumberString
private val (major, minor, patch) = extractComponents(scalaVersion)

val isScala213: Boolean = major == 2 && minor == 13
val isScala21312OrLater: Boolean = isScala213 && patch >= 12

val isScala213: Boolean = shortScalaVersion == "2.13"
val isScala21312: Boolean = scalaVersion == "2.13.12"
private[scapegoat] def extractComponents(version: String) = {
def parseInt(s: String) = Try(s.toInt).getOrElse(0)
version.split('.').toList.map(parseInt) match {
case List(major, minor, patch) => (major, minor, patch)
case _ => (1, 0, 0)
}
}
}
14 changes: 14 additions & 0 deletions src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sksamuel.scapegoat

import com.sksamuel.scapegoat._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

class ScapegoatTest extends AnyFreeSpec with Matchers {

"ScalaVersionPattern" - {
"shoud extract components" in {
extractComponents("2.13.12") shouldBe (2, 13, 12)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sksamuel.scapegoat.inspections.unnecessary

import com.sksamuel.scapegoat.inspections.unneccesary.UnnecessaryConversion
import com.sksamuel.scapegoat.{isScala21312, Inspection, InspectionTest}
import com.sksamuel.scapegoat.{isScala21312OrLater, Inspection, InspectionTest}

/** @author Stephen Samuel */
class UnnecessaryConversionTest extends InspectionTest {
Expand All @@ -17,7 +17,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -28,7 +28,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -39,7 +39,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 2
val expectedWarnings = if (isScala21312OrLater) 0 else 2
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -50,7 +50,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -61,7 +61,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -72,7 +72,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -83,7 +83,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

Expand All @@ -94,7 +94,7 @@ class UnnecessaryConversionTest extends InspectionTest {
|}""".stripMargin

compileCodeSnippet(code)
val expectedWarnings = if (isScala21312) 0 else 1
val expectedWarnings = if (isScala21312OrLater) 0 else 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}
}
Expand Down

0 comments on commit 3bf81ea

Please sign in to comment.