Skip to content

Commit

Permalink
Add finer-grained validation for determining the symbols to defer dur…
Browse files Browse the repository at this point in the history
…ing symbol processing. (#3559)
  • Loading branch information
FWest98 authored Jan 10, 2025
1 parent f5f9ea4 commit c98377b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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

0 comments on commit c98377b

Please sign in to comment.