Skip to content

Add in scalafmt and scalafix. #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ on:
pull_request:

jobs:
scala:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
java: [ '1.8', '1.11' ]
java: [ '8', '11' ]
scala: [
{ version: '2.12.13' },
{ version: '2.12.12' },
Expand All @@ -37,10 +37,29 @@ jobs:
with:
fetch-depth: 0

- name: Set up Scala env
uses: olafurpg/setup-scala@v10
- name: Set up JVM
uses: actions/setup-java@v2
with:
java-version: adopt@${{ matrix.java }}
distribution: 'adopt'
java-version: ${{ matrix.java }}

- name: run tests
run: sbt ++${{ matrix.scala.version }} test

style-check:
runs-on: ubuntu-latest

steps:
- name: checkout the repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up JVM
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '8'

- name: styleCheck
run: sbt styleCheck
12 changes: 12 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rules = [
OrganizeImports
]

OrganizeImports.groupedImports = Explode
OrganizeImports.expandRelative = true
OrganizeImports.removeUnused = true
OrganizeImports.groups = [
"re:javax?\\."
"scala."
"*"
]
4 changes: 4 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = "2.7.5"
project.git = true
assumeStandardLibraryStripMargin = true
xmlLiterals.assumeFormatted = true
41 changes: 34 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import sbtcrossproject.CrossProject
import sbtcrossproject.CrossType

val scalatestVersion = "3.2.8"

val defaultScala213 = "2.13.5"
val bin212 = Seq("2.12.13", "2.12.12", "2.12.11", "2.12.10", "2.12.9", "2.12.8")
val bin213 = Seq("2.13.5", "2.13.4", "2.13.3", "2.13.2", "2.13.1", "2.13.0")
val bin213 =
Seq(defaultScala213, "2.13.4", "2.13.3", "2.13.2", "2.13.1", "2.13.0")

inThisBuild(
List(
Expand All @@ -27,23 +28,37 @@ inThisBuild(
licenses := Seq(
"Apache-2.0" -> url("http://www.apache.org/license/LICENSE-2.0")
),
scalaVersion := bin213.head,
crossScalaVersions := bin212 ++ bin213,
scalaVersion := defaultScala213,
versionScheme := Some("early-semver"),
Test / fork := false,
Test / publishArtifact := false,
Test / parallelExecution := false,
Global / concurrentRestrictions += Tags.limit(Tags.Test, 1),
scalacOptions := Seq(
"-unchecked",
"-deprecation",
"-feature",
"-encoding",
"utf8"
),
Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)
scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.5.0",
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision,
scalafixScalaBinaryVersion := scalaBinaryVersion.value
)
)

lazy val sharedSettings = List(
scalacOptions := {
if (scalaVersion.value == defaultScala213) {
scalacOptions.value :+ "-Wunused:imports"
} else {
scalacOptions.value
}
},
crossScalaVersions := bin212 ++ bin213
)

lazy val root = Project("scalac-scoverage", file("."))
.settings(
name := "scalac-scoverage",
Expand All @@ -63,7 +78,8 @@ lazy val runtime = CrossProject(
crossTarget := target.value / s"scala-${scalaVersion.value}",
libraryDependencies ++= Seq(
"org.scalatest" %%% "scalatest" % scalatestVersion % Test
)
),
sharedSettings
)
.jvmSettings(
Test / fork := true
Expand All @@ -86,8 +102,19 @@ lazy val plugin =
"org.scala-lang.modules" %% "scala-xml" % "1.3.0",
"org.scalatest" %% "scalatest" % scalatestVersion % Test,
"org.scala-lang" % "scala-compiler" % scalaVersion.value % Provided
)
),
sharedSettings
)
.settings(
(Test / unmanagedSourceDirectories) += (Test / sourceDirectory).value / "scala-2.12+"
)

addCommandAlias(
"styleFix",
"scalafixAll ; scalafmtAll ; scalafmtSbt"
)

addCommandAlias(
"styleCheck",
"scalafmtCheckAll ; scalafmtSbtCheck ; scalafix --check"
)
4 changes: 4 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.1")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2")

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.27")
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package scoverage

import scala.collection.mutable
import scala.reflect.internal.util.{Position, SourceFile}
import scala.reflect.internal.util.Position
import scala.reflect.internal.util.SourceFile
import scala.util.matching.Regex

