Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface vs Data class bug#1003 #1014

Merged
merged 2 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.pinterest.ktlint.core.ast.ElementType.DATA_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.ENUM_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.INNER_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.INTERFACE_KEYWORD
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.PRIMARY_CONSTRUCTOR
Expand Down Expand Up @@ -42,7 +43,7 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
}

private fun handleClass(node: ASTNode) {
if (node.isDataClass()) {
if (node.isDataClassOrInterface()) {
return
}

Expand Down Expand Up @@ -144,12 +145,12 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
}

@Suppress("UnsafeCallOnNullableType")
private fun ASTNode.isDataClass(): Boolean {
private fun ASTNode.isDataClassOrInterface(): Boolean {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
if (hasChildOfType(MODIFIER_LIST)) {
val list = getFirstChildWithType(MODIFIER_LIST)!!
return list.getChildren(null).any { it.elementType == DATA_KEYWORD }
}
return false
return hasChildOfType(INTERFACE_KEYWORD)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@ class DataClassesRuleWarnTest : LintTestBase(::DataClassesRule) {
""".trimMargin()
)
}

@Test
@Tag(USE_DATA_CLASS)
fun `shouldn't trigger on interface`() {
lintMethod(
"""
|interface Credentials {
| val code: String
| val success: Boolean
| val message: String
|}
""".trimMargin()
)
}
}