diff options
author | Egor Andreevich <egor@squareup.com> | 2023-01-30 10:53:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-30 10:53:44 -0500 |
commit | bb73e6b6ee9c95bc8e2607f05948ce8b9f45cec3 (patch) | |
tree | eba071e9bfec1de86164884cb84094361efeee38 | |
parent | 5dc539150d28ed686a5ed866f1909d9eeb92a049 (diff) | |
parent | bd770696abf62b6575d48efe7647908ec75e57e7 (diff) | |
download | kotlinpoet-bb73e6b6ee9c95bc8e2607f05948ce8b9f45cec3.tar.gz |
Merge pull request #1463 from square/egor.230130.ktlint-0.48.2
Upgrade ktlint to 0.48.2
12 files changed, 49 insertions, 39 deletions
diff --git a/.editorconfig b/.editorconfig index dae2b79d..3ac56613 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,7 +7,6 @@ trim_trailing_whitespace = true insert_final_newline = true [*.{kt,kts}] -disabled_rules = filename ij_kotlin_imports_layout = * ij_kotlin_allow_trailing_comma = true ij_kotlin_allow_trailing_comma_on_call_site = true diff --git a/build.gradle.kts b/build.gradle.kts index 8fd0beb7..421de38b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -70,7 +70,9 @@ subprojects { configure<SpotlessExtension> { kotlin { target("**/*.kt") - ktlint(libs.versions.ktlint.get()).editorConfigOverride(readEditorConfig()) + ktlint(libs.versions.ktlint.get()).editorConfigOverride( + mapOf("ktlint_standard_filename" to "disabled"), + ) trimTrailingWhitespace() endWithNewline() @@ -132,10 +134,3 @@ apiValidation { "test-processor" // Test only ) } - -fun readEditorConfig(): Map<String, String> { - val settingRegex = Regex("^(\\S+)\\s*=\\s*(\\S+)$") - return file(".editorconfig").readLines() - .mapNotNull { settingRegex.matchEntire(it)?.destructured } - .associate { (key, value) -> key to value } -} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c66a90d7..523b9bc0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ kotlin = "1.7.22" kct = "1.4.9" ksp = "1.7.22-1.0.8" -ktlint = "0.46.1" +ktlint = "0.48.2" [plugins] kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } diff --git a/interop/javapoet/src/main/kotlin/com/squareup/kotlinpoet/javapoet/j2kInterop.kt b/interop/javapoet/src/main/kotlin/com/squareup/kotlinpoet/javapoet/j2kInterop.kt index 2fba9eed..7cb8fdb4 100644 --- a/interop/javapoet/src/main/kotlin/com/squareup/kotlinpoet/javapoet/j2kInterop.kt +++ b/interop/javapoet/src/main/kotlin/com/squareup/kotlinpoet/javapoet/j2kInterop.kt @@ -90,9 +90,11 @@ public fun JTypeVariableName.toKTypeVariableName(): KTypeVariableName { public fun JWildcardTypeName.toKWildcardTypeName(): KWildcardTypeName { return if (lowerBounds.size == 1) { KWildcardTypeName.consumerOf(lowerBounds.first().toKTypeName()) - } else when (val upperBound = upperBounds[0]) { - TypeName.OBJECT -> STAR - else -> KWildcardTypeName.producerOf(upperBound.toKTypeName()) + } else { + when (val upperBound = upperBounds[0]) { + TypeName.OBJECT -> STAR + else -> KWildcardTypeName.producerOf(upperBound.toKTypeName()) + } } } @@ -133,12 +135,16 @@ public fun JTypeName.toKTypeName(): KTypeName { internal fun JTypeName.unboxIfBoxedPrimitive(): JTypeName { return if (isBoxedPrimitive) { unbox() - } else this + } else { + this + } } @OptIn(KotlinPoetJavaPoetPreview::class) internal fun JTypeName.boxIfPrimitive(extraCondition: Boolean = true): JTypeName { return if (extraCondition && isPrimitive && !isBoxedPrimitive) { box() - } else this + } else { + this + } } diff --git a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmFieldModifier.kt b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmFieldModifier.kt index 37792208..dc2a55bb 100644 --- a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmFieldModifier.kt +++ b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmFieldModifier.kt @@ -36,5 +36,5 @@ public enum class JvmFieldModifier : JvmModifier { override fun annotationSpec(): AnnotationSpec = AnnotationSpec.builder( Volatile::class.asClassName(), ).build() - }; + }, } diff --git a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmMethodModifier.kt b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmMethodModifier.kt index e9e2fe38..5d63738b 100644 --- a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmMethodModifier.kt +++ b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/JvmMethodModifier.kt @@ -32,5 +32,5 @@ public enum class JvmMethodModifier : JvmModifier { Synchronized::class.asClassName(), ).build() }, - DEFAULT + DEFAULT, } diff --git a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt index 1f9e1af2..6c0730a6 100644 --- a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt +++ b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt @@ -365,7 +365,9 @@ private fun KmClass.toTypeSpec( val finalSpec = if (isEnum && spec.annotations.isEmpty()) { // Metadata specifies the constructor as private, but that's implicit so we can omit it spec.toBuilder().apply { modifiers.remove(PRIVATE) }.build() - } else spec + } else { + spec + } builder.primaryConstructor(finalSpec) primaryConstructorParams.putAll(spec.parameters.associateBy { it.name }) } diff --git a/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecsTest.kt b/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecsTest.kt index 5465e941..9198dbcb 100644 --- a/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecsTest.kt +++ b/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecsTest.kt @@ -25,6 +25,7 @@ "UNUSED_PARAMETER", "unused", ) + package com.squareup.kotlinpoet.metadata.specs import com.google.common.truth.Truth.assertThat @@ -579,7 +580,7 @@ class KotlinPoetMetadataSpecsTest : MultiClassInspectorTest() { override fun toString(): String { return "baz1" } - } + }, } @Test @@ -647,7 +648,7 @@ class KotlinPoetMetadataSpecsTest : MultiClassInspectorTest() { override fun toString(): String { return "baz1" } - } + }, } @Test @@ -1917,7 +1918,8 @@ class KotlinPoetMetadataSpecsTest : MultiClassInspectorTest() { annotation class CustomAnnotation(val name: String) class ParameterAnnotations( - @property:CustomAnnotation("\$a") @param:CustomAnnotation("b") + @property:CustomAnnotation("\$a") + @param:CustomAnnotation("b") val param1: String, @CustomAnnotation("2") param2: String, ) { diff --git a/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/MultiClassInspectorTest.kt b/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/MultiClassInspectorTest.kt index 10827fd9..cb7c21d6 100644 --- a/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/MultiClassInspectorTest.kt +++ b/interop/kotlinx-metadata/src/test/kotlin/com/squareup/kotlinpoet/metadata/specs/MultiClassInspectorTest.kt @@ -66,7 +66,8 @@ abstract class MultiClassInspectorTest { override fun create(testInstance: MultiClassInspectorTest): ClassInspector { return ElementsClassInspector.create(testInstance.compilation.elements, testInstance.compilation.types) } - }; + }, + ; abstract fun create(testInstance: MultiClassInspectorTest): ClassInspector } diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt index 46d025d7..2884c65b 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt +++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt @@ -479,9 +479,9 @@ internal class CodeWriter constructor( // Mark the member as importable for a future pass unless the name clashes with // a method in the current context if (!kdoc && ( - memberName.isExtension || - !isMethodNameUsedInCurrentContext(memberName.simpleName) - ) + memberName.isExtension || + !isMethodNameUsedInCurrentContext(memberName.simpleName) + ) ) { importableMember(memberName) } diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt index ad751c54..c8abca97 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt +++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt @@ -427,7 +427,8 @@ public class TypeSpec private constructor( ) { CLASS("class", setOf(PUBLIC), setOf(PUBLIC), setOf()), OBJECT("object", setOf(PUBLIC), setOf(PUBLIC), setOf()), - INTERFACE("interface", setOf(PUBLIC, ABSTRACT), setOf(PUBLIC, ABSTRACT), setOf()); + INTERFACE("interface", setOf(PUBLIC, ABSTRACT), setOf(PUBLIC, ABSTRACT), setOf()), + ; internal fun implicitPropertyModifiers(modifiers: Set<KModifier>): Set<KModifier> { return defaultImplicitPropertyModifiers + when { diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt index 8236e5d5..16a443dd 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt @@ -138,18 +138,22 @@ internal fun stringLiteralWithQuotes( } } -internal fun CodeBlock.ensureEndsWithNewLine() = if (isEmpty()) this else with(toBuilder()) { - val lastFormatPart = trim().formatParts.last() - if (lastFormatPart.isPlaceholder && args.isNotEmpty()) { - val lastArg = args.last() - if (lastArg is String) { - args[args.size - 1] = lastArg.trimEnd('\n') + '\n' +internal fun CodeBlock.ensureEndsWithNewLine() = if (isEmpty()) { + this +} else { + with(toBuilder()) { + val lastFormatPart = trim().formatParts.last() + if (lastFormatPart.isPlaceholder && args.isNotEmpty()) { + val lastArg = args.last() + if (lastArg is String) { + args[args.size - 1] = lastArg.trimEnd('\n') + '\n' + } + } else { + formatParts[formatParts.lastIndexOf(lastFormatPart)] = lastFormatPart.trimEnd('\n') + formatParts += "\n" } - } else { - formatParts[formatParts.lastIndexOf(lastFormatPart)] = lastFormatPart.trimEnd('\n') - formatParts += "\n" + return@with build() } - return@with build() } private val IDENTIFIER_REGEX = @@ -291,9 +295,9 @@ private fun String.escapeIfAllCharactersAreUnderscore() = if (allCharactersAreUn private fun String.escapeIfNotJavaIdentifier(): String { return if (( - !Character.isJavaIdentifierStart(first()) || - drop(1).any { !Character.isJavaIdentifierPart(it) } - ) && + !Character.isJavaIdentifierStart(first()) || + drop(1).any { !Character.isJavaIdentifierPart(it) } + ) && !alreadyEscaped() ) { "`$this`".replace(' ', 'ยท') |