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

Restrict emitters count for KtBinaryExpression to ELVIS only #307

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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 @@ -4,6 +4,7 @@ package io.nlopez.compose.core.util

import io.nlopez.compose.core.ComposeKtConfig
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtCallExpression
Expand Down Expand Up @@ -98,6 +99,7 @@ private fun KtExpression.uiEmitterCount(): Int = when (val current = this) {
// Scoped functions like run, with, etc.
current.calleeExpression?.text in KotlinScopeFunctions ->
current.lambdaArguments.singleOrNull()?.getLambdaExpression()?.bodyExpression?.uiEmitterCount() ?: 0

else -> 0
}

Expand All @@ -121,9 +123,13 @@ private fun KtExpression.uiEmitterCount(): Int = when (val current = this) {

// Elvis operators. E.g. text?.let { Text(it) } ?: Text("default")
is KtBinaryExpression -> {
val leftCount = current.left?.uiEmitterCount() ?: 0
val rightCount = current.right?.uiEmitterCount() ?: 0
max(leftCount, rightCount)
if (current.operationToken == KtTokens.ELVIS) {
val leftCount = current.left?.uiEmitterCount() ?: 0
val rightCount = current.right?.uiEmitterCount() ?: 0
max(leftCount, rightCount)
} else {
0
}
}

is KtIfExpression -> {
Expand Down