Skip to content

Commit

Permalink
Work around KT-52315 (#1248)
Browse files Browse the repository at this point in the history
* Work around KT-52315

https://youtrack.jetbrains.com/issue/KT-52315

`header` and `impl` are deprecated and do not behave as modifier
keywords anymore... except in this specific case, seemingly.

* Simpler approach (escape everywhere)

* Rename test function to be more specific

Co-authored-by: Egor Andreevich <github@egorand.dev>

Co-authored-by: Egor Andreevich <github@egorand.dev>
  • Loading branch information
ephemient and Egorand committed May 12, 2022
1 parent 9af3f67 commit bd04961
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt
Expand Up @@ -248,6 +248,10 @@ private val KEYWORDS = setOf(
"value",
"vararg",

// These aren't keywords anymore but still break some code if unescaped. https://youtrack.jetbrains.com/issue/KT-52315
"header",
"impl",

// Other reserved keywords
"yield",
)
Expand Down
37 changes: 37 additions & 0 deletions kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt
Expand Up @@ -4898,6 +4898,43 @@ class TypeSpecTest {
)
}

// https://youtrack.jetbrains.com/issue/KT-52315
@Test fun escapeHeaderAndImplAsEnumConstantNames() {
val primaryConstructor = FunSpec.constructorBuilder()
.addParameter("int", Int::class)
.build()
val enum = TypeSpec
.enumBuilder("MyEnum")
.primaryConstructor(primaryConstructor)
.addEnumConstant(
"header",
TypeSpec.anonymousClassBuilder()
.addSuperclassConstructorParameter("%L", 1)
.build()
)
.addEnumConstant(
"impl",
TypeSpec.anonymousClassBuilder()
.addSuperclassConstructorParameter("%L", 2)
.build()
)
.build()
assertThat(toString(enum)).isEqualTo(
"""
|package com.squareup.tacos
|
|import kotlin.Int
|
|public enum class MyEnum(
| int: Int,
|) {
| `header`(1),
| `impl`(2),
|}
|""".trimMargin()
)
}

@Test fun escapeClassNames() {
val type = TypeSpec.classBuilder("fun").build()
assertThat(type.toString()).isEqualTo(
Expand Down

0 comments on commit bd04961

Please sign in to comment.