Skip to content

Commit

Permalink
Always import member without package name (#1841)
Browse files Browse the repository at this point in the history
* Always import member without package name

* Add fix to changelog

* Update docs/changelog.md

Co-authored-by: Jake Wharton <github@jakewharton.com>

---------

Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
Co-authored-by: Jake Wharton <github@jakewharton.com>
  • Loading branch information
3 people committed Feb 28, 2024
1 parent 1dc9260 commit 83a73d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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

0 comments on commit 83a73d2

Please sign in to comment.