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

Take for loops into account for MultipleContentEmitters rule #137

Merged
merged 1 commit into from
Nov 27, 2023
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 @@ -9,8 +9,10 @@ import io.nlopez.rules.core.util.emitsContent
import io.nlopez.rules.core.util.findChildrenByClass
import io.nlopez.rules.core.util.hasReceiverType
import io.nlopez.rules.core.util.isComposable
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.KtFunction

class MultipleContentEmitters : ComposeKtVisitor {
Expand Down Expand Up @@ -69,11 +71,27 @@ class MultipleContentEmitters : ComposeKtVisitor {
companion object {
internal val KtFunction.directUiEmitterCount: Int
get() = bodyBlockExpression?.let { block ->
block.statements
.filterIsInstance<KtCallExpression>()
.count { it.emitsContent }
// If there's content emitted in a for loop, we assume there's at
// least two iterations and thus count any emitters in them as multiple
val forLoopCount = when {
block.forLoopHasUiEmitters -> 2
else -> 0
}
block.directUiEmitterCount + forLoopCount
} ?: 0

internal val KtBlockExpression.forLoopHasUiEmitters: Boolean
get() = statements.filterIsInstance<KtForExpression>().any {
when (val body = it.body) {
is KtBlockExpression -> body.directUiEmitterCount > 0
is KtCallExpression -> body.emitsContent
else -> false
}
}

internal val KtBlockExpression.directUiEmitterCount: Int
get() = statements.filterIsInstance<KtCallExpression>().count { it.emitsContent }

internal fun KtFunction.indirectUiEmitterCount(mapping: Map<KtFunction, Int>): Int {
val bodyBlock = bodyBlockExpression ?: return 0
return bodyBlock.statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,33 @@ class MultipleContentEmittersCheckTest {
.hasStartSourceLocation(2, 5)
assertThat(errors.first()).hasMessage(MultipleContentEmitters.MultipleContentEmittersDetected)
}

@Test
fun `for loops are captured`() {
@Language("kotlin")
val code = """
@Composable
fun MultipleContent(texts: List<String>, modifier: Modifier = Modifier) {
for (text in texts) {
Text(text)
}
}
@Composable
fun MultipleContent(otherTexts: List<String>, modifier: Modifier = Modifier) {
Text("text 1")
for (otherText in otherTexts) {
Text(otherText)
}
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors)
.hasStartSourceLocations(
SourceLocation(2, 5),
SourceLocation(8, 5),
)
for (error in errors) {
assertThat(error).hasMessage(MultipleContentEmitters.MultipleContentEmittersDetected)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,36 @@ class MultipleContentEmittersCheckTest {
),
)
}

@Test
fun `for loops are captured`() {
@Language("kotlin")
val code = """
@Composable
fun MultipleContent(texts: List<String>, modifier: Modifier = Modifier) {
for (text in texts) {
Text(text)
}
}
@Composable
fun MultipleContent(otherTexts: List<String>, modifier: Modifier = Modifier) {
Text("text 1")
for (otherText in otherTexts) {
Text(otherText)
}
}
""".trimIndent()
emittersRuleAssertThat(code).hasLintViolationsWithoutAutoCorrect(
LintViolation(
line = 2,
col = 5,
detail = MultipleContentEmitters.MultipleContentEmittersDetected,
),
LintViolation(
line = 8,
col = 5,
detail = MultipleContentEmitters.MultipleContentEmittersDetected,
),
)
}
}