From 3f64cb189ef01f5f97b6ee3c492229806e422490 Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:18:41 +1100 Subject: [PATCH] Workaround ordering issue with supertypes in Kotlin scripts https://youtrack.jetbrains.com/issue/KT-64534 --- .../detekt/formatting/WrappingSpec.kt | 11 ++++----- .../detekt/rules/bugs/DeprecationSpec.kt | 8 +++---- .../exceptions/ObjectExtendsThrowableSpec.kt | 10 ++++---- .../naming/ConstructorParameterNamingSpec.kt | 4 ++-- .../rules/naming/FunctionMinNameLengthSpec.kt | 2 +- .../rules/naming/FunctionNameMaxLengthSpec.kt | 2 +- .../detekt/rules/naming/FunctionNamingSpec.kt | 10 ++++---- .../naming/FunctionParameterNamingSpec.kt | 2 +- .../naming/MemberNameEqualsClassNameSpec.kt | 24 +++++++++---------- .../rules/naming/VariableMaxLengthSpec.kt | 8 +++---- .../rules/naming/VariableMinLengthSpec.kt | 8 +++---- .../detekt/rules/naming/VariableNamingSpec.kt | 8 +++---- .../AbstractClassCanBeConcreteClassSpec.kt | 7 +++--- .../style/AbstractClassCanBeInterfaceSpec.kt | 7 +++--- 14 files changed, 53 insertions(+), 58 deletions(-) diff --git a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/WrappingSpec.kt b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/WrappingSpec.kt index ba0cd28764b..7afd107cb4d 100644 --- a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/WrappingSpec.kt +++ b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/WrappingSpec.kt @@ -19,19 +19,18 @@ class WrappingSpec { @Test fun `Given a wrong wrapping in the class definition`() { val code = """ - class A() : B, - C { - } - interface B interface C + class A() : B, + C { + } """.trimIndent() assertThat(subject.compileAndLint(code)) .hasSize(1) - .hasStartSourceLocation(1, 12) - .hasTextLocations(11 to 12) + .hasStartSourceLocation(5, 12) + .hasTextLocations(37 to 38) } } diff --git a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DeprecationSpec.kt b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DeprecationSpec.kt index a54ba889724..1ba832097e7 100644 --- a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DeprecationSpec.kt +++ b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DeprecationSpec.kt @@ -35,16 +35,16 @@ class DeprecationSpec(private val env: KotlinCoreEnvironment) { @Test fun `does not report when supertype is not deprecated`() { val code = """ - abstract class Oof : Foo() { - fun spam() { - } - } abstract class Foo { abstract fun bar() : Int fun baz() { } } + abstract class Oof : Foo() { + fun spam() { + } + } """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } diff --git a/detekt-rules-exceptions/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/exceptions/ObjectExtendsThrowableSpec.kt b/detekt-rules-exceptions/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/exceptions/ObjectExtendsThrowableSpec.kt index cee0e9fa9bb..201fb2ab5a3 100644 --- a/detekt-rules-exceptions/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/exceptions/ObjectExtendsThrowableSpec.kt +++ b/detekt-rules-exceptions/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/exceptions/ObjectExtendsThrowableSpec.kt @@ -38,9 +38,9 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) { @Test fun `reports object that extends custom exception`() { val code = """ - object ObjectCustomException : CustomException("singleton custom exception") - open class CustomException(message: String) : RuntimeException(message) + + object ObjectCustomException : CustomException("singleton custom exception") """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } @@ -79,6 +79,7 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) { fun `does not report objects that do not extend Throwable`() { val code = """ object BanException + open class CustomException(message: String) object AuthException : CustomException(message = "Authentication failed!") sealed class DomainException { @@ -86,8 +87,6 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) { object Exception2 : DomainException() object Exception3 : DomainException() } - - open class CustomException(message: String) """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } @@ -117,6 +116,7 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) { data class AuthException(val code: Int) : RuntimeException() class ReportedException : Exception() class FatalException : Error() + open class CustomException(message: String) class ObjectCustomException : CustomException("singleton custom exception") sealed class DomainException : RuntimeException() { @@ -124,8 +124,6 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) { class Exception2 : DomainException() class Exception3 : DomainException() } - - open class CustomException(message: String) """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/ConstructorParameterNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/ConstructorParameterNamingSpec.kt index 311bb9ed403..68fca7c8170 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/ConstructorParameterNamingSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/ConstructorParameterNamingSpec.kt @@ -45,9 +45,9 @@ class ConstructorParameterNamingSpec { @Test fun `should not complain about override`() { val code = """ - class C(override val PARAM: String) : I - interface I { val PARAM: String } + + class C(override val PARAM: String) : I """.trimIndent() assertThat(ConstructorParameterNaming(Config.empty).compileAndLint(code)).isEmpty() } diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionMinNameLengthSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionMinNameLengthSpec.kt index 66e970e0200..fc1678fbffb 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionMinNameLengthSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionMinNameLengthSpec.kt @@ -32,10 +32,10 @@ class FunctionMinNameLengthSpec { @Test fun `should not report an overridden function name that is too short`() { val code = """ + interface I { @Suppress("FunctionNameMinLength") fun tooShortButShouldNotBeReportedByDefault() } class C : I { override fun tooShortButShouldNotBeReportedByDefault() {} } - interface I { @Suppress("FunctionNameMinLength") fun tooShortButShouldNotBeReportedByDefault() } """.trimIndent() assertThat( FunctionNameMinLength( diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNameMaxLengthSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNameMaxLengthSpec.kt index 13e48120091..1cc9ce8a727 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNameMaxLengthSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNameMaxLengthSpec.kt @@ -19,10 +19,10 @@ class FunctionNameMaxLengthSpec { @Test fun `should not report an overridden function name that is too long`() { val code = """ + interface I { @Suppress("FunctionNameMaxLength") fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() } class C : I { override fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() {} } - interface I { @Suppress("FunctionNameMaxLength") fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() } """.trimIndent() assertThat( FunctionNameMaxLength(TestConfig("maximumFunctionNameLength" to 10)).compileAndLint(code) diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNamingSpec.kt index f6158c4b241..726e1527ad9 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNamingSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionNamingSpec.kt @@ -45,23 +45,23 @@ class FunctionNamingSpec { @Test fun `flags functions inside functions`() { val code = """ + interface I { fun shouldNotBeFlagged() } class C : I { override fun shouldNotBeFlagged() { fun SHOULD_BE_FLAGGED() { } } } - interface I { fun shouldNotBeFlagged() } """.trimIndent() - assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(3, 13) + assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(4, 13) } @Test fun `ignores overridden functions`() { val code = """ + interface I { @Suppress("FunctionNaming") fun SHOULD_NOT_BE_FLAGGED() } class C : I { override fun SHOULD_NOT_BE_FLAGGED() {} } - interface I { @Suppress("FunctionNaming") fun SHOULD_NOT_BE_FLAGGED() } """.trimIndent() assertThat(FunctionNaming(Config.empty).compileAndLint(code)).isEmpty() } @@ -89,14 +89,14 @@ class FunctionNamingSpec { @Test fun `flags functions with bad names inside overridden functions by default`() { val code = """ + interface I { @Suppress("FunctionNaming") fun SHOULD_BE_FLAGGED() } class C : I { override fun SHOULD_BE_FLAGGED() { fun SHOULD_BE_FLAGGED() {} } } - interface I { @Suppress("FunctionNaming") fun SHOULD_BE_FLAGGED() } """.trimIndent() - assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(3, 13) + assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(4, 13) } @Test diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionParameterNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionParameterNamingSpec.kt index e256a5d0f02..d879fc75d2c 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionParameterNamingSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/FunctionParameterNamingSpec.kt @@ -27,10 +27,10 @@ class FunctionParameterNamingSpec { @Test fun `should not detect violations in overridden function`() { val code = """ + interface I { fun someStuff(@Suppress("FunctionParameterNaming") `object`: String) } class C : I { override fun someStuff(`object`: String) {} } - interface I { fun someStuff(@Suppress("FunctionParameterNaming") `object`: String) } """.trimIndent() assertThat(FunctionParameterNaming(Config.empty).compileAndLint(code)).isEmpty() } diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MemberNameEqualsClassNameSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MemberNameEqualsClassNameSpec.kt index 82db5af502d..2d049c14d37 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MemberNameEqualsClassNameSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MemberNameEqualsClassNameSpec.kt @@ -127,12 +127,12 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) { @Test fun `doesn't report overridden methods which are named after the class`() { val code = """ - class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { - override fun AbstractMethodNameEqualsClassName() {} - } abstract class BaseClassForMethodNameEqualsClassName { abstract fun AbstractMethodNameEqualsClassName() } + class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { + override fun AbstractMethodNameEqualsClassName() {} + } """.trimIndent() assertThat(MemberNameEqualsClassName(Config.empty).compileAndLint(code)).isEmpty() } @@ -150,12 +150,12 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) { @Test fun `reports overridden methods which are named after the class if they are not ignored`() { val code = """ - class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { - override fun AbstractMethodNameEqualsClassName() {} - } abstract class BaseClassForMethodNameEqualsClassName { abstract fun AbstractMethodNameEqualsClassName() } + class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { + override fun AbstractMethodNameEqualsClassName() {} + } """.trimIndent() assertThat(MemberNameEqualsClassName(noIgnoreOverridden).compileAndLint(code)).hasSize(1) } @@ -163,12 +163,12 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) { @Test fun `doesn't report overridden properties which are named after the class`() { val code = """ - class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { - override val AbstractMethodNameEqualsClassName = "" - } abstract class BaseClassForMethodNameEqualsClassName { abstract val AbstractMethodNameEqualsClassName: String } + class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { + override val AbstractMethodNameEqualsClassName = "" + } """.trimIndent() assertThat(MemberNameEqualsClassName(Config.empty).compileAndLint(code)).isEmpty() } @@ -176,12 +176,12 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) { @Test fun `reports overridden properties which are named after the class if they are not ignored`() { val code = """ - class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { - override val AbstractMethodNameEqualsClassName = "" - } abstract class BaseClassForMethodNameEqualsClassName { abstract val AbstractMethodNameEqualsClassName: String } + class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() { + override val AbstractMethodNameEqualsClassName = "" + } """.trimIndent() assertThat(MemberNameEqualsClassName(noIgnoreOverridden).compileAndLint(code)).hasSize(1) } diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMaxLengthSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMaxLengthSpec.kt index cd0c73c11f2..ffa4fda47d4 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMaxLengthSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMaxLengthSpec.kt @@ -28,14 +28,14 @@ class VariableMaxLengthSpec { @Test fun `should not report an overridden variable name that is too long`() { val code = """ - class C : I { - override val tooLongButShouldNotBeReported = "banana" + interface I2 { + @Suppress("VariableMaxLength") val tooLongButShouldNotBeReported: String } interface I : I2 { override val tooLongButShouldNotBeReported: String } - interface I2 { - @Suppress("VariableMaxLength") val tooLongButShouldNotBeReported: String + class C : I { + override val tooLongButShouldNotBeReported = "banana" } """.trimIndent() assertThat( diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMinLengthSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMinLengthSpec.kt index 93c2743c2cf..b757279e1c9 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMinLengthSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableMinLengthSpec.kt @@ -58,14 +58,14 @@ class VariableMinLengthSpec { @Test fun `should not report an overridden variable name that is too short`() { val code = """ - class C : I { - override val shortButOk = "banana" + interface I2 { + @Suppress("VariableMinLength") val shortButOk: String } interface I : I2 { override val shortButOk: String } - interface I2 { - @Suppress("VariableMinLength") val shortButOk: String + class C : I { + override val shortButOk = "banana" } """.trimIndent() assertThat( diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableNamingSpec.kt index a96346228ab..13f0e14fa2a 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableNamingSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/VariableNamingSpec.kt @@ -81,14 +81,14 @@ class VariableNamingSpec { @Test fun `should not flag overridden member properties`() { val code = """ - class C : I { - override val SHOULD_NOT_BE_FLAGGED = "banana" + interface I2 { + @Suppress("VariableNaming") val SHOULD_NOT_BE_FLAGGED: String } interface I : I2 { override val SHOULD_NOT_BE_FLAGGED: String } - interface I2 { - @Suppress("VariableNaming") val SHOULD_NOT_BE_FLAGGED: String + class C : I { + override val SHOULD_NOT_BE_FLAGGED = "banana" } """.trimIndent() assertThat(VariableNaming(Config.empty).compileAndLint(code)).isEmpty() diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeConcreteClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeConcreteClassSpec.kt index c624858a3bb..af095f01039 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeConcreteClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeConcreteClassSpec.kt @@ -277,13 +277,12 @@ class AbstractClassCanBeConcreteClassSpec(val env: KotlinCoreEnvironment) { @Test fun `does not report an abstract class with a function derived from an interface`() { val code = """ - abstract class A : Interface { - fun g() {} - } - interface Interface { fun f() } + abstract class A : Interface { + fun g() {} + } """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeInterfaceSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeInterfaceSpec.kt index 6bff295a087..c28a8d885b9 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeInterfaceSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/AbstractClassCanBeInterfaceSpec.kt @@ -281,13 +281,12 @@ class AbstractClassCanBeInterfaceSpec(val env: KotlinCoreEnvironment) { @Test fun `does not report an abstract class with a function derived from an interface`() { val code = """ - abstract class A : Interface { - fun g() {} - } - interface Interface { fun f() } + abstract class A : Interface { + fun g() {} + } """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() }