From c5b9464da696b9085f2db0d6ebe1e8194ed052c7 Mon Sep 17 00:00:00 2001 From: Mark Rebhan Date: Tue, 9 Jul 2024 15:34:02 -0700 Subject: [PATCH 1/2] Fix to prevent naming collisions Prepends parent class(es) to interface and function declarations to prevent naming collisions in the following scenario: 1. Create 2 top-level classes in the same package 2. Create an inner class with the same name in each class 3. Project does not compile because the generated code has a fully qualified name collision because the parent class names are not taken into account --- .../com/philo/compiler/JsNamedArgsVisitor.kt | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/jsnamedargs-compiler/src/main/kotlin/com/philo/compiler/JsNamedArgsVisitor.kt b/jsnamedargs-compiler/src/main/kotlin/com/philo/compiler/JsNamedArgsVisitor.kt index 774f068..8897ced 100644 --- a/jsnamedargs-compiler/src/main/kotlin/com/philo/compiler/JsNamedArgsVisitor.kt +++ b/jsnamedargs-compiler/src/main/kotlin/com/philo/compiler/JsNamedArgsVisitor.kt @@ -8,6 +8,7 @@ import com.google.devtools.ksp.processing.CodeGenerator import com.google.devtools.ksp.symbol.ClassKind import com.google.devtools.ksp.symbol.FunctionKind import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSDeclaration import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.symbol.KSTypeParameter import com.google.devtools.ksp.symbol.KSVisitorVoid @@ -65,7 +66,8 @@ class JsNamedArgsVisitor( processConstructorFunction( it, classDeclaration.typeParameters, - if (isInnerClass) classDeclaration.toClassName().enclosingClassName() else null + if (isInnerClass) classDeclaration.toClassName().enclosingClassName() else null, + classDeclaration.parentDeclaration ) } @@ -95,15 +97,32 @@ class JsNamedArgsVisitor( private fun processConstructorFunction( function: KSFunctionDeclaration, classTypeParameters: List, - outerParentClassTypeName: TypeName? = null + outerParentClassTypeName: TypeName?, + parentDeclaration: KSDeclaration?, ) { if (!function.isConstructor()) return val constructorOf = function.returnType?.toTypeName(classTypeParameters.toTypeParameterResolver()) ?: return val constructorOfBaseName = constructorOf.toBaseTypeName() - val interfaceName = "${constructorOfBaseName}ConstructorArgs" - val functionWithInterfaceName = "create${constructorOfBaseName}Wrapper" + val parentDeclarationNames = mutableListOf() + fun toParentDeclarationName(declaration: KSDeclaration?) { + if (declaration == null) return + + declaration.qualifiedName?.getShortName()?.let { + parentDeclarationNames.add(0, it) + } + + toParentDeclarationName(declaration.parentDeclaration) + } + + toParentDeclarationName(parentDeclaration) + + val interfaceName = parentDeclarationNames.joinToString("") + + "${constructorOfBaseName}ConstructorArgs" + val functionWithInterfaceName = "create" + + parentDeclarationNames.joinToString("") + + "${constructorOfBaseName}Wrapper" processFunction( interfaceName, From 7f01dcfcba755d4352d8ad376ccbcd22f3167752 Mon Sep 17 00:00:00 2001 From: Mark Rebhan Date: Tue, 9 Jul 2024 16:40:23 -0700 Subject: [PATCH 2/2] Bump version --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index e79c39e..b98dd7a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ plugins { allprojects { group = "com.philo" - version = "0.1.0" + version = "0.1.1" repositories { mavenLocal()