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

Aliased imports not working for nested classes and generate unused imports and usage of FQNs #1838

Closed
mcarleio opened this issue Feb 23, 2024 · 2 comments · Fixed by #1876
Closed
Labels

Comments

@mcarleio
Copy link
Contributor

Describe the bug
An aliased import added via addAliasedImport for a nested class seems to be never used, but still generates unused imports.

To Reproduce

package org.example

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec

class Person {
    class Id
}

fun main() {
    println(
        FileSpec.builder("org.example", "SomeFile")
            .addAliasedImport(Person.Id::class, "PID")
            .addFunction(
                FunSpec.builder("pid")
                    .returns(Person.Id::class)
                    .addCode("return %T()", Person.Id::class)
                    .build()
            )
            .build()
    )
}

Expected behavior

package org.example

import org.example.Person.Id as PID

public fun pid(): PID = PID()

Actual behavior

package org.example

import org.example.Person.Id as PID

public fun pid(): Person.Id = Person.Id()

Additional context

If you change the package of the FileSpec to a different package than the nested class (e.g. FileSpec.builder("org.elsewhere", "SomeFile")) and run the same again, you will even get two unused imports and FQNs instead of the alias are used:

expected

package org.elsewhere

import org.example.Person.Id as PID

public fun pid(): PID = PID()

actual

package org.elsewhere

import org.example.Person
import org.example.Person.Id as PID

public fun pid(): org.example.Person.Id = org.example.Person.Id()
@mcarleio mcarleio added the bug label Feb 23, 2024
@mcarleio mcarleio changed the title Aliased imports not working for nested classes and generate unused imports and uses FQNs Aliased imports not working for nested classes and generate unused imports and usage of FQNs Feb 23, 2024
@mcarleio
Copy link
Contributor Author

As @Egorand recognized in #1839, I was not using the latest version 1.16.0 (I was using 1.15.3) - sorry!

But this issue is still valid, although the behavior already changed a bit for the first case. The actual behavior there is now the same as for the last case:

package org.example

import org.example.Person
import org.example.Person.Id as PID

public fun pid(): org.example.Person.Id = org.example.Person.Id()

@Egorand
Copy link
Collaborator

Egorand commented Feb 25, 2024

Yep, thanks @mcarleio, I was able to reproduce this one. The team will take a look!

psymoon added a commit that referenced this issue Apr 1, 2024
psymoon added a commit that referenced this issue Apr 1, 2024
psymoon added a commit that referenced this issue Apr 1, 2024
psymoon added a commit that referenced this issue Apr 2, 2024
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.

2 participants