Skip to content

Commit

Permalink
Suppress deprecation warnings in generated code (#1701)
Browse files Browse the repository at this point in the history
Our generated code calls deprecated APIs, either because the Redwood
generated interfaces they're calling are deprecated, or because other
external symbols like property types are deprecated.

In either case the developer is already getting these deprecation
warnings in the code that declares the schema, so Redwood's additional
deprecations are just noise.

This PR applies deprecation warnings everywhere. I originally
looked into doing it precisely for the Redwood symbols that
declare deprecations, but this doesn't cover deprecations on
property types.
  • Loading branch information
squarejesse authored Nov 24, 2023
1 parent 84fadb3 commit 37e4609
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 158 deletions.
1 change: 1 addition & 0 deletions redwood-tooling-codegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies {
testImplementation projects.testApp.schema.compose
testImplementation libs.junit
testImplementation libs.assertk
testImplementation libs.testParameterInjector
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import app.cash.redwood.tooling.codegen.CodegenType.Modifiers
import app.cash.redwood.tooling.codegen.CodegenType.Testing
import app.cash.redwood.tooling.codegen.CodegenType.Widget
import app.cash.redwood.tooling.schema.SchemaSet
import com.squareup.kotlinpoet.FileSpec
import java.nio.file.Path

public enum class CodegenType {
Expand All @@ -30,34 +31,42 @@ public enum class CodegenType {
}

public fun SchemaSet.generate(type: CodegenType, destination: Path) {
when (type) {
Compose -> {
generateModifierImpls(schema)?.writeTo(destination)
for (scope in schema.scopes) {
generateScope(schema, scope).writeTo(destination)
for (fileSpec in generateFileSpecs(type)) {
fileSpec.writeTo(destination)
}
}

internal fun SchemaSet.generateFileSpecs(type: CodegenType): List<FileSpec> {
return buildList {
when (type) {
Compose -> {
generateModifierImpls(schema)?.let { add(it) }
for (scope in schema.scopes) {
add(generateScope(schema, scope))
}
for (widget in schema.widgets) {
add(generateComposable(schema, widget))
}
}
for (widget in schema.widgets) {
generateComposable(schema, widget).writeTo(destination)
Modifiers -> {
for (modifier in schema.modifiers) {
add(generateModifierInterface(schema, modifier))
}
}
}
Modifiers -> {
for (modifier in schema.modifiers) {
generateModifierInterface(schema, modifier).writeTo(destination)
Testing -> {
add(generateTester(this@generateFileSpecs))
add(generateMutableWidgetFactory(schema))
for (widget in schema.widgets) {
add(generateMutableWidget(schema, widget))
add(generateWidgetValue(schema, widget))
}
}
}
Testing -> {
generateTester(this).writeTo(destination)
generateMutableWidgetFactory(schema).writeTo(destination)
for (widget in schema.widgets) {
generateMutableWidget(schema, widget).writeTo(destination)
generateWidgetValue(schema, widget).writeTo(destination)
}
}
Widget -> {
generateWidgetFactories(this).writeTo(destination)
generateWidgetFactory(schema).writeTo(destination)
for (widget in schema.widgets) {
generateWidget(schema, widget).writeTo(destination)
Widget -> {
add(generateWidgetFactories(this@generateFileSpecs))
add(generateWidgetFactory(schema))
for (widget in schema.widgets) {
add(generateWidget(schema, widget))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ internal fun generateComposable(
val widgetType = schema.widgetType(widget).parameterizedBy(STAR)
val flatName = widget.type.flatName
return FileSpec.builder(schema.composePackage(), flatName)
.addAnnotation(suppressDeprecations)
.addFunction(
FunSpec.builder(flatName)
.addAnnotation(ComposeRuntime.Composable)
Expand Down Expand Up @@ -209,6 +210,7 @@ internal fun generateScope(schema: Schema, scope: FqType): FileSpec {
val scopeName = scope.flatName
val scopeType = ClassName(schema.composePackage(), scopeName)
return FileSpec.builder(scopeType)
.addAnnotation(suppressDeprecations)
.apply {
val scopeBuilder = TypeSpec.interfaceBuilder(scopeType)
.addAnnotation(Redwood.LayoutScopeMarker)
Expand Down Expand Up @@ -243,6 +245,7 @@ internal fun generateModifierImpls(schema: Schema): FileSpec? {
if (schema.modifiers.isEmpty()) return null

return FileSpec.builder(schema.composePackage(), "modifier")
.addAnnotation(suppressDeprecations)
.apply {
for (modifier in schema.modifiers) {
addType(generateModifierImpl(schema, modifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.squareup.kotlinpoet.TypeSpec
internal fun generateModifierInterface(schema: Schema, modifier: Modifier): FileSpec {
val type = schema.modifierType(modifier)
return FileSpec.builder(type.packageName, type.simpleName)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.interfaceBuilder(type)
.addSuperinterface(Redwood.ModifierElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package app.cash.redwood.tooling.codegen
import app.cash.redwood.tooling.codegen.ProtocolCodegenType.Compose
import app.cash.redwood.tooling.codegen.ProtocolCodegenType.Widget
import app.cash.redwood.tooling.schema.ProtocolSchemaSet
import com.squareup.kotlinpoet.FileSpec
import java.nio.file.Path

public enum class ProtocolCodegenType {
Expand All @@ -26,24 +27,32 @@ public enum class ProtocolCodegenType {
}

public fun ProtocolSchemaSet.generate(type: ProtocolCodegenType, destination: Path) {
when (type) {
Compose -> {
generateProtocolBridge(this).writeTo(destination)
generateComposeProtocolModifierSerialization(this).writeTo(destination)
for (dependency in all) {
generateProtocolWidgetFactory(dependency, host = schema).writeTo(destination)
generateProtocolModifierSerializers(dependency, host = schema)?.writeTo(destination)
for (widget in dependency.widgets) {
generateProtocolWidget(dependency, widget, host = schema).writeTo(destination)
for (fileSpec in generateFileSpecs(type)) {
fileSpec.writeTo(destination)
}
}

internal fun ProtocolSchemaSet.generateFileSpecs(type: ProtocolCodegenType): List<FileSpec> {
return buildList {
when (type) {
Compose -> {
add(generateProtocolBridge(this@generateFileSpecs))
add(generateComposeProtocolModifierSerialization(this@generateFileSpecs))
for (dependency in all) {
add(generateProtocolWidgetFactory(dependency, host = schema))
generateProtocolModifierSerializers(dependency, host = schema)?.let { add(it) }
for (widget in dependency.widgets) {
add(generateProtocolWidget(dependency, widget, host = schema))
}
}
}
}
Widget -> {
generateProtocolFactory(this).writeTo(destination)
for (dependency in all) {
generateProtocolModifierImpls(dependency, host = schema)?.writeTo(destination)
for (widget in dependency.widgets) {
generateProtocolNode(dependency, widget, host = schema).writeTo(destination)
Widget -> {
add(generateProtocolFactory(this@generateFileSpecs))
for (dependency in all) {
generateProtocolModifierImpls(dependency, host = schema)?.let { add(it) }
for (widget in dependency.widgets) {
add(generateProtocolNode(dependency, widget, host = schema))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ internal fun generateProtocolBridge(
val type = schema.protocolBridgeType()
val providerType = schema.getWidgetFactoryProviderType().parameterizedBy(NOTHING)
return FileSpec.builder(type)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(type)
.addSuperinterface(ProtocolGuest.ProtocolBridge)
Expand Down Expand Up @@ -195,6 +196,7 @@ internal fun generateProtocolWidgetFactory(
): FileSpec {
val type = schema.protocolWidgetFactoryType(host)
return FileSpec.builder(type)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(type)
.addModifiers(INTERNAL)
Expand Down Expand Up @@ -293,6 +295,7 @@ internal fun generateProtocolWidget(
val type = schema.protocolWidgetType(widget, host)
val widgetName = schema.widgetType(widget)
return FileSpec.builder(type)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(type)
.addModifiers(INTERNAL)
Expand Down Expand Up @@ -512,6 +515,7 @@ internal fun generateProtocolModifierSerializers(
return null
}
return FileSpec.builder(schema.composePackage(host), "modifierSerializers")
.addAnnotation(suppressDeprecations)
.apply {
for (modifier in serializableModifiers) {
val serializerType = schema.modifierSerializer(modifier, host)
Expand Down Expand Up @@ -691,6 +695,7 @@ internal fun generateComposeProtocolModifierSerialization(
val schema = schemaSet.schema
val name = schema.modifierToProtocol.simpleName
return FileSpec.builder(schema.composePackage(), "modifierSerialization")
.addAnnotation(suppressDeprecations)
.addFunction(
FunSpec.builder(name)
.addModifiers(INTERNAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ internal fun Deprecation.toAnnotationSpec(): AnnotationSpec {
.build()
}

internal val suppressDeprecations = AnnotationSpec.builder(Suppress::class)
.useSiteTarget(AnnotationSpec.UseSiteTarget.FILE)
.addMember("%S", "DEPRECATION")
.build()

private fun Deprecation.Level.toMemberName(): MemberName {
return MemberName(
DeprecationLevel::class.asClassName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ internal fun generateTester(schemaSet: SchemaSet): FileSpec {
returnType = typeVarR,
).copy(suspending = true)
return FileSpec.builder(testerFunction.packageName, testerFunction.simpleName)
.addAnnotation(suppressDeprecations)
.addFunction(
FunSpec.builder(testerFunction)
.optIn(Redwood.RedwoodCodegenApi)
Expand Down Expand Up @@ -127,6 +128,7 @@ public class EmojiSearchTestingWidgetFactory : EmojiSearchWidgetFactory<WidgetVa
internal fun generateMutableWidgetFactory(schema: Schema): FileSpec {
val mutableWidgetFactoryType = schema.getTestingWidgetFactoryType()
return FileSpec.builder(mutableWidgetFactoryType)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(mutableWidgetFactoryType)
.addSuperinterface(schema.getWidgetFactoryType().parameterizedBy(RedwoodTesting.WidgetValue))
Expand Down Expand Up @@ -171,6 +173,7 @@ internal fun generateMutableWidget(schema: Schema, widget: Widget): FileSpec {
val mutableWidgetType = schema.mutableWidgetType(widget)
val widgetValueType = schema.widgetValueType(widget)
return FileSpec.builder(mutableWidgetType)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(mutableWidgetType)
.addModifiers(INTERNAL)
Expand Down Expand Up @@ -360,6 +363,7 @@ internal fun generateWidgetValue(schema: Schema, widget: Widget): FileSpec {
}

return FileSpec.builder(widgetValueType)
.addAnnotation(suppressDeprecations)
.addType(
classBuilder
.primaryConstructor(constructorBuilder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal fun generateWidgetFactories(schemaSet: SchemaSet): FileSpec {
val schema = schemaSet.schema
val widgetFactoriesType = schema.getWidgetFactoriesType()
return FileSpec.builder(widgetFactoriesType)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(widgetFactoriesType)
.addTypeVariable(typeVariableW)
Expand Down Expand Up @@ -102,6 +103,7 @@ interface ExampleWidgetFactory<W : Any> : Widget.Factory<W> {
internal fun generateWidgetFactory(schema: Schema): FileSpec {
val widgetFactoryType = schema.getWidgetFactoryType()
return FileSpec.builder(widgetFactoryType)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.interfaceBuilder(widgetFactoryType)
.addTypeVariable(typeVariableW)
Expand Down Expand Up @@ -157,6 +159,7 @@ interface Button<W: Any> : Widget<W> {
internal fun generateWidget(schema: Schema, widget: Widget): FileSpec {
val flatName = widget.type.flatName
return FileSpec.builder(schema.widgetPackage(), flatName)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.interfaceBuilder(flatName)
.addTypeVariable(typeVariableW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ internal fun generateProtocolFactory(
val provider = schema.getWidgetFactoryProviderType().parameterizedBy(typeVariableW)
val type = schema.protocolFactoryType()
return FileSpec.builder(type)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(type)
.addTypeVariable(typeVariableW)
Expand Down Expand Up @@ -225,6 +226,7 @@ internal fun generateProtocolNode(
val protocolType = WidgetProtocol.ProtocolNode.parameterizedBy(typeVariableW)
val (childrens, properties) = widget.traits.partition { it is ProtocolChildren }
return FileSpec.builder(type)
.addAnnotation(suppressDeprecations)
.addType(
TypeSpec.classBuilder(type)
.addModifiers(INTERNAL)
Expand Down Expand Up @@ -413,6 +415,7 @@ internal fun generateProtocolModifierImpls(
return null
}
return FileSpec.builder(schema.widgetPackage(host), "modifierImpls")
.addAnnotation(suppressDeprecations)
.apply {
for (modifier in schema.modifiers) {
val typeName = ClassName(schema.widgetPackage(host), modifier.type.flatName + "Impl")
Expand Down
Loading

0 comments on commit 37e4609

Please sign in to comment.