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

Do not use 'header' as a enum value name as it breaks the Kotlin compiler #4086

Merged
merged 4 commits into from May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,14 +10,17 @@ private val JAVA_RESERVED_WORDS = arrayOf(
"transient", "try", "true", "void", "volatile", "while"
)


// Reference:
// https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position
private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal")

fun String.escapeJavaReservedWord() = if (this in JAVA_RESERVED_WORDS) "${this}_" else this

// Does nothing. KotlinPoet will add the backticks
fun String.escapeKotlinReservedWord() = this

fun String.escapeKotlinReservedEnumValueNames() = if (this in KOTLIN_RESERVED_ENUM_VALUE_NAMES) "${this}_" else this
fun String.escapeKotlinReservedEnumValueNames() : String {
return when {
// https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position
this in arrayOf("name", "ordinal") -> "${this}_"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember why we don't use backticks for these two as well? (we wouldn't want to change this now as it would be breaking, but I'm curious if we simply didn't think of it or a good reason).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's different. One is an actual language limitation (name, ordinal). I think because there's a nameclash somewhere. The other is most likely a compiler bug (header, impl).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you want to

"(?:name|ordinal)_*".toRegex().matches(this)

to prevent collisions if a schema defines both name and name_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point, much better 👍

// "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315
this in arrayOf("header", "impl") -> "`${this}`"
else -> this
}
}
1 change: 1 addition & 0 deletions tests/enums/src/main/graphql/operation.graphql
@@ -1,4 +1,5 @@
query GetEnums {
direction
gravity
foo
}
7 changes: 7 additions & 0 deletions tests/enums/src/main/graphql/schema.graphqls
@@ -1,6 +1,7 @@
type Query {
direction: Direction
gravity: Gravity
foo: Foo
}

enum Direction {
Expand Down Expand Up @@ -29,3 +30,9 @@ enum Gravity {
name,
ordinal,
}

# See https://youtrack.jetbrains.com/issue/KT-52315
enum Foo {
header,
footer,
}
6 changes: 6 additions & 0 deletions tests/enums/src/test/kotlin/test/EnumsTest.kt
@@ -1,6 +1,7 @@
package test

import enums.type.Direction
import enums.type.Foo
import enums.type.Gravity
import org.junit.Test
import kotlin.test.assertEquals
Expand All @@ -24,6 +25,11 @@ class EnumsTest {
assertEquals(Gravity.ordinal, Gravity.safeValueOf("ordinal"))
}

@Test
fun headerAndImpl() {
assertEquals(Foo.header.rawValue, "header")
}

@Test
fun sealedClassesKnownValues() {
// Order is important
Expand Down