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

Import aliases may introduce new naming collisions #2046

Open
damianw opened this issue Dec 18, 2024 · 3 comments · May be fixed by #2052
Open

Import aliases may introduce new naming collisions #2046

damianw opened this issue Dec 18, 2024 · 3 comments · May be fixed by #2052
Labels

Comments

@damianw
Copy link
Contributor

damianw commented Dec 18, 2024

Describe the bug
Import aliases introduced to avoid name collisions may themselves introduce new name collisions.

To Reproduce

class ImportAliasTest {

    @Test
    fun `name collision`() {
        val dupName1 = ClassName("com.foo", "MyClass")
        val dupName2 = ClassName("com.bar", "FooMyClass")
        val dupName3 = ClassName("com.baz", "MyClass")
        val file = FileSpec
            .builder("com.sample", "Sample")
            .addFunction(
                FunSpec.builder("foo")
                    .addStatement("%T", dupName1)
                    .addStatement("%T", dupName2)
                    .addStatement("%T", dupName3)
                    .build()
            )
            .build()
        assertEquals("?", file.toString())
    }
}

It is not clear what KotlinPoet should generate (hence "?"), but what it does generate currently is invalid:

package com.sample

import com.bar.FooMyClass
import com.baz.MyClass as BazMyClass
import com.foo.MyClass as FooMyClass

public fun foo() {
  com.foo.MyClass
  FooMyClass
  BazMyClass
}

Expected behavior
The generated code should not have any name collisions. One option might be:

package com.sample

import com.bar.FooMyClass as BarFooMyClass
import com.baz.MyClass as BazMyClass
import com.foo.MyClass as FooMyClass

public fun foo() {
  FooMyClass
  BarFooMyClass
  BazMyClass
}
@damianw damianw added the bug label Dec 18, 2024
@JakeWharton
Copy link
Collaborator

We'll probably just add underscores like we do for other name collisions. Good find.

@Egorand
Copy link
Collaborator

Egorand commented Dec 18, 2024

@damianw please feel free to send us a PR if you're interested!

@damianw
Copy link
Contributor Author

damianw commented Dec 19, 2024

I will take a look 👀

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

Successfully merging a pull request may close this issue.

3 participants