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

Fix modifier annotation parsing in FIR #2210

Merged
merged 1 commit into from
Jul 29, 2024
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 @@ -577,9 +577,10 @@ private fun FirContext.parseModifier(
firClass.primaryConstructorIfAny(firSession)!!.valueParameterSymbols.map { parameter ->
val name = parameter.name.identifier
val parameterType = parameter.resolvedReturnType.toFqType()
val property = firClass.declarations.filterIsInstance<FirProperty>().single { it.name == parameter.name }

val defaultAnnotation = findDefaultAnnotation(parameter.annotations)
val deprecation = findDeprecationAnnotation(parameter.annotations)
val defaultAnnotation = findDefaultAnnotation(property.annotations)
val deprecation = findDeprecationAnnotation(property.annotations)
?.toDeprecation { "$memberType.$name" }
val documentation = parameter.source?.findAndParseKDoc()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package app.cash.redwood.tooling.schema
import app.cash.redwood.layout.RedwoodLayout
import app.cash.redwood.layout.Row
import app.cash.redwood.schema.Children
import app.cash.redwood.schema.Default
import app.cash.redwood.schema.Modifier
import app.cash.redwood.schema.Property
import app.cash.redwood.schema.Schema
import app.cash.redwood.schema.Schema.Dependency
import app.cash.redwood.schema.Widget
import app.cash.redwood.tooling.schema.Deprecation.Level
import app.cash.redwood.tooling.schema.FqType.Variance.Out
import app.cash.redwood.tooling.schema.ProtocolWidget.ProtocolProperty
import app.cash.redwood.tooling.schema.Widget.Children as ChildrenTrait
Expand All @@ -49,6 +51,7 @@ import com.example.redwood.testapp.TestSchema
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import java.io.File
import kotlin.DeprecationLevel.ERROR
import kotlin.DeprecationLevel.HIDDEN
import kotlin.reflect.KClass
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -785,6 +788,33 @@ class SchemaParserTest(
assertThat(modifier.tag).isEqualTo(-4_543_827)
}

@Schema([DefaultExpressionWidget::class, DefaultExpressionModifier::class])
interface DefaultExpressionSchema

@Widget(1)
data class DefaultExpressionWidget(
@Property(1) @Default("5") val a: Int,
@Children(1) @Default("{}") val b: () -> Unit,
)

@Modifier(1)
data class DefaultExpressionModifier(
@Default("5") val a: Int,
)

@Test fun defaultExpressions() {
val schema = parser.parse(DefaultExpressionSchema::class).schema

val widget = schema.widgets.single()
val property = widget.traits.filterIsInstance<PropertyTrait>().single()
val children = widget.traits.filterIsInstance<ChildrenTrait>().single()
assertThat(property.defaultExpression).isEqualTo("5")
assertThat(children.defaultExpression).isEqualTo("{}")

val modifier = schema.modifiers.single()
assertThat(modifier.properties.single().defaultExpression).isEqualTo("5")
}

@Schema([SomeWidget::class, SomeModifier::class])
interface SchemaTag

Expand Down Expand Up @@ -1238,6 +1268,62 @@ class SchemaParserTest(
assertThat(noProperty.isSerializable).isFalse()
}

@Suppress("DEPRECATION")
@Schema([DeprecatedWidget::class, DeprecatedModifier::class])
interface DeprecationSchema

@Widget(1)
@Deprecated("w")
data class DeprecatedWidget(
@Property(1) @Deprecated("a") val warn: Int,
@Property(2) @Deprecated("b", level = ERROR) val error: Int,
@Children(1) @Deprecated("c") val children: () -> Unit,
)

@Modifier(1)
@Deprecated("m")
data class DeprecatedModifier(
@Deprecated("d") val property: Int,
)

@Test fun deprecation() {
val schema = parser.parse(DeprecationSchema::class).schema

val widget = schema.widgets.single()
assertThat(widget.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.WARNING)
prop(Deprecation::message).isEqualTo("w")
}

val warn = widget.traits.single { it.name == "warn" }
assertThat(warn.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.WARNING)
prop(Deprecation::message).isEqualTo("a")
}
val error = widget.traits.single { it.name == "error" }
assertThat(error.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.ERROR)
prop(Deprecation::message).isEqualTo("b")
}
val children = widget.traits.single { it.name == "children" }
assertThat(children.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.WARNING)
prop(Deprecation::message).isEqualTo("c")
}

val modifier = schema.modifiers.single()
assertThat(modifier.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.WARNING)
prop(Deprecation::message).isEqualTo("m")
}

val property = modifier.properties.single()
assertThat(property.deprecation).isNotNull().all {
prop(Deprecation::level).isEqualTo(Level.WARNING)
prop(Deprecation::message).isEqualTo("d")
}
}

@Schema(
[
DeprecationHiddenWidget::class,
Expand Down