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

Do not fail for default Modifier in abstract functions #111

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -32,5 +32,8 @@ val KtFunction.isActual: Boolean
val KtFunction.isExpect: Boolean
get() = hasModifier(KtTokens.EXPECT_KEYWORD)

val KtFunction.isAbstract: Boolean
get() = hasModifier(KtTokens.ABSTRACT_KEYWORD)

val KtFunction.definedInInterface: Boolean
get() = ((parent as? KtClassBody)?.parent as? KtClass)?.isInterface() ?: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.twitter.compose.rules
import com.twitter.rules.core.ComposeKtVisitor
import com.twitter.rules.core.Emitter
import com.twitter.rules.core.util.definedInInterface
import com.twitter.rules.core.util.isAbstract
import com.twitter.rules.core.util.isActual
import com.twitter.rules.core.util.isModifier
import com.twitter.rules.core.util.isOverride
Expand All @@ -15,7 +16,7 @@ import org.jetbrains.kotlin.psi.KtFunction
class ComposeModifierWithoutDefault : ComposeKtVisitor {

override fun visitComposable(function: KtFunction, autoCorrect: Boolean, emitter: Emitter) {
if (function.definedInInterface || function.isActual || function.isOverride) return
if (function.definedInInterface || function.isActual || function.isOverride || function.isAbstract) return

// Look for modifier params in the composable signature, and if any without a default value is found, error out.
function.valueParameters.filter { it.isModifier }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class ComposeModifierWithoutDefaultCheckTest {
assertThat(errors).isEmpty()
}

@Test
fun `passes when a Composable is an abstract function but without default values`() {
@Language("kotlin")
val composableCode = """
abstract class Bleh {
@Composable
abstract fun Something(modifier: Modifier)
}
""".trimIndent()

val errors = rule.lint(composableCode)
assertThat(errors).isEmpty()
}

@Test
fun `passes when a Composable has modifiers with defaults`() {
@Language("kotlin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ class ComposeModifierWithoutDefaultCheckTest {
modifierRuleAssertThat(composableCode).hasNoLintViolations()
}

@Test
fun `passes when a Composable is an abstract function but without default values`() {
@Language("kotlin")
val composableCode = """
abstract class Bleh {
@Composable
abstract fun Something(modifier: Modifier)
}
""".trimIndent()

modifierRuleAssertThat(composableCode).hasNoLintViolations()
}

@Test
fun `passes when a Composable has modifiers with defaults`() {
@Language("kotlin")
Expand Down