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

Add finer-grained validation for determining the symbols to defer during symbol processing #3559

Merged
merged 1 commit into from
Jan 10, 2025
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 @@ -14,7 +14,9 @@ import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSAnnotation
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.validate

class OpticsProcessor(
Expand All @@ -27,7 +29,15 @@ class OpticsProcessor(
val (resolved, deferred) = resolver
.getSymbolsWithAnnotation("arrow.optics.optics")
.filterIsInstance<KSClassDeclaration>()
.partition { it.validate() }
.partition {
it.validate { _, element ->
when (element) {
is KSAnnotation -> false
is KSFunctionDeclaration -> false
else -> true
}
}
}
resolved.forEach(::processClass)

// If types used by the annotated class are by other processors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ class LensTests {
""".evals("r" to true)
}

@Test
fun `Lenses are generated for data class referencing its own lenses for type inference`() {
"""
|$`package`
|$imports
|@optics
|data class UsingLens(val field: String) {
| fun getLens() = UsingLens.field
| companion object
|}
|
|val i: Lens<UsingLens, String> = UsingLens.field
|val r = i != null
""".evals("r" to true)
}

@Test
fun `Lenses are not generated for unresolved types`() {
"""
|$`package`
|$imports
|@optics
|data class InvalidType(val field: Foo) {
| companion object
|}
""".compilationFails()
}

@Test
fun `Lenses which mentions imported elements`() {
"""
Expand Down