openrewrite / rewrite-kotlin

Work-in-progress implementation of Kotlin language support for OpenRewrite.
Apache License 2.0
38 stars 11 forks source link

Idempotent print issue: Select disappears #556

Closed traceyyoshima closed 5 months ago

traceyyoshima commented 6 months ago

Reference: https://github.com/evitwilly/Kotlin-Algorithms-and-Design-Patterns/blob/develop/src/main/kotlin/design_patterns/Builder.kt

    companion object {
        fun newBuilder() = Pony2().Builder()
    }
kunli2 commented 6 months ago

this is a full test

    @Test
    void innerClassInvocation() {
        rewriteRun(
          kotlin(
            """
              class Pony2 {
                  inner class Builder

                  companion object {
                      fun newBuilder() = Pony2().Builder()
                  }
              }
              """
          )
        );
    }

The root cause here is both Pony2() and Builder() are KtCallExpression, we translate KtCallExpression to different LST models by types from FIR, for this case, both are CONSTRUCTOR types here, so they are all turned into two J.NewClass here.

@traceyyoshima , what do you think about the solution for this?

knutwannheden commented 6 months ago

For context here is the diff this test produces:

diff --git a/openRewriteFile.kt b/openRewriteFile.kt
index e84a1aa..79e2d9d 100644
--- a/openRewriteFile.kt
+++ b/openRewriteFile.kt
@@ -2,6 +2,6 @@ 
     inner class Builder

     companion object {
-        fun newBuilder() = Pony2().Builder()
+        fun newBuilder() = Builder()
     }
 }