From db8737f2d19ecebd512a8342285739b82dc95ec3 Mon Sep 17 00:00:00 2001 From: Guillermo Mazzola Date: Tue, 9 Jul 2024 11:28:45 +0200 Subject: [PATCH] Fixed Java identifier conversion for default group when has numbers (#162) --- .../gmazzo/buildconfig/BuildConfigPlugin.kt | 2 +- .../gmazzo/buildconfig/BuildConfigTypeUtils.kt | 3 +++ .../buildconfig/BuildConfigTypeUtilsTests.kt | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt index 1d35439..4d0b746 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt @@ -102,7 +102,7 @@ class BuildConfigPlugin : Plugin { sourceSet.className.convention("${prefix}BuildConfig") sourceSet.packageName.convention(when (sourceSet) { - defaultSS -> defaultPackage.map { it.replace("[^a-zA-Z._$]".toRegex(), "_") } + defaultSS -> defaultPackage.map(String::javaIdentifier) else -> defaultSS.packageName }) sourceSet.generator.convention( diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt index b4655f2..cb28701 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt @@ -162,3 +162,6 @@ internal fun Any?.asVarArg(): Array<*> = when (this) { is Map<*, *> -> entries.asSequence().flatMap { (k, v) -> sequenceOf(k, v) }.toList().toTypedArray() else -> arrayOf(this) } + +private val javaClassRegex = "(?>")) assertEquals(mapStringListOfInt.copy(nullable = true), nameOf("java.util.Map>?")) assertEquals(mapStringListOfInt.copy(array = true), nameOf("java.util.Map>[]")) - assertEquals(mapStringListOfInt.copy(nullable = true, array = true), nameOf("java.util.Map>?[]")) + assertEquals( + mapStringListOfInt.copy(nullable = true, array = true), + nameOf("java.util.Map>?[]") + ) + } + + @Test + fun testJavaIdentifier() { + assertEquals("com.example.app", "com.example.app".javaIdentifier) + assertEquals("com.example.app10", "com.example.app10".javaIdentifier) + assertEquals("com.example.my10app", "com.example.my10app".javaIdentifier) + assertEquals("com.example._10app", "com.example.10app".javaIdentifier) + assertEquals("com.example.app$10", "com.example.app$10".javaIdentifier) + assertEquals("com.example_app", "com.example-app".javaIdentifier) } }