Skip to content

Commit

Permalink
Update kotlinpoet to 2.0.0 (#6215)
Browse files Browse the repository at this point in the history
* update KotlinPoet

* update test fixtures
  • Loading branch information
martinbonnin authored Oct 23, 2024
1 parent 88748d0 commit 5736f7c
Show file tree
Hide file tree
Showing 629 changed files with 2,575 additions and 4,175 deletions.
2 changes: 1 addition & 1 deletion gradle/libraries.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ moshi = { group = "com.squareup.moshi", name = "moshi", version = "1.14.0" }
okio = { group = "com.squareup.okio", name = "okio", version.ref = "okio" }
okio-nodefilesystem = { group = "com.squareup.okio", name = "okio-nodefilesystem", version.ref = "okio" }
poet-java = { group = "com.squareup", name = "javapoet", version.ref = "javaPoet" }
poet-kotlin = { group = "com.squareup", name = "kotlinpoet", version = "1.14.2" }
poet-kotlin = { group = "com.squareup", name = "kotlinpoet", version = "2.0.0" }
rx-java2 = { group = "io.reactivex.rxjava2", name = "rxjava", version.ref = "rx-java2" }
rx-java3 = { group = "io.reactivex.rxjava3", name = "rxjava", version.ref = "rx-java3" }
sqldelight-android = { group = "app.cash.sqldelight", name = "android-driver", version.ref = "sqldelight" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal fun readFromResponseCodeBlock(
}

CodeBlock.of(
"var·%N:·%T·=·%L",
"var %N: %T = %L",
property.info.responseName.variableName(),
context.resolver.resolveIrType(property.info.type, context.jsExport).copy(nullable = !property.info.type.optional),
variableInitializer
Expand All @@ -66,14 +66,14 @@ internal fun readFromResponseCodeBlock(
*/
val loop = if (regularProperties.isNotEmpty()) {
CodeBlock.builder()
.beginControlFlow("while·(true)")
.beginControlFlow("when·($reader.selectName($RESPONSE_NAMES))")
.beginControlFlow("while (true)")
.beginControlFlow("when ($reader.selectName($RESPONSE_NAMES))")
.add(
regularProperties.mapIndexed { index, property ->
val variableName = property.info.responseName.variableName()
val adapterInitializer = context.resolver.adapterInitializer(property.info.type, property.requiresBuffering, context.jsExport)
CodeBlock.of(
"%L·->·%N·=·%L.$fromJson($reader,·${customScalarAdapters})",
"%L -> %N = %L.$fromJson($reader, ${customScalarAdapters})",
index,
variableName,
adapterInitializer,
Expand Down Expand Up @@ -104,7 +104,7 @@ internal fun readFromResponseCodeBlock(
add("$reader.rewind()\n")
add(typenameFromReaderCodeBlock())
} else {
beginControlFlow("check($__typename·!=·null)")
beginControlFlow("check($__typename != null)")
add("%S\n", "__typename was not found")
endControlFlow()
}
Expand All @@ -119,7 +119,7 @@ internal fun readFromResponseCodeBlock(
.apply {
if (property.condition != BooleanExpression.True) {
add(
"var·%N:·%T·=·null\n",
"var %N: %T = null\n",
property.info.responseName.variableName(),
context.resolver.resolveIrType(property.info.type, context.jsExport).copy(nullable = !property.info.type.optional),
)
Expand All @@ -133,17 +133,17 @@ internal fun readFromResponseCodeBlock(
} else {
"null"
}
beginControlFlow("if·(%L.%M($customScalarAdapters.falseVariables,·$typenameLiteral,·$customScalarAdapters.deferredFragmentIdentifiers,·$pathLiteral))", property.condition.codeBlock(), evaluate)
beginControlFlow("if (%L.%M($customScalarAdapters.falseVariables, $typenameLiteral, $customScalarAdapters.deferredFragmentIdentifiers, $pathLiteral))", property.condition.codeBlock(), evaluate)
add("$reader.rewind()\n")
} else {
checkedProperties.add(property.info.responseName)
add("$reader.rewind()\n")
add("val·")
add("val ")
}
}
.add(
CodeBlock.of(
"%N·=·%L.$fromJson($reader, $customScalarAdapters)\n",
"%N = %L.$fromJson($reader, $customScalarAdapters)\n",
property.info.responseName.variableName(),
context.resolver.resolveModelAdapter(property.info.type.modelPath()),
)
Expand All @@ -155,20 +155,20 @@ internal fun readFromResponseCodeBlock(
}.joinToCode("\n")

val suffix = CodeBlock.builder()
.addStatement("return·%T(", context.resolver.resolveModel(model.id))
.addStatement("return %T(", context.resolver.resolveModel(model.id))
.indent()
.add(model.properties.map { property ->
val maybeAssertNotNull = if (
(property.info.type.catchTo == IrCatchTo.Result || !property.info.type.nullable)
&& !property.info.type.optional
&& !checkedProperties.contains(property.info.responseName)
) {
CodeBlock.of("·?:·%M(reader,·%S)", KotlinSymbols.missingField, property.info.responseName)
CodeBlock.of(" ?: %M(reader, %S)", KotlinSymbols.missingField, property.info.responseName)
} else {
CodeBlock.of("")
}
CodeBlock.of(
"%N·=·%N%L",
"%N = %N%L",
context.layout.propertyName(property.info.responseName),
property.info.responseName.variableName(),
maybeAssertNotNull
Expand Down Expand Up @@ -229,7 +229,7 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
* Output types do not distinguish between null and absent
*/
if (this.info.type.nullable) {
builder.beginControlFlow("if·($value.%N·!=·null)", propertyName)
builder.beginControlFlow("if ($value.%N != null)", propertyName)
}
builder.addStatement(
"%L.$toJson($writer, $customScalarAdapters, $value.%N)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal fun TypeSpec.Builder.withEqualsImplementation(className: ClassName): Ty
fun methodCode(): CodeBlock {
if (propertySpecs.isEmpty()) {
return CodeBlock.builder()
.addStatement("return·other != null·&&·other::class·==·this::class")
.addStatement("return other != null && other::class == this::class")
.build()
}
return CodeBlock.builder()
Expand Down Expand Up @@ -183,7 +183,7 @@ internal fun TypeSpec.Builder.withHashCodeImplementation(): TypeSpec.Builder = a

fun methodCode(): CodeBlock {
if (propertySpecs.isEmpty()) {
return CodeBlock.builder().addStatement("return·this::class.hashCode()").build()
return CodeBlock.builder().addStatement("return this::class.hashCode()").build()
}
return CodeBlock.builder()
.beginControlFlow("if (%L == null)", MEMOIZED_HASH_CODE_VAR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ internal fun TypeSpec.Builder.maybeAddDescription(description: String?): TypeSpe
return this
}

return addKdoc("%L", description)
return addKdoc("%L", description.replace(' ', ''))
}

internal fun PropertySpec.Builder.maybeAddDescription(description: String?): PropertySpec.Builder {
if (description.isNullOrBlank()) {
return this
}

return addKdoc("%L", description)
return addKdoc("%L", description.replace(' ', ''))
}

internal fun ParameterSpec.Builder.maybeAddDescription(description: String?): ParameterSpec.Builder {
if (description.isNullOrBlank()) {
return this
}

return addKdoc("%L", description)
return addKdoc("%L", description.replace(' ', ''))
}

internal fun FunSpec.Builder.maybeAddDescription(description: String?): FunSpec.Builder {
if (description.isNullOrBlank()) {
return this
}

return addKdoc("%L", description)
return addKdoc("%L", description.replace(' ', ''))
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ internal fun NamedType.toSetterFunSpec(context: KotlinContext): FunSpec {
val body = CodeBlock.builder()
val parameterType: IrType
if (type.optional) {
body.add("this.%N·=·%T(%N)\n", propertyName, KotlinSymbols.Present, propertyName)
body.add("this.%N = %T(%N)\n", propertyName, KotlinSymbols.Present, propertyName)
parameterType = type.optional(false)
} else {
body.add("this.%N·=·%N\n", propertyName, propertyName)
body.add("this.%N = %N\n", propertyName, propertyName)
parameterType = type
}
body.add("return this")
Expand Down Expand Up @@ -124,14 +124,14 @@ private fun List<NamedType>.toBuildFunSpec(context: KotlinContext, returnedClass
.returns(returnedClassName)
.addCode(
CodeBlock.builder()
.add("return·%T(\n", returnedClassName)
.add("return %T(\n", returnedClassName)
.indent()
.apply {
forEach {
val propertyName = context.layout.propertyName(it.graphQlName)
add("%N·=·%N", propertyName, propertyName)
add("%N = %N", propertyName, propertyName)
if (!it.type.nullable && !it.type.optional) {
add("·?:·error(\"missing·value·for·$propertyName\")")
add(" ?: error(\"missing value for $propertyName\")")
}
add(",\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal fun TypeSpec.patchKotlinNativeOptionalArrayProperties(): TypeSpec {
FunSpec
.builder("${propertySpec.name}FilterNotNull")
.returns(nonOptionalListType)
.addStatement("return·%N%L.filterNotNull()", propertySpec.name, if (propertySpec.type.isNullable) "?" else "")
.addStatement("return %N%L.filterNotNull()", propertySpec.name, if (propertySpec.type.isNullable) "?" else "")
.build()
}
return toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal fun dataBuilderCtor(
)
.addCode(
CodeBlock.builder()
.add("return·%M(\n", KotlinSymbols.buildData)
.add("return %M(\n", KotlinSymbols.buildData)
.indent()
.apply {
if (builderFactoryParameterRequired) {
Expand Down Expand Up @@ -141,10 +141,10 @@ internal fun topLevelBuildFunSpec(
.receiver(KotlinSymbols.BuilderScope)
.addCode(
CodeBlock.builder()
.add("return·%T(${customScalarAdapters}).apply($block)", builderClassName)
.add("return %T(${customScalarAdapters}).apply($block)", builderClassName)
.apply {
if (requiresTypename) {
add(".apply·{·__typename·=·typename·}")
add(".apply { __typename = typename }")
}
}
.add(".build()")
Expand Down Expand Up @@ -214,7 +214,7 @@ internal fun concreteBuilderTypeSpec(
.addFunction(buildFunSpec(packageName, mapName))
.apply {
if (typename != null) {
addInitializerBlock(CodeBlock.of("$__typename·=·%S", typename))
addInitializerBlock(CodeBlock.of("$__typename = %S", typename))
}
}
.build()
Expand Down Expand Up @@ -242,7 +242,7 @@ private fun buildFunSpec(packageName: String, mapName: String): FunSpec {
.addModifiers(KModifier.OVERRIDE)
.addCode(
CodeBlock.builder()
.addStatement("return·%T(${Identifier.__fields})", mapClassName)
.addStatement("return %T(${Identifier.__fields})", mapClassName)
.build()
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ internal class CompiledSelectionsBuilder(
val builder = CodeBlock.builder()
builder.add("%T(\n", KotlinSymbols.CompiledFieldBuilder)
builder.indent()
builder.add("name·=·%S,\n", name)
builder.add("name = %S,\n", name)
builder.add(
CodeBlock.of("type·=·%L\n", type.codeBlock(context))
CodeBlock.of("type = %L\n", type.codeBlock(context))
)
builder.unindent()
builder.add(")")
Expand All @@ -77,8 +77,8 @@ internal class CompiledSelectionsBuilder(
val builder = CodeBlock.builder()
builder.add("%T(\n", KotlinSymbols.CompiledFragmentBuilder)
builder.indent()
builder.add("typeCondition·=·%S,\n", typeCondition)
builder.add("possibleTypes·=·%L\n", possibleTypes.map { CodeBlock.of("%S", it) }.toListInitializerCodeblock(false))
builder.add("typeCondition = %S,\n", typeCondition)
builder.add("possibleTypes = %L\n", possibleTypes.map { CodeBlock.of("%S", it) }.toListInitializerCodeblock(false))
builder.unindent()
builder.add(")")

Expand Down Expand Up @@ -119,7 +119,7 @@ internal class CompiledSelectionsBuilder(

check(expression is BooleanExpression.Element)

return CodeBlock.of("%T(%S,·%L)", KotlinSymbols.CompiledCondition, expression.value.name, inverted.toString())
return CodeBlock.of("%T(%S, %L)", KotlinSymbols.CompiledCondition, expression.value.name, inverted.toString())
}

private fun IrArgument.codeBlock(): CodeBlock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,25 @@ internal class OperationBuilder(
private fun operationIdFunSpec() = FunSpec.builder(Identifier.id)
.addModifiers(KModifier.OVERRIDE)
.returns(KotlinSymbols.String)
.addStatement("return·${Identifier.OPERATION_ID}")
.addStatement("return ${Identifier.OPERATION_ID}")
.build()

private fun queryDocumentFunSpec(generateQueryDocument: Boolean) = FunSpec.builder(Identifier.document)
.addModifiers(KModifier.OVERRIDE)
.returns(KotlinSymbols.String)
.apply {
if (generateQueryDocument) {
addStatement("return·${Identifier.OPERATION_DOCUMENT}")
addStatement("return ${Identifier.OPERATION_DOCUMENT}")
} else {
addStatement("error(\"The·query·document·was·removed·from·this·operation.·Use·generateQueryDocument.set(true)·if·you·need·it\")")
addStatement("error(\"The query document was removed from this operation. Use generateQueryDocument.set(true) if you need it\")")
}
}
.build()

private fun nameFunSpec() = FunSpec.builder(Identifier.name)
.addModifiers(KModifier.OVERRIDE)
.returns(KotlinSymbols.String)
.addStatement("return·OPERATION_NAME")
.addStatement("return OPERATION_NAME")
.build()

private fun companionTypeSpec(): TypeSpec {
Expand All @@ -175,7 +175,7 @@ internal class OperationBuilder(
.applyIf(generateQueryDocument) {
addProperty(PropertySpec.builder(Identifier.OPERATION_DOCUMENT, KotlinSymbols.String)
.getter(FunSpec.getterBuilder()
.addStatement("return·%S", QueryDocumentMinifier.minify(operation.sourceWithFragments))
.addStatement("return %S", QueryDocumentMinifier.minify(operation.sourceWithFragments))
.build()
)
.addKdoc("%L", """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal fun adapterFunSpec(
.returns(KotlinSymbols.Adapter.parameterizedBy(context.resolver.resolveIrType(type, context.jsExport)))
.addCode(
CodeBlock.of(
"return·%L",
"return %L",
context.resolver.adapterInitializer(type, property.requiresBuffering, context.jsExport)
)
)
Expand All @@ -69,13 +69,13 @@ internal fun rootFieldFunSpec(context: KotlinOperationsContext, parentType: Stri
.returns(KotlinSymbols.CompiledField)
.addCode(
CodeBlock.builder()
.add("return·%T(\n", KotlinSymbols.CompiledFieldBuilder)
.add("return %T(\n", KotlinSymbols.CompiledFieldBuilder)
.indent()
.add("name·=·%S,\n", Identifier.data)
.add("type·=·%L\n", context.resolver.resolveCompiledType(parentType))
.add("name = %S,\n", Identifier.data)
.add("type = %L\n", context.resolver.resolveCompiledType(parentType))
.unindent()
.add(")\n")
.add(".$selections(selections·=·%T.$root)\n", selectionsClassName)
.add(".$selections(selections = %T.$root)\n", selectionsClassName)
.add(".build()\n")
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal class PolymorphicFieldResponseAdapterBuilder(

builder.add(typenameFromReaderCodeBlock())

builder.beginControlFlow("return·when($__typename) {")
builder.beginControlFlow("return when($__typename) {")
implementations.sortedByDescending {
it.typeSet.size
}.sortedByDescending {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal class EnumAsEnumBuilder(
private fun IrEnum.knownValuesFunSpec(): FunSpec {
return FunSpec.builder(Identifier.knownValues)
.addKdoc("Returns all [%T] known at compile time", selfClassName)
.addAnnotation(deprecatedAnnotation("Use knownEntries instead").toBuilder().addMember("replaceWith·=·ReplaceWith(%S)", "this.knownEntries").build())
.addAnnotation(deprecatedAnnotation("Use knownEntries instead").toBuilder().addMember("replaceWith = ReplaceWith(%S)", "this.knownEntries").build())
.returns(KotlinSymbols.Array.parameterizedBy(selfClassName))
.addCode("return %N.toTypedArray()", Identifier.knownEntries)
.build()
Expand All @@ -103,7 +103,7 @@ internal class EnumAsEnumBuilder(
.getter(
FunSpec.getterBuilder()
.addCode(CodeBlock.builder()
.add("return·listOf(\n")
.add("return listOf(\n")
.indent()
.add(
values.map {
Expand Down Expand Up @@ -131,7 +131,7 @@ internal class EnumAsEnumBuilder(
.addParameter("rawValue", String::
class)
.returns(selfClassName)
.addCode("return·$entries.find·{·it.rawValue·==·rawValue·} ?: ")
.addCode("return $entries.find { it.rawValue == rawValue } ?: ")
.apply {
if (withUnknown) {
addCode(Identifier.UNKNOWN__)
Expand Down
Loading

0 comments on commit 5736f7c

Please sign in to comment.