From ab1352358dc59c437c31b323e137caf43242a87c Mon Sep 17 00:00:00 2001 From: NataliaPeterwas <111683562+nataliapeterwas@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:47:28 +0100 Subject: [PATCH 1/4] fix bug --- .../declaration/KoParentDeclarationCore.kt | 22 +++++++++++-------- .../konsist/core/model/DataCore.kt | 9 -------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt index 925ad60e9..d3f6a50e2 100644 --- a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt +++ b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt @@ -105,25 +105,29 @@ internal class KoParentDeclarationCore( if (import.alias != null) { import.alias?.name == innerName } else { - import.name.substringAfterLast(".") == innerName + import.name.substringAfterLast(".") == outerName } } val fullyQualifiedName = import ?.name - ?: (containingFile.packagee?.name + "." + name) + ?: (if (containingFile.packagee != null) { + containingFile.packagee?.name + "." + } else { + "" + } + name) val isAlias = import?.alias != null ( - import?.alias - ?: getClass(outerName, fullyQualifiedName, isAlias, containingFile) - ?: getInterface(outerName, fullyQualifiedName, isAlias, containingFile) - ?: getTypeAlias(outerName, fullyQualifiedName, containingFile) - ?: KoExternalDeclarationCore.getInstance(outerName, ktSuperTypeListEntry) - ) - as? KoDeclarationCastProvider + import?.alias + ?: getClass(outerName, fullyQualifiedName, isAlias, containingFile) + ?: getInterface(outerName, fullyQualifiedName, isAlias, containingFile) + ?: getTypeAlias(outerName, fullyQualifiedName, containingFile) + ?: KoExternalDeclarationCore.getInstance(outerName, ktSuperTypeListEntry) + ) + as? KoDeclarationCastProvider } override val name: String by lazy { diff --git a/lib/src/main/kotlin/com/lemonappdev/konsist/core/model/DataCore.kt b/lib/src/main/kotlin/com/lemonappdev/konsist/core/model/DataCore.kt index b69a90df8..cc7adf242 100644 --- a/lib/src/main/kotlin/com/lemonappdev/konsist/core/model/DataCore.kt +++ b/lib/src/main/kotlin/com/lemonappdev/konsist/core/model/DataCore.kt @@ -60,9 +60,6 @@ fun getClass( ?: containingFile .classes() .firstOrNull { decl -> decl.fullyQualifiedName == declarationQualifiedName } - ?: containingFile - .classes() - .firstOrNull { decl -> decl.name == name } } fun getInterface( @@ -84,9 +81,6 @@ fun getInterface( ?: containingFile .interfaces() .firstOrNull { decl -> decl.fullyQualifiedName == declarationQualifiedName } - ?: containingFile - .interfaces() - .firstOrNull { decl -> decl.name == name } } fun getObject( @@ -108,9 +102,6 @@ fun getObject( ?: containingFile .objects() .firstOrNull { decl -> decl.fullyQualifiedName == declarationQualifiedName } - ?: containingFile - .objects() - .firstOrNull { decl -> decl.name == name } } fun getTypeAlias( From 62652e15b33f89a322a79185c7e97c49f85a89c5 Mon Sep 17 00:00:00 2001 From: NataliaPeterwas <111683562+nataliapeterwas@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:10:09 +0100 Subject: [PATCH 2/4] add tests --- ...ationForKoSourceDeclarationProviderTest.kt | 35 +++++++++++++++++++ ...ith-parent-class-with-the-same-name.kttest | 7 ++++ ...parent-interface-with-the-same-name.kttest | 7 ++++ ...parent-interface-with-the-same-name.kttest | 7 ++++ ...ith-parent-class-with-the-same-name.kttest | 7 ++++ ...parent-interface-with-the-same-name.kttest | 7 ++++ 6 files changed, 70 insertions(+) create mode 100644 lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-class-with-the-same-name.kttest create mode 100644 lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-interface-with-the-same-name.kttest create mode 100644 lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/interface-with-parent-interface-with-the-same-name.kttest create mode 100644 lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-class-with-the-same-name.kttest create mode 100644 lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-interface-with-the-same-name.kttest diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/KoParentDeclarationForKoSourceDeclarationProviderTest.kt b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/KoParentDeclarationForKoSourceDeclarationProviderTest.kt index f3f5806f3..18f380051 100644 --- a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/KoParentDeclarationForKoSourceDeclarationProviderTest.kt +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/KoParentDeclarationForKoSourceDeclarationProviderTest.kt @@ -302,6 +302,20 @@ class KoParentDeclarationForKoSourceDeclarationProviderTest { null, null, ), + arguments( + "class-with-parent-interface-with-the-same-name", + KoInterfaceDeclaration::class, + KoClassDeclaration::class, + null, + "com.samplepackage.SampleInterface.SampleName", + ), + arguments( + "class-with-parent-class-with-the-same-name", + KoClassDeclaration::class, + KoInterfaceDeclaration::class, + null, + "com.samplepackage.SampleInterface.SampleName", + ), ) @Suppress("unused", "detekt.LongMethod") @@ -364,6 +378,13 @@ class KoParentDeclarationForKoSourceDeclarationProviderTest { null, null, ), + arguments( + "interface-with-parent-interface-with-the-same-name", + KoInterfaceDeclaration::class, + KoClassDeclaration::class, + null, + "com.samplepackage.SampleInterface.SampleName", + ), ) @Suppress("unused", "detekt.LongMethod") @@ -510,6 +531,20 @@ class KoParentDeclarationForKoSourceDeclarationProviderTest { null, null, ), + arguments( + "object-with-parent-interface-with-the-same-name", + KoInterfaceDeclaration::class, + KoClassDeclaration::class, + null, + "com.samplepackage.SampleInterface.SampleName", + ), + arguments( + "object-with-parent-class-with-the-same-name", + KoClassDeclaration::class, + KoInterfaceDeclaration::class, + null, + "com.samplepackage.SampleInterface.SampleName", + ), ) } } diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-class-with-the-same-name.kttest b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-class-with-the-same-name.kttest new file mode 100644 index 000000000..1ceef7ce9 --- /dev/null +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-class-with-the-same-name.kttest @@ -0,0 +1,7 @@ +package com.samplepackage + +data class SampleName(val data: String) : SampleInterface.SampleName() + +interface SampleInterface { + open class SampleName +} diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-interface-with-the-same-name.kttest b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-interface-with-the-same-name.kttest new file mode 100644 index 000000000..8e333c8b5 --- /dev/null +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/class-with-parent-interface-with-the-same-name.kttest @@ -0,0 +1,7 @@ +package com.samplepackage + +data class SampleName(val data: String) : SampleInterface.SampleName + +interface SampleInterface { + interface SampleName +} diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/interface-with-parent-interface-with-the-same-name.kttest b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/interface-with-parent-interface-with-the-same-name.kttest new file mode 100644 index 000000000..a40ce0d8d --- /dev/null +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/interface-with-parent-interface-with-the-same-name.kttest @@ -0,0 +1,7 @@ +package com.samplepackage + +interface SampleName : SampleInterface.SampleName + +interface SampleInterface { + interface SampleName +} diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-class-with-the-same-name.kttest b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-class-with-the-same-name.kttest new file mode 100644 index 000000000..2667935b6 --- /dev/null +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-class-with-the-same-name.kttest @@ -0,0 +1,7 @@ +package com.samplepackage + +object SampleName : SampleInterface.SampleName() + +interface SampleInterface { + open class SampleName +} diff --git a/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-interface-with-the-same-name.kttest b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-interface-with-the-same-name.kttest new file mode 100644 index 000000000..65ee2139a --- /dev/null +++ b/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koparent/snippet/forkosourcedeclarationprovider/object-with-parent-interface-with-the-same-name.kttest @@ -0,0 +1,7 @@ +package com.samplepackage + +object SampleName : SampleInterface.SampleName + +interface SampleInterface { + interface SampleName +} From f4f63750cf40002778a61954345ef16d3b56e029 Mon Sep 17 00:00:00 2001 From: NataliaPeterwas <111683562+nataliapeterwas@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:12:13 +0100 Subject: [PATCH 3/4] clean project --- .../declaration/KoParentDeclarationCore.kt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt index d3f6a50e2..605f8b052 100644 --- a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt +++ b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt @@ -112,22 +112,24 @@ internal class KoParentDeclarationCore( val fullyQualifiedName = import ?.name - ?: (if (containingFile.packagee != null) { - containingFile.packagee?.name + "." - } else { - "" - } + name) + ?: ( + if (containingFile.packagee != null) { + containingFile.packagee?.name + "." + } else { + "" + } + name + ) val isAlias = import?.alias != null ( - import?.alias - ?: getClass(outerName, fullyQualifiedName, isAlias, containingFile) - ?: getInterface(outerName, fullyQualifiedName, isAlias, containingFile) - ?: getTypeAlias(outerName, fullyQualifiedName, containingFile) - ?: KoExternalDeclarationCore.getInstance(outerName, ktSuperTypeListEntry) - ) - as? KoDeclarationCastProvider + import?.alias + ?: getClass(outerName, fullyQualifiedName, isAlias, containingFile) + ?: getInterface(outerName, fullyQualifiedName, isAlias, containingFile) + ?: getTypeAlias(outerName, fullyQualifiedName, containingFile) + ?: KoExternalDeclarationCore.getInstance(outerName, ktSuperTypeListEntry) + ) + as? KoDeclarationCastProvider } override val name: String by lazy { From 1beb188f49c6037c8fd88ad8a477cbfc0183e43c Mon Sep 17 00:00:00 2001 From: NataliaPeterwas <111683562+nataliapeterwas@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:19:40 +0100 Subject: [PATCH 4/4] clean code --- .../core/declaration/KoParentDeclarationCore.kt | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt index 605f8b052..2b271be47 100644 --- a/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt +++ b/lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoParentDeclarationCore.kt @@ -110,15 +110,8 @@ internal class KoParentDeclarationCore( } val fullyQualifiedName = - import - ?.name - ?: ( - if (containingFile.packagee != null) { - containingFile.packagee?.name + "." - } else { - "" - } + name - ) + import?.name + ?: "${containingFile.packagee?.name?.let { "$it." } ?: ""}$name" val isAlias = import?.alias != null