Skip to content

Commit

Permalink
Apply scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Dec 7, 2023
1 parent 50a6049 commit f1eb46f
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 78 deletions.
125 changes: 92 additions & 33 deletions cli/src/org/scalajs/cli/Scalajsld.scala
Expand Up @@ -6,7 +6,6 @@
** |/____/ **
\* */


package org.scalajs.cli

import org.scalajs.ir.ScalaJSVersions
Expand Down Expand Up @@ -52,7 +51,10 @@ object Scalajsld {
logLevel: Level = Level.Info
)

private def moduleInitializer(s: String, hasArgs: Boolean): ModuleInitializer = {
private def moduleInitializer(
s: String,
hasArgs: Boolean
): ModuleInitializer = {
val lastDot = s.lastIndexOf('.')
if (lastDot < 0)
throw new IllegalArgumentException(s"$s is not a valid main method")
Expand All @@ -67,21 +69,35 @@ object Scalajsld {
private implicit object ModuleKindRead extends scopt.Read[ModuleKind] {
val arity = 1
val reads = { (s: String) =>
ModuleKind.All.find(_.toString() == s).getOrElse(
throw new IllegalArgumentException(s"$s is not a valid module kind"))
ModuleKind.All
.find(_.toString() == s)
.getOrElse(
throw new IllegalArgumentException(s"$s is not a valid module kind")
)
}
}

private object ModuleSplitStyleRead {
val All = List(ModuleSplitStyle.FewestModules.toString, ModuleSplitStyle.SmallestModules.toString, "SmallModulesFor")
val All = List(
ModuleSplitStyle.FewestModules.toString,
ModuleSplitStyle.SmallestModules.toString,
"SmallModulesFor"
)

def moduleSplitStyleRead(splitStyle: String, modulePackages: Seq[String]): ModuleSplitStyle =
def moduleSplitStyleRead(
splitStyle: String,
modulePackages: Seq[String]
): ModuleSplitStyle =
try {
(new ModuleSplitStyleParser).parse(splitStyle, modulePackages.toArray).underlying
}
catch {
(new ModuleSplitStyleParser)
.parse(splitStyle, modulePackages.toArray)
.underlying
} catch {
case e: NoClassDefFoundError =>
throw new IllegalArgumentException(s"$splitStyle is not a valid module split style", e.getCause)
throw new IllegalArgumentException(
s"$splitStyle is not a valid module split style",
e.getCause
)
}
}

Expand Down Expand Up @@ -128,14 +144,22 @@ object Scalajsld {
.text("Don't optimize code")
opt[String]("moduleSplitStyle")
.action { (x, c) => c.copy(moduleSplitStyle = x) }
.text("Module splitting style " + ModuleSplitStyleRead.All.mkString("(", ", ", ")"))
.text(
"Module splitting style " + ModuleSplitStyleRead.All
.mkString("(", ", ", ")")
)
opt[Seq[String]]("smallModuleForPackages")
.valueName("<package1>,<package2>...")
.action((x, c) => c.copy(smallModuleForPackages = x))
.text("Create as many small modules as possible for the classes in the passed packages and their subpackages.")
.text(
"Create as many small modules as possible for the classes in the passed packages and their subpackages."
)
opt[String]("jsFilePattern")
.action { (x, c) => c.copy(outputPatterns = OutputPatterns.fromJSFile(x)) }
.text("Pattern for JS file names (default: `%s.js`). " +
.action { (x, c) =>
c.copy(outputPatterns = OutputPatterns.fromJSFile(x))
}
.text(
"Pattern for JS file names (default: `%s.js`). " +
"Expects a printf-style pattern with a single placeholder for the module ID. " +
"A typical use case is changing the file extension, e.g. `%.mjs` for Node.js modules."
)
Expand All @@ -154,15 +178,21 @@ object Scalajsld {
.action { (_, c) => c.copy(sourceMap = true) }
.text("Produce a source map for the produced code")
opt[Unit]("compliantAsInstanceOfs")
.action { (_, c) => c.copy(semantics =
c.semantics.withAsInstanceOfs(Compliant))
.action { (_, c) =>
c.copy(semantics = c.semantics.withAsInstanceOfs(Compliant))
}
.text("Use compliant asInstanceOfs")
opt[Unit]("es2015")
.action { (_, c) => c.copy(esFeatures = c.esFeatures.withESVersion(ESVersion.ES2015)) }
.action { (_, c) =>
c.copy(esFeatures = c.esFeatures.withESVersion(ESVersion.ES2015))
}
.text("Use ECMAScript 2015")
opt[String]("esVersion")
.action { (esV, c) => c.copy(esFeatures = c.esFeatures.withESVersion(EsVersionParser.parse(esV))) }
.action { (esV, c) =>
c.copy(esFeatures =
c.esFeatures.withESVersion(EsVersionParser.parse(esV))
)
}
.text("EsVersion " + EsVersionParser.All.mkString("(", ", ", ")"))
opt[ModuleKind]('k', "moduleKind")
.action { (kind, c) => c.copy(moduleKind = kind) }
Expand All @@ -173,19 +203,25 @@ object Scalajsld {
opt[File]('r', "relativizeSourceMap")
.valueName("<path>")
.action { (x, c) => c.copy(relativizeSourceMap = Some(x.toURI)) }
.text("Relativize source map with respect to given path (meaningful with -s)")
.text(
"Relativize source map with respect to given path (meaningful with -s)"
)
opt[Unit]("noStdlib")
.action { (_, c) => c.copy(stdLib = Nil) }
.text("Don't automatically include Scala.js standard library")
opt[String]("stdlib")
.valueName("<scala.js stdlib jar>")
.hidden()
.action { (x, c) => c.copy(stdLib = x.split(File.pathSeparator).map(new File(_)).toSeq) }
.text("Location of Scala.js standard libarary. This is set by the " +
.action { (x, c) =>
c.copy(stdLib = x.split(File.pathSeparator).map(new File(_)).toSeq)
}
.text(
"Location of Scala.js standard libarary. This is set by the " +
"runner script and automatically prepended to the classpath. " +
"Use -n to not include it.")
"Use -n to not include it."
)
opt[String]("jsHeader")
.action { (jsHeader, c) => c.copy(jsHeader = jsHeader)}
.action { (jsHeader, c) => c.copy(jsHeader = jsHeader) }
.text("A header that will be added at the top of generated .js files")
opt[Unit]('d', "debug")
.action { (_, c) => c.copy(logLevel = Level.Debug) }
Expand All @@ -205,8 +241,10 @@ object Scalajsld {
.text("prints this usage text")
checkConfig { c =>
if (c.output.isDefined) {
reportWarning("using a single file as output (--output) is deprecated since Scala.js 1.3.0." +
" Use --outputDir instead.")
reportWarning(
"using a single file as output (--output) is deprecated since Scala.js 1.3.0." +
" Use --outputDir instead."
)
}

if (c.outputDir.isDefined == c.output.isDefined)
Expand All @@ -225,7 +263,10 @@ object Scalajsld {
val semantics =
if (options.fullOpt) options.semantics.optimized
else options.semantics
val moduleSplitStyle = ModuleSplitStyleRead.moduleSplitStyleRead(options.moduleSplitStyle, options.smallModuleForPackages)
val moduleSplitStyle = ModuleSplitStyleRead.moduleSplitStyleRead(
options.moduleSplitStyle,
options.smallModuleForPackages
)

val config = StandardConfig()
.withSemantics(semantics)
Expand Down Expand Up @@ -253,10 +294,24 @@ object Scalajsld {
.flatMap { irFiles =>
(options.output, options.outputDir) match {
case (Some(jsFile), None) =>
(DeprecatedLinkerAPI: DeprecatedLinkerAPI).link(linker, irFiles.toList, moduleInitializers, jsFile, logger)
(DeprecatedLinkerAPI: DeprecatedLinkerAPI).link(
linker,
irFiles.toList,
moduleInitializers,
jsFile,
logger
)
case (None, Some(outputDir)) =>
linker.link(irFiles, moduleInitializers, PathOutputDirectory(outputDir.toPath()), logger)
case _ => throw new AssertionError("Either output or outputDir have to be defined.")
linker.link(
irFiles,
moduleInitializers,
PathOutputDirectory(outputDir.toPath()),
logger
)
case _ =>
throw new AssertionError(
"Either output or outputDir have to be defined."
)
}
}
Await.result(result, Duration.Inf)
Expand All @@ -265,22 +320,26 @@ object Scalajsld {

// Covers deprecated api with not deprecated method. Suppresses warning.
private abstract class DeprecatedLinkerAPI {
def link(linker: Linker,
def link(
linker: Linker,
irFiles: Seq[IRFile],
moduleInitializers: Seq[ModuleInitializer],
linkerOutputFile: File,
logger: Logger): Future[Unit]
logger: Logger
): Future[Unit]
}

private object DeprecatedLinkerAPI extends DeprecatedLinkerAPI {
def apply(): DeprecatedLinkerAPI = this

@deprecated("Deprecate to silence warnings", "never/always")
def link(linker: Linker,
def link(
linker: Linker,
irFiles: Seq[IRFile],
moduleInitializers: Seq[ModuleInitializer],
linkerOutputFile: File,
logger: Logger): Future[Unit] = {
logger: Logger
): Future[Unit] = {
val js = linkerOutputFile.toPath()
val sm = js.resolveSibling(js.getFileName().toString() + ".map")

Expand Down
47 changes: 28 additions & 19 deletions cli/src/org/scalajs/cli/Scalajsp.scala
Expand Up @@ -6,7 +6,6 @@
** |/____/ **
\* */


package org.scalajs.cli

import org.scalajs.ir.ScalaJSVersions
Expand Down Expand Up @@ -49,7 +48,9 @@ object Scalajsp {
.action { (x, c) => c.copy(jar = Some(x)) }
.text("Read *.sjsir file(s) from the given JAR.")
opt[Unit]('s', "supported")
.action { (_,_) => printSupported(); exit(0) }
.action { (_, _) =>
printSupported(); exit(0)
}
.text("Show supported Scala.js IR versions")
version("version")
.abbr("v")
Expand All @@ -65,11 +66,13 @@ object Scalajsp {
options <- parser.parse(args, Options())
fileName <- options.fileNames
} {
val vfile = options.jar.map { jar =>
readFromJar(jar, fileName)
}.getOrElse {
readFromFile(fileName)
}
val vfile = options.jar
.map { jar =>
readFromJar(jar, fileName)
}
.getOrElse {
readFromFile(fileName)
}

displayFileContent(Await.result(vfile, Duration.Inf), options)
}
Expand Down Expand Up @@ -116,19 +119,25 @@ object Scalajsp {
*/

def findRequestedClass(sjsirFiles: Seq[IRFile]): Future[IRFile] = {
Future.traverse(sjsirFiles) { irFile =>
val ir = IRFileImpl.fromIRFile(irFile)
ir.entryPointsInfo.map { i =>
if (i.className.nameString == name) Success(Some(ir))
else Success(None)
}.recover { case t => Failure(t) }
}.map { irs =>
irs.collectFirst {
case Success(Some(f)) => f
}.getOrElse {
fail(s"No such class in jar: $name")
Future
.traverse(sjsirFiles) { irFile =>
val ir = IRFileImpl.fromIRFile(irFile)
ir.entryPointsInfo
.map { i =>
if (i.className.nameString == name) Success(Some(ir))
else Success(None)
}
.recover { case t => Failure(t) }
}
.map { irs =>
irs
.collectFirst { case Success(Some(f)) =>
f
}
.getOrElse {
fail(s"No such class in jar: $name")
}
}
}
}

val cache = StandardImpl.irFileCache().newCache
Expand Down
34 changes: 23 additions & 11 deletions cli/src/org/scalajs/cli/internal/EsVersionParser.scala
Expand Up @@ -7,17 +7,29 @@ import java.util.Locale
object EsVersionParser {
def parse(esVersion: String): ESVersion =
esVersion.trim.toLowerCase(Locale.ROOT) match {
case "es5_1" => ESVersion.ES5_1
case "es2015" => ESVersion.ES2015
case "es2016" => ESVersion.ES2016
case "es2017" => ESVersion.ES2017
case "es2018" => ESVersion.ES2018
case "es2019" => ESVersion.ES2019
case "es2020" => ESVersion.ES2020
case "es2021" => ESVersion.ES2021
case unknown => throw new IllegalArgumentException(s"Warning: unrecognized argument: $unknown for --esVersion parameter")
}
case "es5_1" => ESVersion.ES5_1
case "es2015" => ESVersion.ES2015
case "es2016" => ESVersion.ES2016
case "es2017" => ESVersion.ES2017
case "es2018" => ESVersion.ES2018
case "es2019" => ESVersion.ES2019
case "es2020" => ESVersion.ES2020
case "es2021" => ESVersion.ES2021
case unknown =>
throw new IllegalArgumentException(
s"Warning: unrecognized argument: $unknown for --esVersion parameter"
)
}

val All: List[ESVersion] =
List(ESVersion.ES5_1, ESVersion.ES2015, ESVersion.ES2016, ESVersion.ES2017, ESVersion.ES2018, ESVersion.ES2019, ESVersion.ES2020, ESVersion.ES2021)
List(
ESVersion.ES5_1,
ESVersion.ES2015,
ESVersion.ES2016,
ESVersion.ES2017,
ESVersion.ES2018,
ESVersion.ES2019,
ESVersion.ES2020,
ESVersion.ES2021
)
}
17 changes: 13 additions & 4 deletions cli/src/org/scalajs/cli/internal/ModuleSplitStyleParser.scala
Expand Up @@ -4,15 +4,24 @@ import org.scalajs.linker.interface.{ModuleSplitStyle => ActualModuleSplitStyle}

// class rather than object, as that's easier to substitute from native-image
class ModuleSplitStyleParser {
def parse(splitStyle: String, modulePackages: Array[String]): ModuleSplitStyle =
def parse(
splitStyle: String,
modulePackages: Array[String]
): ModuleSplitStyle =
if (splitStyle == ActualModuleSplitStyle.FewestModules.toString)
ModuleSplitStyle(ActualModuleSplitStyle.FewestModules)
else if (splitStyle == ActualModuleSplitStyle.SmallestModules.toString)
ModuleSplitStyle(ActualModuleSplitStyle.SmallestModules)
else if (splitStyle == ActualModuleSplitStyle.SmallModulesFor.toString) {
if (modulePackages.isEmpty)
throw new IllegalArgumentException(s"SmallModuleFor style must have at least one package. To define it pass `--smallModuleForPackages` parameter.")
ModuleSplitStyle(ActualModuleSplitStyle.SmallModulesFor(modulePackages.toList))
throw new IllegalArgumentException(
s"SmallModuleFor style must have at least one package. To define it pass `--smallModuleForPackages` parameter."
)
ModuleSplitStyle(
ActualModuleSplitStyle.SmallModulesFor(modulePackages.toList)
)
} else
throw new IllegalArgumentException(s"$splitStyle is not a valid module split style")
throw new IllegalArgumentException(
s"$splitStyle is not a valid module split style"
)
}

0 comments on commit f1eb46f

Please sign in to comment.