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

Always import member without package name #1841

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Change Log
Change: kotlinx-metadata 0.9.0. Note that the `KotlinClassMetadata .read` is deprecated in 0.9.0 and replaced with `readStrict` (#1830).
* Fix: `KSAnnotation.toAnnotationSpec` writes varargs in place instead of making them an array to work around a Kotlin
issue with `OptIn` annotations (#1831).
* Fix: `MemberName`s without a package are now correctly imported (#1841)

## Version 1.16.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,10 @@ internal class CodeWriter constructor(
}

private fun importableMember(memberName: MemberName) {
if (memberName.packageName.isNotEmpty()) {
val simpleName = imports[memberName.canonicalName]?.alias ?: memberName.simpleName
// Check for name clashes with types.
if (memberName.isExtension || simpleName !in importableTypes) {
importableMembers[simpleName] = importableMembers.getValue(simpleName) + memberName
}
val simpleName = imports[memberName.canonicalName]?.alias ?: memberName.simpleName
// Check for name clashes with types.
if (memberName.isExtension || simpleName !in importableTypes) {
importableMembers[simpleName] = importableMembers.getValue(simpleName) + memberName
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,29 @@ class MemberNameTest {
)
}

@Test fun importMemberWithoutPackage() {
val createTaco = MemberName("", "createTaco")
val file = FileSpec.builder("com.example", "Test")
.addFunction(
FunSpec.builder("makeTacoHealthy")
.addStatement("val taco = %M()", createTaco)
.build(),
)
.build()
assertThat(file.toString()).isEqualTo(
"""
|package com.example
|
|import createTaco
|
|public fun makeTacoHealthy() {
| val taco = createTaco()
|}
|
""".trimMargin(),
)
}

// https://github.com/square/kotlinpoet/issues/1089
@Test fun `extension MemberName imported if name clash`() {
val hashCode = MemberName("kotlin", "hashCode", isExtension = true)
Expand Down