Skip to content

Commit

Permalink
Merge pull request #13 from nomisRev/update-to-1.2.0-raise
Browse files Browse the repository at this point in the history
Update to 1.2.0-RC Raise DSL
  • Loading branch information
diastremskii authored Apr 4, 2023
2 parents f4b19d9 + b18b056 commit c8aa77f
Show file tree
Hide file tree
Showing 3 changed files with 527 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val ktlint: Configuration by configurations.creating
dependencies {
compileOnly("io.gitlab.arturbosch.detekt:detekt-api:1.22.0")

testImplementation(platform("io.arrow-kt:arrow-stack:1.1.5"))
testImplementation(platform("io.arrow-kt:arrow-stack:1.1.6-alpha.91"))
testImplementation("io.arrow-kt:arrow-core")

testImplementation("io.gitlab.arturbosch.detekt:detekt-test:1.22.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
import org.jetbrains.kotlin.resolve.BindingContext
Expand All @@ -31,6 +32,25 @@ class NoEffectScopeBindableValueAsStatement(config: Config) : Rule(config) {
Debt.FIVE_MINS,
)

override fun visitNamedFunction(function: KtNamedFunction) {
super.visitNamedFunction(function)

if (bindingContext == BindingContext.EMPTY) {
return
}

val receiverType = bindingContext[BindingContext.FUNCTION, function]
?.extensionReceiverParameter
?.type
?: return

val isEffectScope = isBindableScope(receiverType) || receiverType.supertypes().any(::isBindableScope)

if (isEffectScope) {
EffectScopeVisitor().visitNamedFunction(function)
}
}

override fun visitLambdaExpression(lambdaExpression: KtLambdaExpression) {
super.visitLambdaExpression(lambdaExpression)

Expand All @@ -45,20 +65,21 @@ class NoEffectScopeBindableValueAsStatement(config: Config) : Rule(config) {
?.type
?: return

val isEffectScope = isEffectScope(firstArgumentType) || firstArgumentType.supertypes().any(::isEffectScope)
val isEffectScope = isBindableScope(firstArgumentType) || firstArgumentType.supertypes().any(::isBindableScope)

if (isEffectScope) {
EffectScopeVisitor().visitLambdaExpression(lambdaExpression)
}
}

private fun isEffectScope(type: KotlinType): Boolean = type
private fun isBindableScope(type: KotlinType): Boolean = type
.constructor
.declarationDescriptor
?.fqNameSafe
?.let {
it == FqName("arrow.core.continuations.EffectScope") ||
it == FqName("arrow.core.continuations.EagerEffectScope")
it == FqName("arrow.core.continuations.EagerEffectScope") ||
it == FqName("arrow.core.raise.Raise")
}
?: false

Expand Down Expand Up @@ -105,13 +126,25 @@ class NoEffectScopeBindableValueAsStatement(config: Config) : Rule(config) {
.getQualifiedExpressionForReceiver()
?.selectorExpression

private fun isBindable(type: KotlinType): Boolean =
type
.constructor
.declarationDescriptor
?.fqNameSafe
?.let { it in BindableFqNames }
?: false
private fun isBindable(type: KotlinType): Boolean {
val isBindableFqNames =
type
.constructor
.declarationDescriptor
?.fqNameSafe
?.let { it in BindableFqNames } ?: false

val isRaiseExtensionFunction = type.annotations.hasAnnotation(FqName("kotlin.ExtensionFunctionType")) &&
type
.arguments
.firstOrNull()
?.type
?.constructor
?.declarationDescriptor
?.fqNameSafe == FqName("arrow.core.raise.Raise")

return isBindableFqNames || isRaiseExtensionFunction
}
}

companion object {
Expand Down
Loading

0 comments on commit c8aa77f

Please sign in to comment.