Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "matching simple name but different fully-qualified name" member imports #1605

Open
Egorand opened this issue Jun 28, 2023 · 0 comments

Comments

@Egorand
Copy link
Collaborator

Egorand commented Jun 28, 2023

Is your feature request related to a problem? Please describe.

On 1.14.2, the following test case (posted in #1604):

/**
 * Referenced code:
 *
 * ```Kotlin
 * package pkg1
 *
 * class Foo
 *
 * fun Foo.doSomething() {}
 * ```
 *
 * ```Kotlin
 * package pkg2
 *
 * class Bar
 *
 * fun Bar.doSomething() {}
 * ```
 */
@Test
fun test() {
    val fooClassName = ClassName("pkg1", "Foo")
    val fooExtensionName = MemberName("pkg1", "doSomething", isExtension = true)
    val barClassName = ClassName("pkg2", "Bar")
    val barExtensionName = MemberName("pkg2", "doSomething", isExtension = true)
    val fileSpec = FileSpec
        .builder("app", "MyFile")
        .addFunction(
            FunSpec
                .builder("doSomethingWith")
                .addParameter("foo", fooClassName)
                .addStatement("foo.%M()", fooExtensionName)
                .build()
        )
        .addFunction(
            FunSpec
                .builder("doSomethingWith")
                .addParameter("bar", barClassName)
                .addStatement("bar.%M()", barExtensionName)
                .build()
        )
        .build()
    val expected = """
        package app

        import kotlin.Unit
        import pkg1.Foo
        import pkg1.doSomething
        import pkg2.Bar
        import pkg2.doSomething

        public fun doSomethingWith(foo: Foo): Unit {
          foo.doSomething()
        }

        public fun doSomethingWith(bar: Bar): Unit {
          bar.doSomething()
        }
    """.trimIndent()
    assertEquals(expected, fileSpec.toString())
}

produces the following output:

package app
    
import pkg1.Foo
import pkg2.Bar
import pkg1.doSomething as pkg1DoSomething
import pkg2.doSomething as pkg2DoSomething
    
public fun doSomethingWith(foo: Foo) {
  foo.pkg1DoSomething()
}
    
public fun doSomethingWith(bar: Bar) {
  bar.pkg2DoSomething()
}

This code works, but the import aliases aren't necessary. The following code compiles as well:

package app

import kotlin.Unit
import pkg1.Foo
import pkg1.doSomething
import pkg2.Bar
import pkg2.doSomething

public fun doSomethingWith(foo: Foo): Unit {
  foo.doSomething()
}

public fun doSomethingWith(bar: Bar): Unit {
  bar.doSomething()
}

Note though that this will not compile:

package app

import pkg1.Foo
import pkg2.Foo

Describe the solution you'd like

The library should not treat member imports as conflicting if only their simple name matches, it should instead match by FQ name. Currently the matching logic is shared between member imports and type imports.

Describe alternatives you've considered

"Do nothing" is a good alternative here. This is purely a stylistic improvement and shouldn't have any effect on the correctness of generated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant