-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
812 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
scalafix-rules/src/main/resources-3/META-INF/services/scalafix.v1.Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
scalafix-rules/src/main/scala-3/scalafix/internal/rule/ExplicitResultTypes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package scalafix.internal.rule | ||
|
||
import scala.meta.* | ||
|
||
import buildinfo.RulesBuildInfo | ||
import dotty.tools.pc.ScalaPresentationCompiler | ||
import metaconfig.Configured | ||
import scalafix.internal.pc.PcExplicitResultTypes | ||
import scalafix.patch.Patch | ||
import scalafix.v1.* | ||
|
||
final class ExplicitResultTypes( | ||
val config: ExplicitResultTypesConfig, | ||
fallback: Option[PcExplicitResultTypes] | ||
) extends SemanticRule("ExplicitResultTypes") | ||
with ExplicitResultTypesBase[Scala3Printer] { | ||
|
||
def this() = this(ExplicitResultTypesConfig.default, None) | ||
|
||
val compilerScalaVersion: String = RulesBuildInfo.scalaVersion | ||
|
||
private def toBinaryVersion(v: String) = v.split('.').take(2).mkString(".") | ||
|
||
override def description: String = | ||
"Inserts type annotations for inferred public members." | ||
|
||
override def isRewrite: Boolean = true | ||
|
||
override def afterComplete(): Unit = { | ||
shutdownCompiler() | ||
} | ||
|
||
private def shutdownCompiler(): Unit = { | ||
fallback.foreach(_.shutdownCompiler()) | ||
} | ||
|
||
override def withConfiguration(config: Configuration): Configured[Rule] = { | ||
config.conf // Support deprecated explicitReturnTypes config | ||
.getOrElse("explicitReturnTypes", "ExplicitResultTypes")( | ||
ExplicitResultTypesConfig.default | ||
) | ||
.map(c => | ||
new ExplicitResultTypes( | ||
c, | ||
Option { | ||
if ( | ||
toBinaryVersion(config.scalaVersion) == toBinaryVersion( | ||
compilerScalaVersion | ||
) | ||
) | ||
PcExplicitResultTypes | ||
.static(config, new ScalaPresentationCompiler()) | ||
else | ||
PcExplicitResultTypes.dynamic(config) | ||
} | ||
) | ||
) | ||
} | ||
|
||
override def fix(implicit ctx: SemanticDocument): Patch = | ||
try { | ||
implicit val printer = new Scala3Printer(fallback) | ||
unsafeFix() | ||
} catch { | ||
case _: Throwable if !config.fatalWarnings => | ||
Patch.empty | ||
} | ||
|
||
} | ||
|
||
class Scala3Printer( | ||
fallback: Option[PcExplicitResultTypes] | ||
) extends Printer { | ||
|
||
def defnType( | ||
defn: Defn, | ||
replace: Token, | ||
space: String | ||
)(implicit | ||
ctx: SemanticDocument | ||
): Option[Patch] = { | ||
fallback.flatMap(_.defnType(replace)) | ||
} | ||
} |
Oops, something went wrong.