Skip to content

Commit

Permalink
Apply scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Dec 6, 2023
1 parent ab66170 commit 713ddf8
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 81 deletions.
140 changes: 104 additions & 36 deletions cli/src/org/scalajs/cli/Scalajsld.scala
Original file line number Diff line number Diff line change
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 @@ -121,27 +137,44 @@ object Scalajsld {
// "By default, undefined behaviors are in Fatal mode for fastLinkJS and in Unchecked mode for fullLinkJS"
// taken from: https://www.scala-js.org/doc/semantics.html#undefined-behaviors
opt[Unit]('f', "fastOpt")
.action { (_, c) => c.copy(noOpt = false, fullOpt = false, semantics = Semantics.Defaults) }
.action { (_, c) =>
c.copy(noOpt = false, fullOpt = false, semantics = Semantics.Defaults)
}
.text("Optimize code (this is the default)")
opt[Unit]('n', "noOpt")
.action { (_, c) => c.copy(noOpt = true, fullOpt = false) }
.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.")
"A typical use case is changing the file extension, e.g. `%.mjs` for Node.js modules."
)
// "By default, undefined behaviors are in Fatal mode for fastLinkJS and in Unchecked mode for fullLinkJS"
// taken from: https://www.scala-js.org/doc/semantics.html#undefined-behaviors
opt[Unit]('u', "fullOpt")
.action { (_, c) => c.copy(noOpt = false, fullOpt = true, semantics = Semantics.Defaults.optimized) }
.action { (_, c) =>
c.copy(
noOpt = false,
fullOpt = true,
semantics = Semantics.Defaults.optimized
)
}
.text("Fully optimize code (uses Google Closure Compiler)")
opt[Unit]('p', "prettyPrint")
.action { (_, c) => c.copy(prettyPrint = true) }
Expand All @@ -150,15 +183,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 @@ -169,19 +208,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 @@ -201,8 +246,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 @@ -221,7 +268,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 @@ -249,10 +299,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 @@ -261,22 +325,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
)
}
Loading

0 comments on commit 713ddf8

Please sign in to comment.