diff options
author | Anton Bashirov <anton.sh.local@gmail.com> | 2021-06-21 18:45:21 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-21 11:45:21 -0400 |
commit | 369653caa8b349dbb020b41e266766d8fa0c065e (patch) | |
tree | 9831544bd2cca4e0b1d210598c46d90aa0b39ac8 /kotlinpoet | |
parent | 434812fe703e7397bb0fed6347ba61110a69b648 (diff) | |
download | kotlinpoet-369653caa8b349dbb020b41e266766d8fa0c065e.tar.gz |
Escape members that contains only underscores (#1100)
* Escape members that contains only underscores:
`data class(val `____`: String)`
* remove underscore keyword
* fix failed tests
* add allStringsAreEscaped test
* rename test
Diffstat (limited to 'kotlinpoet')
-rw-r--r-- | kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt | 18 | ||||
-rw-r--r-- | kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt | 37 |
2 files changed, 48 insertions, 7 deletions
diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt index 728f6c4e..d2ba2c7f 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt @@ -251,10 +251,14 @@ private val KEYWORDS = setOf( private const val ALLOWED_CHARACTER = '$' +private const val UNDERSCORE_CHARACTER = '_' + internal val String.isKeyword get() = this in KEYWORDS internal val String.hasAllowedCharacters get() = this.any { it == ALLOWED_CHARACTER } +internal val String.allCharactersAreUnderscore get() = this.all { it == UNDERSCORE_CHARACTER } + // https://github.com/JetBrains/kotlin/blob/master/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JvmSimpleNameBacktickChecker.kt private val ILLEGAL_CHARACTERS_TO_ESCAPE = setOf('.', ';', '[', ']', '/', '<', '>', ':', '\\') @@ -265,13 +269,11 @@ private fun String.failIfEscapeInvalid() { } } -internal fun String.escapeIfNecessary(validate: Boolean = true): String { - val escapedString = escapeIfNotJavaIdentifier().escapeIfKeyword().escapeIfHasAllowedCharacters() - if (validate) { - escapedString.failIfEscapeInvalid() - } - return escapedString -} +internal fun String.escapeIfNecessary(validate: Boolean = true): String = escapeIfNotJavaIdentifier() + .escapeIfKeyword() + .escapeIfHasAllowedCharacters() + .escapeIfAllCharactersAreUnderscore() + .apply { if (validate) failIfEscapeInvalid() } private fun String.alreadyEscaped() = startsWith("`") && endsWith("`") @@ -279,6 +281,8 @@ private fun String.escapeIfKeyword() = if (isKeyword && !alreadyEscaped()) "`$th private fun String.escapeIfHasAllowedCharacters() = if (hasAllowedCharacters && !alreadyEscaped()) "`$this`" else this +private fun String.escapeIfAllCharactersAreUnderscore() = if (allCharactersAreUnderscore && !alreadyEscaped()) "`$this`" else this + private fun String.escapeIfNotJavaIdentifier(): String { return if (!Character.isJavaIdentifierStart(first()) || drop(1).any { !Character.isJavaIdentifierPart(it) } && !alreadyEscaped() diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt index e816d308..be36267c 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt +++ b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt @@ -1223,4 +1223,41 @@ class KotlinPoetTest { |""".trimMargin() ) } + + @Test fun allStringsAreUnderscore() { + val file = FileSpec.builder("com.squareup.tacos", "SourceWithUnderscores") + .addType( + TypeSpec.classBuilder("SourceWithUnderscores") + .primaryConstructor( + FunSpec.constructorBuilder() + .addParameter("_", Float::class) + .addParameter("____", Float::class) + .build() + ) + .addProperty( + PropertySpec.builder("_", Float::class) + .initializer("_") + .build() + ) + .addProperty( + PropertySpec.builder("____", Float::class) + .initializer("____") + .build() + ) + .build() + ) + .build() + assertThat(file.toString()).isEqualTo( + """ + |package com.squareup.tacos + | + |import kotlin.Float + | + |public class SourceWithUnderscores( + | public val `_`: Float, + | public val `____`: Float + |) + |""".trimMargin() + ) + } } |