Skip to content

Commit

Permalink
Fixed Java identifier conversion for default group when has numbers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo authored Jul 9, 2024
1 parent 62a7ce0 commit db8737f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BuildConfigPlugin : Plugin<Project> {

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "(?<!^\\.)[^\\w.$]|(?<=^|\\.)(?=\\d)".toRegex()
internal val String.javaIdentifier get() = replace(javaClassRegex, "_")
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ class BuildConfigTypeUtilsTests {
assertEquals(mapStringListOfInt, nameOf("java.util.Map<String, List<Int?>>"))
assertEquals(mapStringListOfInt.copy(nullable = true), nameOf("java.util.Map<String, List<Int?>>?"))
assertEquals(mapStringListOfInt.copy(array = true), nameOf("java.util.Map<String, List<Int?>>[]"))
assertEquals(mapStringListOfInt.copy(nullable = true, array = true), nameOf("java.util.Map<String, List<Int?>>?[]"))
assertEquals(
mapStringListOfInt.copy(nullable = true, array = true),
nameOf("java.util.Map<String, List<Int?>>?[]")
)
}

@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)
}

}

0 comments on commit db8737f

Please sign in to comment.