Skip to content
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

Fix Scalafmt #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 2 additions & 38 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,38 +1,2 @@
align=none
newlines.alwaysBeforeTopLevelStatements = true
rewrite.rules = [
AvoidInfix,
RedundantParens,
SortModifiers,
PreferCurlyFors,
SortImports
]
rewrite.neverInfix.excludeFilters = [
until
to
by
eq
ne
"should.*"
"contain.*"
"must.*"
in
ignore
be
taggedAs
thrownBy
synchronized
have
when
size
only
noneOf
oneElementOf
noElementsOf
atLeastOneElementOf
atMostOneElementOf
allElementsOf
inOrderElementsOf
theSameElementsAs
length
]
version = "3.5.1"
runner.dialect = "scala213"
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@
<groupId>org.antipathy</groupId>
<artifactId>mvn-scalafmt_${scala.major.version}</artifactId>
<version>${mvn-scalafmt.version}</version>
<configuration>
<validateOnly>true</validateOnly>
<configLocation>.scalafmt.conf</configLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
Comment on lines +217 to +223
Copy link
Owner

@evis evis Apr 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues here:

  1. can't simply run mvn scalafmt:format in order to format code locally;
  2. can't compile unformatted files.

I suggest to remove validateOnly and don't run scalafmt on format goal. Instead, we should check formatting on CI using github action, similar to https://github.com/evis/scalafix-maven-plugin/blob/master/.github/workflows/scalafix.yml.

This way, the issues will be solved, and formatting will still be checked on CI.

</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.evis</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import scalafix.interfaces.{

import scala.collection.JavaConverters._

/**
* It has the same methods as [[ScalafixArguments]], but:
/** It has the same methods as [[ScalafixArguments]], but:
*
* 1. withXXX methods, which take lists, add to current list. For example:
* 1. withXXX methods, which take lists, add to current list. For example:
*
* {{{
* scalafix
* .newArguments()
* .withScalacOptions(singletonList("1"))
Expand All @@ -30,11 +30,13 @@ import scala.collection.JavaConverters._
* .withScalacOptions(List("1"))
* .withScalacOptions(List("2"))
* .build(scalafix) // contains both 1 and 2
* }}}
*
* Why: because several mojo params may add elements to the same argument field,
* e.g., [[CompiledDirectoryParam]] and [[ProjectDependenciesParam]]
* Why: because several mojo params may add elements to the same argument
* field, e.g., [[CompiledDirectoryParam]] and [[ProjectDependenciesParam]]
*
* 2. All withXXX methods take Scala Lists/Options instead of Java for convenience.
* 2. All withXXX methods take Scala Lists/Options instead of Java for
* convenience.
*/
final case class ScalafixArgumentsBuilder(
rules: List[String] = Nil,
Expand Down Expand Up @@ -80,14 +82,16 @@ final case class ScalafixArgumentsBuilder(
copy(rules = this.rules ::: rules)

def withToolClasspath(
toolClasspath: URLClassLoader): ScalafixArgumentsBuilder =
toolClasspath: URLClassLoader
): ScalafixArgumentsBuilder =
copy(toolClasspath = Some(toolClasspath))

def withPaths(paths: List[Path]): ScalafixArgumentsBuilder =
copy(paths = this.paths ::: paths)

def withExcludedPaths(
excludedPaths: List[PathMatcher]): ScalafixArgumentsBuilder =
excludedPaths: List[PathMatcher]
): ScalafixArgumentsBuilder =
copy(excludedPaths = this.excludedPaths ::: excludedPaths)

def withConfig(config: Path): ScalafixArgumentsBuilder =
Expand All @@ -97,7 +101,8 @@ final case class ScalafixArgumentsBuilder(
copy(mode = Some(mode))

def withParsedArguments(
parsedArguments: List[String]): ScalafixArgumentsBuilder =
parsedArguments: List[String]
): ScalafixArgumentsBuilder =
copy(parsedArguments = this.parsedArguments ::: parsedArguments)

def withPrintStream(printStream: PrintStream): ScalafixArgumentsBuilder =
Expand All @@ -110,7 +115,8 @@ final case class ScalafixArgumentsBuilder(
copy(sourceroot = Some(sourceroot))

def withMainCallback(
mainCallback: ScalafixMainCallback): ScalafixArgumentsBuilder =
mainCallback: ScalafixMainCallback
): ScalafixArgumentsBuilder =
copy(mainCallback = Some(mainCallback))

def withCharset(charset: Charset): ScalafixArgumentsBuilder =
Expand All @@ -132,11 +138,13 @@ object ScalafixArgumentsBuilder {
extends AnyVal {

def withToolClasspath(
toolClasspath: Option[URLClassLoader]): ScalafixArguments =
toolClasspath: Option[URLClassLoader]
): ScalafixArguments =
withOpt(toolClasspath)(_.withToolClasspath)

def withWorkingDirectory(
workingDirectory: Option[Path]): ScalafixArguments =
workingDirectory: Option[Path]
): ScalafixArguments =
withOpt(workingDirectory)(_.withWorkingDirectory)

def withMode(mode: Option[ScalafixMainMode]): ScalafixArguments =
Expand All @@ -149,7 +157,8 @@ object ScalafixArgumentsBuilder {
withOpt(sourceroot)(_.withSourceroot)

def withMainCallback(
mainCallback: Option[ScalafixMainCallback]): ScalafixArguments =
mainCallback: Option[ScalafixMainCallback]
): ScalafixArguments =
withOpt(mainCallback)(_.withMainCallback)

def withCharset(charset: Option[Charset]): ScalafixArguments =
Expand All @@ -159,7 +168,8 @@ object ScalafixArgumentsBuilder {
withOpt(scalaVersion)(_.withScalaVersion)

private def withOpt[A](value: Option[A])(
f: ScalafixArguments => A => ScalafixArguments): ScalafixArguments =
f: ScalafixArguments => A => ScalafixArguments
): ScalafixArguments =
value.fold(arguments)(f(arguments))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ final class ScalafixMojo extends AbstractMojo {
property = "project",
defaultValue = "${project}",
required = true,
readonly = true)
readonly = true
)
private var project: MavenProject = _

