From f1eb46f5e7774e5e94c068fd48bd3367d014bb72 Mon Sep 17 00:00:00 2001 From: Maciej Gajek Date: Wed, 6 Dec 2023 15:23:19 +0100 Subject: [PATCH] Apply scalafmt --- cli/src/org/scalajs/cli/Scalajsld.scala | 125 +++++++++++++----- cli/src/org/scalajs/cli/Scalajsp.scala | 47 ++++--- .../cli/internal/EsVersionParser.scala | 34 +++-- .../cli/internal/ModuleSplitStyleParser.scala | 17 ++- .../src/org/scalajs/cli/tests/Tests.scala | 44 ++++-- 5 files changed, 189 insertions(+), 78 deletions(-) diff --git a/cli/src/org/scalajs/cli/Scalajsld.scala b/cli/src/org/scalajs/cli/Scalajsld.scala index e6fe9ee..25b9515 100644 --- a/cli/src/org/scalajs/cli/Scalajsld.scala +++ b/cli/src/org/scalajs/cli/Scalajsld.scala @@ -6,7 +6,6 @@ ** |/____/ ** \* */ - package org.scalajs.cli import org.scalajs.ir.ScalaJSVersions @@ -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") @@ -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 + ) } } @@ -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(",...") .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." ) @@ -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) } @@ -173,19 +203,25 @@ object Scalajsld { opt[File]('r', "relativizeSourceMap") .valueName("") .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("") .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) } @@ -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) @@ -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) @@ -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) @@ -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") diff --git a/cli/src/org/scalajs/cli/Scalajsp.scala b/cli/src/org/scalajs/cli/Scalajsp.scala index 470c8d3..03b1a00 100644 --- a/cli/src/org/scalajs/cli/Scalajsp.scala +++ b/cli/src/org/scalajs/cli/Scalajsp.scala @@ -6,7 +6,6 @@ ** |/____/ ** \* */ - package org.scalajs.cli import org.scalajs.ir.ScalaJSVersions @@ -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") @@ -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) } @@ -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 diff --git a/cli/src/org/scalajs/cli/internal/EsVersionParser.scala b/cli/src/org/scalajs/cli/internal/EsVersionParser.scala index 016afaf..b8f9c95 100644 --- a/cli/src/org/scalajs/cli/internal/EsVersionParser.scala +++ b/cli/src/org/scalajs/cli/internal/EsVersionParser.scala @@ -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 + ) } diff --git a/cli/src/org/scalajs/cli/internal/ModuleSplitStyleParser.scala b/cli/src/org/scalajs/cli/internal/ModuleSplitStyleParser.scala index 20645eb..280d096 100644 --- a/cli/src/org/scalajs/cli/internal/ModuleSplitStyleParser.scala +++ b/cli/src/org/scalajs/cli/internal/ModuleSplitStyleParser.scala @@ -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" + ) } diff --git a/tests/test/src/org/scalajs/cli/tests/Tests.scala b/tests/test/src/org/scalajs/cli/tests/Tests.scala index 959c124..2840a9c 100644 --- a/tests/test/src/org/scalajs/cli/tests/Tests.scala +++ b/tests/test/src/org/scalajs/cli/tests/Tests.scala @@ -26,7 +26,18 @@ class Tests extends munit.FunSuite { |""".stripMargin ) - val scalaJsLibraryCp = os.proc("cs", "fetch", "--classpath", "-E", "org.scala-lang:scala-library", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim() + val scalaJsLibraryCp = os + .proc( + "cs", + "fetch", + "--classpath", + "-E", + "org.scala-lang:scala-library", + s"org.scala-js::scalajs-library:$scalaJsVersion" + ) + .call(cwd = dir) + .out + .trim() os.makeDir.all(dir / "bin") os.proc( @@ -37,16 +48,26 @@ class Tests extends munit.FunSuite { "-classpath", scalaJsLibraryCp, s"-Xplugin:${os.proc("cs", "fetch", "--intransitive", s"org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion").call(cwd = dir).out.trim()}", - "-d", "bin", "foo.scala" + "-d", + "bin", + "foo.scala" ).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit) - val res = os.proc( - launcher, - "--stdlib", - scalaJsLibraryCp, - "-s", "-o", "test.js", "-mm", "Foo.main", "bin" - ).call(cwd = dir, stderr = os.Pipe) - val expectedInOutput = "Warning: using a single file as output (--output) is deprecated since Scala.js 1.3.0. Use --outputDir instead." + val res = os + .proc( + launcher, + "--stdlib", + scalaJsLibraryCp, + "-s", + "-o", + "test.js", + "-mm", + "Foo.main", + "bin" + ) + .call(cwd = dir, stderr = os.Pipe) + val expectedInOutput = + "Warning: using a single file as output (--output) is deprecated since Scala.js 1.3.0. Use --outputDir instead." assert(res.err.text().contains(expectedInOutput)) val testJsSize = os.size(dir / "test.js") @@ -80,9 +101,10 @@ class Tests extends munit.FunSuite { } assert(jsFileCount > 1) - val splitRunRes = os.proc("node", "test-output/main.js") + val splitRunRes = os + .proc("node", "test-output/main.js") .call(cwd = dir) - val splitRunOutput =splitRunRes.out.trim() + val splitRunOutput = splitRunRes.out.trim() assert(splitRunOutput == "asdf 2") }