Skip to content

Set up Scalafmt and Scalafix and other small improvements #39

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

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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
with:
java-version: "[email protected]"
- uses: coursier/cache-action@v5
- name: Formatting
run: sbt lint
- name: Unit tests
run: sbt "++${{ matrix.scalaversion }}" "${{ env.PROJECT_NAME }}/test"
- name: Test generate documentation
Expand Down
16 changes: 16 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rules = [
ExplicitResultTypes
LeakingImplicitClassVal
NoAutoTupling
NoValInForComprehension
OrganizeImports
ProcedureSyntax
RemoveUnused
]
OrganizeImports {
# Allign with IntelliJ IDEA so that they don't fight each other
groupedImports = Merge
}
RemoveUnused {
imports = false // handled by OrganizeImports
}
7 changes: 7 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version = "2.7.5"
preset=Scala.js
maxColumn = 100
newlines.alwaysBeforeMultilineDef = false
rewrite.rules = [AvoidInfix, PreferCurlyFors]
docstrings.style = AsteriskSpace
danglingParentheses.ctrlSite = false
137 changes: 85 additions & 52 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,50 +1,77 @@
// shadow sbt-scalajs' crossProject and CrossType until Scala.js 1.0.0 is released
// shadow sbt-scalajs' crossProject until Scala.js 1.0.0 is released
import com.typesafe.tools.mima.core._
import sbtcrossproject.{crossProject, CrossType}
import sbtcrossproject.crossProject

val previousVersion = "1.1.1"

inThisBuild(Def.settings(
crossScalaVersions := Seq("2.12.13", "2.11.12", "2.13.4"),
scalaVersion := crossScalaVersions.value.head,
version := "1.1.2-SNAPSHOT",
organization := "org.portable-scala",

scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-encoding",
"utf-8",
"-Xfatal-warnings",
),

homepage := Some(url("https://github.com/portable-scala/portable-scala-reflect")),
licenses += ("BSD New",
url("https://github.com/portable-scala/portable-scala-reflect/blob/master/LICENSE")),
scmInfo := Some(ScmInfo(
url("https://github.com/portable-scala/portable-scala-reflect"),
"scm:git:[email protected]:portable-scala/portable-scala-reflect.git",
Some("scm:git:[email protected]:portable-scala/portable-scala-reflect.git"))),
))
inThisBuild(
Def.settings(
crossScalaVersions := Seq("2.13.4", "2.12.13", "2.11.12"),
scalaVersion := crossScalaVersions.value.head,
version := "1.1.2-SNAPSHOT",
organization := "org.portable-scala",
scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-encoding",
"utf-8",
"-Ywarn-unused"
) ++ {
if (sys.env.contains("CI")) {
List("-Xfatal-warnings")
} else {
List() // to enable Scalafix locally
}
},
semanticdbEnabled := true,
semanticdbOptions += "-P:semanticdb:synthetics:on",
semanticdbVersion := scalafixSemanticdb.revision,
ThisBuild / scalafixScalaBinaryVersion := CrossVersion
.binaryScalaVersion(scalaVersion.value),
ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.5.0",
libraryDependencies ++= List(
("com.github.ghik" % "silencer-lib" % "1.7.3" % Provided).cross(CrossVersion.full),
compilerPlugin(
("com.github.ghik" % "silencer-plugin" % "1.7.3").cross(CrossVersion.full)
)
),
homepage := Some(url("https://github.com/portable-scala/portable-scala-reflect")),
licenses += ("BSD New",
url("https://github.com/portable-scala/portable-scala-reflect/blob/master/LICENSE")),
scmInfo := Some(ScmInfo(url("https://github.com/portable-scala/portable-scala-reflect"),
"scm:git:[email protected]:portable-scala/portable-scala-reflect.git",
Some("scm:git:[email protected]:portable-scala/portable-scala-reflect.git")))
))

