diff --git a/modules/cli-options/src/main/scala/scala/cli/commands/ScalacOptions.scala b/modules/cli-options/src/main/scala/scala/cli/commands/ScalacOptions.scala index ed129a51ab..da664413b6 100644 --- a/modules/cli-options/src/main/scala/scala/cli/commands/ScalacOptions.scala +++ b/modules/cli-options/src/main/scala/scala/cli/commands/ScalacOptions.scala @@ -33,11 +33,20 @@ object ScalacOptions { private val scalacOptionsPurePrefixes = Set("-V", "-W", "-X", "-Y") private val scalacOptionsPrefixes = - Set("-g", "-language", "-opt", "-P", "-target") ++ scalacOptionsPurePrefixes + Set("-g", "-language", "-opt", "-P", "-target", "-source") ++ scalacOptionsPurePrefixes private val scalacAliasedOptions = // these options don't require being passed after -O and accept an arg Set("-encoding", "-release", "-color") private val scalacNoArgAliasedOptions = // these options don't require being passed after -O and don't accept an arg - Set("-nowarn", "-feature", "-deprecation") + Set( + "-nowarn", + "-feature", + "-deprecation", + "-rewrite", + "-old-syntax", + "-new-syntax", + "-indent", + "-no-indent" + ) /** This includes all the scalac options which disregard inputs and print a help and/or context * message instead. diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala index 2aaa21b3ad..acb04c6ff4 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -2573,4 +2573,53 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) expect(res.out.trim() == "€") } } + + if (actualScalaVersion.startsWith("3") || actualScalaVersion.startsWith("2.13")) { + val fileName = "Main.scala" + val expectedOutput = "Hello" + val oldSyntaxCode = + s"""object Main extends App { + | if (true) println("$expectedOutput") else println("Error") + |} + |""".stripMargin + val newSyntaxCode = + s"""object Main extends App { + | if true then println("$expectedOutput") else println("Error") + |} + |""".stripMargin + + test("rewrite code to new syntax and then run it correctly (no -O required)") { + TestInputs(os.rel / fileName -> oldSyntaxCode) + .fromRoot { root => + val res = os.proc( + TestUtil.cli, + fileName, + "-new-syntax", + "-rewrite", + "-source:3.2-migration" + ).call(cwd = root, stderr = os.Pipe) + val filePath = root / fileName + expect(res.err.trim().contains(s"[patched file $filePath]")) + expect(os.read(filePath) == newSyntaxCode) + expect(res.out.trim() == expectedOutput) + } + } + + test("rewrite code to old syntax and then run it correctly (no -O required)") { + TestInputs(os.rel / fileName -> newSyntaxCode) + .fromRoot { root => + val res = os.proc( + TestUtil.cli, + fileName, + "-old-syntax", + "-rewrite", + "-source:3.2-migration" + ).call(cwd = root, stderr = os.Pipe) + val filePath = root / fileName + expect(res.err.trim().contains(s"[patched file $filePath]")) + expect(os.read(filePath) == oldSyntaxCode) + expect(res.out.trim() == expectedOutput) + } + } + } }