Skip to content

Commit

Permalink
SONARKT-302 - stop raising issues on external abstract classes in rul…
Browse files Browse the repository at this point in the history
…e S6526 after validating on peach (#388)
  • Loading branch information
erwan-serandour authored Nov 22, 2023
1 parent 5861622 commit ea15223
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,6 @@
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/examples/scripting/jvm-simple-script/script/src/org/jetbrains/kotlin/script/examples/jvm/simple/scriptDef.kt": [
16
],
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/stdlib/js/src/org.w3c/org.khronos.webgl.kt": [
69,
75,
81,
87,
93,
99,
105,
111
],
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/stdlib/js/src/org.w3c/org.w3c.dom.kt": [
280,
335,
341,
356,
543,
581,
719
],
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/stdlib/js/src/org.w3c/org.w3c.dom.svg.kt": [
368,
371,
377,
383,
389,
395,
401,
436,
540,
587,
615,
621,
661,
667,
735,
779,
782,
785,
814,
817
],
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/stdlib/js/src/org.w3c/org.w3c.xhr.kt": [
39
],
"kotlin-kotlin-project:sources/kotlin/kotlin/libraries/tools/binary-compatibility-validator/src/test/kotlin/cases/companions/companions.kt": [
13,
47,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package checks

class AbstractClassShouldBeInterfaceCheckSampleNonCompiling {

external abstract class Foo {} // compliant
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AbstractClassShouldBeInterfaceCheck : AbstractCheck() {

override fun visitClass(klass: KtClass, context: KotlinFileContext) {

if (!klass.isAbstract() || klass.isInterface() || klass.extendsClass()) return
if (!klass.isAbstract() || klass.isInterface() || klass.extendsClass() || klass.isExternal()) return

val allMethods = klass.collectDescendantsOfType<KtFunction>()
val allProperties: List<KtProperty> by lazy { klass.collectDescendantsOfType<KtProperty>() }
Expand All @@ -51,6 +51,7 @@ class AbstractClassShouldBeInterfaceCheck : AbstractCheck() {
private fun KtClass.extendsClass(): Boolean {
return superTypeListEntries.any { it is KtSuperTypeCallEntry }
}
private fun KtClass.isExternal() = hasModifier(KtTokens.EXTERNAL_KEYWORD)

private fun KtDeclaration.isAbstract() = hasModifier(KtTokens.ABSTRACT_KEYWORD)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
*/
package org.sonarsource.kotlin.checks

internal class AbstractClassShouldBeInterfaceCheckTest : CheckTest(AbstractClassShouldBeInterfaceCheck())
internal class AbstractClassShouldBeInterfaceCheckTest : CheckTest(AbstractClassShouldBeInterfaceCheck()),
CheckTestNonCompiling by CheckTestNonCompilingImpl(AbstractClassShouldBeInterfaceCheck())
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SonarSource Kotlin
* Copyright (C) 2018-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.kotlin.checks

import org.junit.jupiter.api.Test
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.testapi.KotlinVerifier
import java.nio.file.Paths

private const val NON_COMPILING_TEST_FILE_POSTFIX = "SampleNonCompiling.kt"
val NON_COMPILING_BASE_DIR = Paths.get("..", "kotlin-checks-test-sources", "src", "main", "files", "non-compiling", "checks")

interface CheckTestNonCompiling {
@Test
fun `non compiling`()
}

class CheckTestNonCompilingImpl(
val check: AbstractCheck,
private val sampleFileNonCompiling: String? = null,
private val shouldReport: Boolean = false,
) : CheckTestNonCompiling {
private val checkName = check::class.java.simpleName


override fun `non compiling`() {
KotlinVerifier(check) {
this.fileName = sampleFileNonCompiling ?: "$checkName$NON_COMPILING_TEST_FILE_POSTFIX"
this.baseDir = NON_COMPILING_BASE_DIR
this.classpath = emptyList()
this.deps = emptyList()
}.let {
if (this.shouldReport) it.verify() else it.verifyNoIssue()
}
}
}

0 comments on commit ea15223

Please sign in to comment.