/**
* Methods related to filtering the instrumentation and coverage.
*
* @author Stephen Samuel
*/
/** Methods related to filtering the instrumentation and coverage.
*
* @author Stephen Samuel
*/
trait CoverageFilter {
def isClassIncluded(className: String): Boolean
def isFileIncluded(file: SourceFile): Boolean
Expand All @@ -25,40 +25,44 @@ object AllCoverageFilter extends CoverageFilter {
override def isSymbolIncluded(symbolName: String): Boolean = true
}

class RegexCoverageFilter(excludedPackages: Seq[String],
excludedFiles: Seq[String],
excludedSymbols: Seq[String]) extends CoverageFilter {
class RegexCoverageFilter(
excludedPackages: Seq[String],
excludedFiles: Seq[String],
excludedSymbols: Seq[String]
) extends CoverageFilter {

val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
val excludedFilePatterns = excludedFiles.map(_.r.pattern)
val excludedSymbolPatterns = excludedSymbols.map(_.r.pattern)

/**
* We cache the excluded ranges to avoid scanning the source code files
* repeatedly. For a large project there might be a lot of source code
* data, so we only hold a weak reference.
*/
val linesExcludedByScoverageCommentsCache: mutable.Map[SourceFile, List[Range]] = mutable.WeakHashMap.empty
/** We cache the excluded ranges to avoid scanning the source code files
* repeatedly. For a large project there might be a lot of source code
* data, so we only hold a weak reference.
*/
val linesExcludedByScoverageCommentsCache
: mutable.Map[SourceFile, List[Range]] = mutable.WeakHashMap.empty

final val scoverageExclusionCommentsRegex =
"""(?ms)^\s*//\s*(\$COVERAGE-OFF\$).*?(^\s*//\s*\$COVERAGE-ON\$|\Z)""".r

/**
* True if the given className has not been excluded by the
* `excludedPackages` option.
*/
/** True if the given className has not been excluded by the
* `excludedPackages` option.
*/
override def isClassIncluded(className: String): Boolean = {
excludedClassNamePatterns.isEmpty || !excludedClassNamePatterns.exists(_.matcher(className).matches)
excludedClassNamePatterns.isEmpty || !excludedClassNamePatterns.exists(
_.matcher(className).matches
)
}

override def isFileIncluded(file: SourceFile): Boolean = {
def isFileMatch(file: SourceFile) = excludedFilePatterns.exists(_.matcher(file.path.replace(".scala", "")).matches)
def isFileMatch(file: SourceFile) = excludedFilePatterns.exists(
_.matcher(file.path.replace(".scala", "")).matches
)
excludedFilePatterns.isEmpty || !isFileMatch(file)
}

/**
* True if the line containing `position` has not been excluded by a magic comment.
*/
/** True if the line containing `position` has not been excluded by a magic comment.
*/
def isLineIncluded(position: Position): Boolean = {
if (position.isDefined) {
val excludedLineNumbers = getExcludedLineNumbers(position.source)
Expand All @@ -70,27 +74,34 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
}

override def isSymbolIncluded(symbolName: String): Boolean = {
excludedSymbolPatterns.isEmpty || !excludedSymbolPatterns.exists(_.matcher(symbolName).matches)
excludedSymbolPatterns.isEmpty || !excludedSymbolPatterns.exists(
_.matcher(symbolName).matches
)
}

/**
* Provides overloads to paper over 2.12.13+ SourceFile incompatibility
*/
def compatFindAllIn(regexp: Regex, pattern: Array[Char]): Regex.MatchIterator = regexp.findAllIn(new String(pattern))
def compatFindAllIn(regexp: Regex, pattern: String): Regex.MatchIterator = regexp.findAllIn(pattern)
/** Provides overloads to paper over 2.12.13+ SourceFile incompatibility
*/
def compatFindAllIn(
regexp: Regex,
pattern: Array[Char]
): Regex.MatchIterator = regexp.findAllIn(new String(pattern))
def compatFindAllIn(regexp: Regex, pattern: String): Regex.MatchIterator =
regexp.findAllIn(pattern)

/**
* Checks the given sourceFile for any magic comments which exclude lines
* from coverage. Returns a list of Ranges of lines that should be excluded.
*
* The line numbers returned are conventional 1-based line numbers (i.e. the
* first line is line number 1)
*/
/** Checks the given sourceFile for any magic comments which exclude lines
* from coverage. Returns a list of Ranges of lines that should be excluded.
*
* The line numbers returned are conventional 1-based line numbers (i.e. the
* first line is line number 1)
*/
def getExcludedLineNumbers(sourceFile: SourceFile): List[Range] = {
linesExcludedByScoverageCommentsCache.get(sourceFile) match {
case Some(lineNumbers) => lineNumbers
case None =>
val lineNumbers = compatFindAllIn(scoverageExclusionCommentsRegex, sourceFile.content).matchData.map { m =>
val lineNumbers = compatFindAllIn(
scoverageExclusionCommentsRegex,
sourceFile.content
).matchData.map { m =>
// Asking a SourceFile for the line number of the char after
// the end of the file gives an exception
val endChar = math.min(m.end(2), sourceFile.content.length - 1)
Expand All @@ -100,7 +111,8 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
// line numbers
Range(
1 + sourceFile.offsetToLine(m.start(1)),
1 + sourceFile.offsetToLine(endChar))
1 + sourceFile.offsetToLine(endChar)
)
}.toList
linesExcludedByScoverageCommentsCache.put(sourceFile, lineNumbers)
lineNumbers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scoverage

import java.text.{DecimalFormat, DecimalFormatSymbols}
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale

object DoubleFormat {
Expand Down
Loading