scalafix icon indicating copy to clipboard operation
scalafix copied to clipboard

New built-in rule to update Scala 3 code to the new syntax

Open mlachkar opened this issue 4 years ago • 2 comments
trafficstars

We could create a new rule that groups many small rewrites or lint messages to update the code to the new syntax: Some of those rewrite exist already in Scalafmt, but I think having them here too should be ok

  • Rewrite underscores in imports and type arguments
  • Rewrite if to if then
  • Drop the case in lambdas when it is not needed anymore
  • replace implicit class with extension
  • replace package object with top level defs
  • replace implicit with using/given

Some of these options are maybe quite difficult to implement, but we can maybe Lint about the possible rewrite instead.

mlachkar avatar Jun 28 '21 14:06 mlachkar

I think we should start with a Source3 rule that turns Scala 2 code into Scala 2 code that compiles under -Xsource:3. It's really what people migrating must do, and it should be possible to automate it fully.

Replacing implicit with using/given is just a nice to have (and will be needed down the line in 3.x) but in most cases would require human interaction to get it right. That would also make testing possible with the testkit: we can define a outputSource3 module using the same version of the compiler used for input but with an extra -Xsource:3, thus verifying that produced code actually compiles.

bjaglin avatar Jun 30 '21 20:06 bjaglin

I wonder if this "migration" rule should be available in scalafix-rules though - could it be implemented/tested in https://github.com/scalacenter/scala3-migrate?

bjaglin avatar Jun 30 '21 20:06 bjaglin

Rewrite underscores in imports and type arguments

https://scalameta.org/scalafmt/docs/configuration.html#scala3-rewrites

bjaglin avatar Oct 28 '22 07:10 bjaglin

Closing, as scalafmt covers a lot of these rewrites, and more (semantic) can be added as community rules

bjaglin avatar Jun 30 '23 23:06 bjaglin