rewrite-kotlin icon indicating copy to clipboard operation
rewrite-kotlin copied to clipboard

Support recipe development using Kotlin's Deprecated annotation `replaceWith`

Open timtebeek opened this issue 2 years ago • 0 comments

Kotlin's Deprecated annotation comes with a replaceWith attribute.

If present, specifies a code fragment which should be used as a replacement for the deprecated API usage.

This can speed up recipe development, as simple replacements are specified directly on the deprecated methods. A few different samples taken from Arrow's deprecation of Validated:

arrow/core/Either.kt

  @Deprecated(
    "orNone is being renamed to getOrNone to be more consistent with the Kotlin Standard Library naming",
    ReplaceWith("getOrNone()")
  )
  public fun orNone(): Option<B> = getOrNone()

arrow/core/Validated.kt

  @Deprecated(
    DeprMsg + "Use isLeft on Either after refactoring",
    ReplaceWith("toEither().isLeft()")
  )
  public val isInvalid: Boolean =
    fold({ true }, { false })
@Deprecated(
  DeprMsg + "Use left instead to construct the equivalent Either value",
  ReplaceWith("this.left()", "arrow.core.left")
)
public inline fun <E> E.invalid(): Validated<E, Nothing> =
  Invalid(this)

Goal

Ideally these replacements can be done just by looking at the runtime classpath for any deprecated methods used in the current code, and replacing those instances directly. As an intermediate we can look at generating replacement recipes for some simple cases only, and/or write those recipe stubs out to yaml files to be applied separately. There may be more options, but figured start a discussion here with this initial outline.

timtebeek avatar Feb 22 '23 15:02 timtebeek