Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly escape class names in Optics KSP when a property clashes with a package name #3326

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private fun processIsoSyntax(ele: ADT, dsl: ValueClassDsl, className: String): S
}

private fun resolveClassName(ele: ADT): Pair<String, String> = if (hasPackageCollisions(ele)) {
val classNameAlias = ele.sourceClassName.replace(".", "")
val classNameAlias = ele.sourceClassName.replace(".", "").replace("`", "").sanitize()
val aliasImport = "import ${ele.sourceClassName} as $classNameAlias"
classNameAlias to aliasImport
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fun String.plusIfNotBlank(prefix: String = "", postfix: String = "") =
* Sanitizes each delimited section if it matches with Kotlin reserved keywords.
*/
fun KSName.asSanitizedString(delimiter: String = ".", prefix: String = "") =
asString().splitToSequence(delimiter).joinToString(delimiter, prefix) { if (it in KOTLIN_KEYWORDS) "`$it`" else it }
asString().sanitize(delimiter, prefix)

/**
* Sanitizes each delimited section if it matches with Kotlin reserved keywords.
*/
fun String.sanitize(delimiter: String = ".", prefix: String = "") =
splitToSequence(delimiter).joinToString(delimiter, prefix) { if (it in KOTLIN_KEYWORDS) "`$it`" else it }

private val KOTLIN_KEYWORDS = setOf(
// Hard keywords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ class DSLTests {
""".compilationSucceeds()
}

@Test
fun `DSL for a class in a package including keywords, issue #3134, part 1`() {
"""
|package com.sats.core.data.workouts.models
|
|$imports
|
|@optics
|data class Source(val program: String) {
| companion object
|}
|
""".compilationSucceeds()
}

/*
This test is for a very specific corner case, in which:
- The package name includes a Kotlin keyword, so we need to escape them,
- There's at least one property which shares name with part of the package,
so we need to include an explicit import
*/
@Test
fun `DSL for a class in a package including keywords and conflicting fields, issue #3134, part 2`() {
"""
|package com.sats.core.data.workouts.models
|
|$imports
|
|@optics
|data class Source(val models: String) {
| companion object
|}
|
""".compilationSucceeds()
}

@Test
fun `DSL works with variance, issue #3057`() {
"""
Expand Down