@Parameter(property = "scalafix.mainSourceDirectories")
Expand Down Expand Up @@ -62,7 +63,8 @@ final class ScalafixMojo extends AbstractMojo {
getLog.info("Skip scalafix since skip flag passed")
} else if (skipMain && skipTest) {
getLog.info(
"Skip scalafix since both skip.main and skip.test flags passed")
"Skip scalafix since both skip.main and skip.test flags passed"
)
} else {
val bld = project.getBuild
val mainOutputs = if (skipMain) Nil else List(bld.getOutputDirectory)
Expand All @@ -84,14 +86,17 @@ final class ScalafixMojo extends AbstractMojo {
private def getSourceParam: MojoParam = {
val lookup = new SourceDirectoryLookup(FileOps, project)
val main = checkSources("main", skipMain)(
lookup.getMain(mainSourceDirectories.asScala))
lookup.getMain(mainSourceDirectories.asScala)
)
val test = checkSources("test", skipTest)(
lookup.getTest(testSourceDirectories.asScala))
lookup.getTest(testSourceDirectories.asScala)
)
SourceDirectoryParam(main ++ test)
}

private def checkSources(flagName: String, skip: Boolean)(
getPaths: => List[Path]): List[Path] = {
getPaths: => List[Path]
): List[Path] = {
val paths = if (skip) Nil else getPaths
if (paths.isEmpty) getLog.info(s"Skip scalafix[$flagName]")
else paths.foreach(dir => getLog.info(s"Processing[$flagName]: $dir"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ object PluginsParam {

def apply(plugins: Iterable[Plugin]): MojoParam = {
_.withScalacOptions(
plugins.findScalaPlugin.flatMap(config).toList.flatMap(compilerArgs))
plugins.findScalaPlugin.flatMap(config).toList.flatMap(compilerArgs)
)
}

implicit private class PluginsOps(private val plugins: Iterable[Plugin])
extends AnyVal {

def findScalaPlugin: Option[Plugin] = {
plugins.find { plugin =>
scalaPluginOrgs.contains(plugin.getGroupId) && plugin.getArtifactId == "scala-maven-plugin"
scalaPluginOrgs.contains(
plugin.getGroupId
) && plugin.getArtifactId == "scala-maven-plugin"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ class SourceDirectoryLookup(fileOps: FileOps, project: MavenProject) {
getFiles(
customDirs,
build.getSourceDirectory(),
project.getCompileSourceRoots())
project.getCompileSourceRoots()
)

def getTest(customDirs: Iterable[File]): List[Path] =
getFiles(
customDirs,
build.getTestSourceDirectory(),
project.getTestCompileSourceRoots())
project.getTestCompileSourceRoots()
)

private def getFiles(
customDirs: Iterable[File],
primaryDir: => String,
alternativeDirs: => JList[String]): List[Path] = {
alternativeDirs: => JList[String]
): List[Path] = {
val customDirsOpt = Option(customDirs).filter(_.nonEmpty)
customDirsOpt.fold {
val builder = Set.newBuilder[Path]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ trait ShowErrors {
def showErrors(errors: List[ScalafixError]): Unit = {
if (errors.nonEmpty) {
throw new MojoExecutionException(
"Scalafix invoked with errors. Check logs for details.")
"Scalafix invoked with errors. Check logs for details."
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object LogLevel {

implicit val logLevelOrdering: Ordering[LogLevel] = Ordering.by {
case Debug => 0
case Info => 1
case Warn => 2
case Info => 1
case Warn => 2
case Error => 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package io.github.evis.scalafix.maven.plugin.log
final case class LogMessage(
level: LogLevel,
charSequence: Option[CharSequence],
throwable: Option[Throwable])
throwable: Option[Throwable]
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class TestLog(level: LogLevel) extends Log {
private def add(
level: LogLevel,
s: Option[CharSequence],
e: Option[Throwable]): Unit =
e: Option[Throwable]
): Unit =
log = log.enqueue(LogMessage(level, s, e))

private def isEnabled(level: LogLevel) = level >= this.level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class CommandLineArgsParamSpec extends ParamSpec {
args.add("--verbose")
CommandLineArgsParam(args).applied.parsedArguments shouldBe List(
"--syntactic",
"--verbose")
"--verbose"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ package io.github.evis.scalafix.maven.plugin.params
class CompiledDirectoryParamSpec extends ParamSpec {

"CompiledDirectoryParam" should "add existing classpath" in {
CompiledDirectoryParam("src/main/scala").applied.classpath.loneElement.toString shouldBe "src/main/scala"
CompiledDirectoryParam(
"src/main/scala"
).applied.classpath.loneElement.toString shouldBe "src/main/scala"
}

it should "not add non-existing classpath" in {
CompiledDirectoryParam("src/main/unexisting").applied.classpath shouldBe empty
CompiledDirectoryParam(
"src/main/unexisting"
).applied.classpath shouldBe empty
}

it should "add all classpaths if invoked with multiple" in {
val result =
CompiledDirectoryParam("src/main/scala", "src/test/scala").applied.classpath
CompiledDirectoryParam(
"src/main/scala",
"src/test/scala"
).applied.classpath
.map(_.toString)
result should contain theSameElementsAs List(
"src/main/scala",
"src/test/scala")
"src/test/scala"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ModeParamSpec extends ParamSpec {
}

it should "set AUTO_SUPPRESS_LINTER_ERRORS mode" in {
ModeParam(AUTO_SUPPRESS_LINTER_ERRORS).applied.mode.value shouldBe AUTO_SUPPRESS_LINTER_ERRORS
ModeParam(
AUTO_SUPPRESS_LINTER_ERRORS
).applied.mode.value shouldBe AUTO_SUPPRESS_LINTER_ERRORS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class PluginsParamSpec extends ParamSpec {

"PluginsParam" should "return scalacOptions if they exist" in {
PluginsParam(
List(scalaPlugin("-Xlint" :: "-Ypartial-unification" :: Nil))).applied.scalacOptions shouldBe List(
"-Xlint",
"-Ypartial-unification")
List(scalaPlugin("-Xlint" :: "-Ypartial-unification" :: Nil))
).applied.scalacOptions shouldBe List("-Xlint", "-Ypartial-unification")
}

it should "return no scalacOptions if they are not set" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class RunSpec extends BaseSpec with Run {
List(
SourceDirectoryParam("src/test/resources/project"),
ConfigParam(
new File("src/test/resources/project/.success.scalafix.conf")),
new File("src/test/resources/project/.success.scalafix.conf")
),
ModeParam(CHECK)
)
noException should be thrownBy run(params, log)
Expand Down