Skip to content

Commit 0ee742d

Browse files
committed
Ensure compiler options relevant to syntax rewriting don't require being passed after -O:
- `-rewrite` - `-new-syntax` - `-old-syntax` - `-source:<target>` - `-indent` - `-no-indent`
1 parent 68064f9 commit 0ee742d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

modules/cli-options/src/main/scala/scala/cli/commands/ScalacOptions.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ object ScalacOptions {
3333
private val scalacOptionsPurePrefixes =
3434
Set("-V", "-W", "-X", "-Y")
3535
private val scalacOptionsPrefixes =
36-
Set("-g", "-language", "-opt", "-P", "-target") ++ scalacOptionsPurePrefixes
36+
Set("-g", "-language", "-opt", "-P", "-target", "-source") ++ scalacOptionsPurePrefixes
3737
private val scalacAliasedOptions = // these options don't require being passed after -O and accept an arg
3838
Set("-encoding", "-release", "-color")
3939
private val scalacNoArgAliasedOptions = // these options don't require being passed after -O and don't accept an arg
40-
Set("-nowarn", "-feature", "-deprecation")
40+
Set(
41+
"-nowarn",
42+
"-feature",
43+
"-deprecation",
44+
"-rewrite",
45+
"-old-syntax",
46+
"-new-syntax",
47+
"-indent",
48+
"-no-indent"
49+
)
4150

4251
/** This includes all the scalac options which disregard inputs and print a help and/or context
4352
* message instead.

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,4 +2573,53 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
25732573
expect(res.out.trim() == "")
25742574
}
25752575
}
2576+
2577+
if (actualScalaVersion.startsWith("3") || actualScalaVersion.startsWith("2.13")) {
2578+
val fileName = "Main.scala"
2579+
val expectedOutput = "Hello"
2580+
val oldSyntaxCode =
2581+
s"""object Main extends App {
2582+
| if (true) println("$expectedOutput") else println("Error")
2583+
|}
2584+
|""".stripMargin
2585+
val newSyntaxCode =
2586+
s"""object Main extends App {
2587+
| if true then println("$expectedOutput") else println("Error")
2588+
|}
2589+
|""".stripMargin
2590+
2591+
test("rewrite code to new syntax and then run it correctly (no -O required)") {
2592+
TestInputs(os.rel / fileName -> oldSyntaxCode)
2593+
.fromRoot { root =>
2594+
val res = os.proc(
2595+
TestUtil.cli,
2596+
fileName,
2597+
"-new-syntax",
2598+
"-rewrite",
2599+
"-source:3.2-migration"
2600+
).call(cwd = root, stderr = os.Pipe)
2601+
val filePath = root / fileName
2602+
expect(res.err.trim().contains(s"[patched file $filePath]"))
2603+
expect(os.read(filePath) == newSyntaxCode)
2604+
expect(res.out.trim() == expectedOutput)
2605+
}
2606+
}
2607+
2608+
test("rewrite code to old syntax and then run it correctly (no -O required)") {
2609+
TestInputs(os.rel / fileName -> newSyntaxCode)
2610+
.fromRoot { root =>
2611+
val res = os.proc(
2612+
TestUtil.cli,
2613+
fileName,
2614+
"-old-syntax",
2615+
"-rewrite",
2616+
"-source:3.2-migration"
2617+
).call(cwd = root, stderr = os.Pipe)
2618+
val filePath = root / fileName
2619+
expect(res.err.trim().contains(s"[patched file $filePath]"))
2620+
expect(os.read(filePath) == oldSyntaxCode)
2621+
expect(res.out.trim() == expectedOutput)
2622+
}
2623+
}
2624+
}
25762625
}

0 commit comments

Comments
 (0)