lazy val `portable-scala-reflect` = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("."))
.settings(
scalacOptions in (Compile, doc) -= "-Xfatal-warnings",

mimaPreviousArtifacts +=
organization.value %%% moduleName.value % previousVersion,

publishMavenStyle := true,
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
pomExtra := (
<developers>
scalacOptions in (Compile, doc) -= "-Xfatal-warnings",
mimaPreviousArtifacts +=
organization.value %%% moduleName.value % previousVersion,
mimaBinaryIssueFilters ++= List(
/* `MacroCompat` has been removed, it was needed for Scala 2.10 only */
ProblemFilters.exclude[MissingClassProblem](
"org.portablescala.reflect.internal.Macros$MacroCompat$"),
ProblemFilters.exclude[MissingClassProblem](
"org.portablescala.reflect.internal.Macros$MacroCompat$Scope1$"),
ProblemFilters.exclude[MissingClassProblem](
"org.portablescala.reflect.internal.Macros$MacroCompat$Scope2$"),
ProblemFilters.exclude[MissingClassProblem](
"org.portablescala.reflect.internal.Macros$MacroCompat$Scope2$Inner$"),
ProblemFilters.exclude[MissingClassProblem](
"org.portablescala.reflect.internal.Macros$MacroCompat$Scope1$blackbox$")
),
publishMavenStyle := true,
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots".at(nexus + "content/repositories/snapshots"))
else
Some("releases".at(nexus + "service/local/staging/deploy/maven2"))
},
pomExtra := (
<developers>
<developer>
<id>sjrd</id>
<name>Sébastien Doeraene</name>
Expand All @@ -61,22 +88,28 @@ lazy val `portable-scala-reflect` = crossProject(JSPlatform, JVMPlatform, Native
<url>https://github.com/densh/</url>
</developer>
</developers>
),
pomIncludeRepository := { _ => false },
),
pomIncludeRepository := { _ => false }
)
.jvmSettings(
// Macros
libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value % Provided,

// Speed up compilation a bit. Our .java files do not need to see the .scala files.
compileOrder := CompileOrder.JavaThenScala,

libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test",
// Macros
libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value % Provided,
// Speed up compilation a bit. Our .java files do not need to see the .scala files.
compileOrder := CompileOrder.JavaThenScala,
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
.nativeSettings(
libraryDependencies +=
"org.scala-native" %%% "junit-runtime" % "0.4.0" % "test",
addCompilerPlugin(
"org.scala-native" % "junit-plugin" % "0.4.0" cross CrossVersion.full),
libraryDependencies +=
"org.scala-native" %%% "junit-runtime" % "0.4.0" % "test",
addCompilerPlugin(("org.scala-native" % "junit-plugin" % "0.4.0").cross(CrossVersion.full))
)

addCommandAlias(
"fix",
"; all compile:scalafix test:scalafix; all scalafmtSbt scalafmtAll"
)
addCommandAlias(
"lint",
"; scalafmtSbtCheck; scalafmtCheckAll; compile:scalafix --check; test:scalafix --check"
)
11 changes: 7 additions & 4 deletions js/src/main/scala/org/portablescala/reflect/Reflect.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.portablescala.reflect

import com.github.ghik.silencer.silent

import scala.scalajs.reflect.{Reflect => SJSReflect}

object Reflect {

/** Reflectively looks up a loadable module class.
*
* A module class is the technical term referring to the class of a Scala
Expand Down Expand Up @@ -34,10 +37,10 @@ object Reflect {
* Fully-qualified name of the module class, including its trailing `$`
*
* @param loader
* Ignored
* Ignored, used in the JVM variant and we need to keep the API
*/
def lookupLoadableModuleClass(fqcn: String,
loader: ClassLoader): Option[LoadableModuleClass] = {
@silent("never used") loader: ClassLoader): Option[LoadableModuleClass] = {
lookupLoadableModuleClass(fqcn)
}

Expand Down Expand Up @@ -72,10 +75,10 @@ object Reflect {
* Fully-qualified name of the class
*
* @param loader
* Ignored
* Ignored, used in the JVM variant and we need to keep the API
*/
def lookupInstantiatableClass(fqcn: String,
loader: ClassLoader): Option[InstantiatableClass] = {
@silent("never used") loader: ClassLoader): Option[InstantiatableClass] = {
lookupInstantiatableClass(fqcn)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import java.lang.reflect.InvocationTargetException
* The `java.lang.Class[_]` representing the class.
*/
final class InstantiatableClass private[reflect] (val runtimeClass: Class[_]) {

/** A list of the public constructors declared in this class. */
val declaredConstructors: List[InvokableConstructor] =
runtimeClass.getConstructors().map(new InvokableConstructor(_)).toList
runtimeClass.getConstructors.map(new InvokableConstructor(_)).toList

/** Instantiates this class using its zero-argument constructor.
*
Expand All @@ -26,12 +27,6 @@ final class InstantiatableClass private[reflect] (val runtimeClass: Class[_]) {
throw e.getCause
case e: NoSuchMethodException =>
throw new InstantiationException(runtimeClass.getName).initCause(e)
case _: IllegalAccessException =>
/* The constructor exists but is private; make it look like it does not
* exist at all.
*/
throw new InstantiationException(runtimeClass.getName).initCause(
new NoSuchMethodException(runtimeClass.getName + ".<init>()"))
}
}

Expand All @@ -42,8 +37,7 @@ final class InstantiatableClass private[reflect] (val runtimeClass: Class[_]) {
*/
def getConstructor(parameterTypes: Class[_]*): Option[InvokableConstructor] = {
try {
Some(new InvokableConstructor(
runtimeClass.getConstructor(parameterTypes: _*)))
Some(new InvokableConstructor(runtimeClass.getConstructor(parameterTypes: _*)))
} catch {
case _: NoSuchMethodException => None
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.portablescala.reflect

import java.lang.reflect.Constructor
import java.lang.reflect._

/** A description of a constructor that can reflectively invoked. */
final class InvokableConstructor private[reflect] (ctor: Constructor[_]) {

/** The `Class[_]` objects representing the formal parameters of this
* constructor.
*/
val parameterTypes: List[Class[_]] = ctor.getParameterTypes().toList
val parameterTypes: List[Class[_]] = ctor.getParameterTypes.toList

/** Invokes this constructor to instantiate a new object.
*
Expand All @@ -22,12 +23,8 @@ final class InvokableConstructor private[reflect] (ctor: Constructor[_]) {
try {
ctor.newInstance(args.asInstanceOf[Seq[AnyRef]]: _*)
} catch {
case e: java.lang.reflect.InvocationTargetException =>
val cause = e.getCause
if (cause == null)
throw e
else
throw cause
case e: InvocationTargetException if e.getCause != null =>
throw e.getCause
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.portablescala.reflect
* The `java.lang.Class[_]` representing the module class.
*/
final class LoadableModuleClass private[reflect] (val runtimeClass: Class[_]) {

/** Loads the module instance and returns it.
*
* If the underlying constructor throws an exception `e`, then `loadModule`
Expand All @@ -16,12 +17,8 @@ final class LoadableModuleClass private[reflect] (val runtimeClass: Class[_]) {
try {
runtimeClass.getField("MODULE$").get(null)
} catch {
case e: java.lang.ExceptionInInitializerError =>
val cause = e.getCause
if (cause == null)
throw e
else
throw cause
case e: ExceptionInInitializerError if e.getCause != null =>
throw e.getCause
}
}
}
22 changes: 10 additions & 12 deletions jvm/src/main/scala/org/portablescala/reflect/Reflect.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.portablescala.reflect

import scala.language.experimental.macros
import scala.collection.mutable
import java.lang.reflect._
import org.portablescala.reflect.annotation._
import org.portablescala.reflect.internal.Macros

import java.lang.reflect._
import scala.collection.mutable
import scala.language.experimental.macros

object Reflect {

/** Reflectively looks up a loadable module class using the current class
Expand Down Expand Up @@ -52,8 +53,7 @@ object Reflect {
* @param loader
* Class loader to use to load the module class
*/
def lookupLoadableModuleClass(fqcn: String,
loader: ClassLoader): Option[LoadableModuleClass] = {
def lookupLoadableModuleClass(fqcn: String, loader: ClassLoader): Option[LoadableModuleClass] = {
load(fqcn, loader).filter(isModuleClass).map(new LoadableModuleClass(_))
}

Expand Down Expand Up @@ -101,8 +101,7 @@ object Reflect {
* @param loader
* Class loader to use to load the class
*/
def lookupInstantiatableClass(fqcn: String,
loader: ClassLoader): Option[InstantiatableClass] = {
def lookupInstantiatableClass(fqcn: String, loader: ClassLoader): Option[InstantiatableClass] = {
load(fqcn, loader).filter(isInstantiatableClass).map(new InstantiatableClass(_))
}

Expand All @@ -121,10 +120,10 @@ object Reflect {
* inner class (non-local), both are the same non-null class.
*/
def isLocalClass: Boolean =
clazz.getEnclosingClass() != clazz.getDeclaringClass()
clazz.getEnclosingClass != clazz.getDeclaringClass

(clazz.getModifiers() & Modifier.ABSTRACT) == 0 &&
clazz.getConstructors().length > 0 &&
(clazz.getModifiers & Modifier.ABSTRACT) == 0 &&
clazz.getConstructors.length > 0 &&
!isModuleClass(clazz) &&
!isLocalClass
}
Expand All @@ -136,8 +135,7 @@ object Reflect {
* `loadModule`.
*/
val clazz = Class.forName(fqcn, false, loader)
if (inheritsAnnotation(clazz)) Some(clazz)
else None
Some(clazz).filter(inheritsAnnotation)
} catch {
case _: ClassNotFoundException => None
}
Expand Down
Loading