Skip to content

Commit

Permalink
Fix: AbstractClassesRule should also take properties into account (#1102
Browse files Browse the repository at this point in the history
)

### What's done:
* Fix logic
* Add tests

Closes #1100
  • Loading branch information
petertrr authored Nov 8, 2021
1 parent d249fbb commit 1dd9230
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.OPEN_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement

Expand All @@ -40,10 +41,12 @@ class AbstractClassesRule(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("UnsafeCallOnNullableType")
private fun handleAbstractClass(node: ASTNode, classNode: ASTNode) {
val functions = node.getAllChildrenWithType(FUN)
val properties = node.getAllChildrenWithType(PROPERTY)
val members = functions + properties

val identifier = classNode.getFirstChildWithType(IDENTIFIER)!!.text

if (functions.isNotEmpty() && functions.none { hasAbstractModifier(it) }) {
if (members.isNotEmpty() && members.none { hasAbstractModifier(it) }) {
CLASS_SHOULD_NOT_BE_ABSTRACT.warnAndFix(configRules, emitWarn, isFixMode, identifier, node.startOffset, node) {
val modList = classNode.getFirstChildWithType(MODIFIER_LIST)!!
val abstractKeyword = modList.getFirstChildWithType(ABSTRACT_KEYWORD)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ class AbstractClassesWarnTest : LintTestBase(::AbstractClassesRule) {
LintError(1, 58, ruleId, "${Warnings.CLASS_SHOULD_NOT_BE_ABSTRACT.warnText()} CoroutineTest", true)
)
}

@Test
@Tag(CLASS_SHOULD_NOT_BE_ABSTRACT)
fun `should not remove abstract on class if there are only abstract properties`() {
lintMethod(
"""
|abstract class BaseUsesProcessor() {
| // Store uses by file
| abstract val a: String
|
| fun foo() {}
|}
""".trimMargin()
)
}
}

0 comments on commit 1dd9230

Please sign in to comment.