Daniel Souza Bertoldi
08/08/2023, 4:48 PMRoundedCornerShape
. Is it possible to achieve this by finding out the variable type declared in it? More details in the thread.Daniel Souza Bertoldi
08/08/2023, 4:50 PM@Composable
private fun CardWithRoundedCorners(flag: Boolean) {
val cornerRadius by animateDpAsState(
targetValue = if (flag) CornerRadius.none else CornerRadius.lg,
label = "cornerRadiusAnimation",
)
Card(
shape = RoundedCornerShape(cornerRadius),
) {}
}
In this case, cornerRadius is of type CornerRadius
, an object I created:
object CornerRadius {
val none = 0.dp
val lg = 16.dp
}
Daniel Souza Bertoldi
08/08/2023, 4:53 PMRoundedCornerShape
constructor, not when I have a variable of type CornerRadius being passed to RoundedCornerShape
Atul Gupta
08/08/2023, 7:10 PMbindingContext
is map of of complier info(only available when when used with type resolution) which contains various info around the codeDaniel Souza Bertoldi
08/08/2023, 7:43 PMgetType(bindingContext)
function, but it’s not clear yet how I could achieve this.
I created a unit test with compileAndLintWithContext
but I can’t use the evaluation tool when debugging 😞 the tool can’t recognize the return type of getType
Is it possible by coding something like this?
it.getArgumentExpression()?.getType(bindingContext)..isSubtypeOf(CornerRadius)
?
I’m pretty lost, it’s my first time using type resolution 😬Atul Gupta
08/08/2023, 8:52 PMoverride fun visitCallExpression(expression: KtCallExpression) {
expression.valueArguments.forEach {
val superTypes = expression.getResolvedCall(bindingContext)?.getParameterForArgument(it)?.type?.supertypes()?.toList() ?: emptyList()
FqName("compose.package.RoundedCornerShape") in superTypes.map { it.fqNameOrNull() }
}
}
in place of compose.package.RoundedCornerShape
you have to use your class package with class name. And you might have to update some code as wellDaniel Souza Bertoldi
08/09/2023, 12:55 PMgetResolvedCall(bindingContext)
call always returns null when I run my test. I’m structuring my test this way:
val code = """
@Composable
private fun CardWithRoundedCorners(flag: Boolean) {
val cornerRadius by animateDpAsState(
targetValue = if (flag) CornerRadius.none else CornerRadius.lg,
label = "cornerRadiusAnimation",
)
Card(
shape = RoundedCornerShape(cornerRadius),
) {}
}
""".trimIndent()
val findings = WrongRoundedCornerShapeSizeProperty().compileAndLintWithContext(
env,
code,
)
assertEquals(0, findings.size)
I’m using JUnit 5, so I added the @KotlinCoreEnvironmentTest
annotation to my test class along with the env in the constructor but it doesn’t work. I’ve already tried using .lintWithContext()
and using createEnvironment().env
, but to no avail 😕
I tried reading something about this in the docs but couldn’t find anything related to itAtul Gupta
08/09/2023, 12:59 PM@Composable
matter? as detekt itself compile the code AFAIK it will not apply any other Kotlin complier plugin. Also have you added the dependency of compose in your project. right?Atul Gupta
08/09/2023, 1:49 PM@Test
fun `abc`() {
val code = """
import kotlin.properties.Delegates.observable
var cornerRadius: Int by observable(initialValue = if (true) CornerRadius.none else CornerRadius.lg) { _, _, _ ->
}
object CornerRadius {
val none = 0
val lg = 16
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
and able to find the types
Make sure you code is compilable in your TCDaniel Souza Bertoldi
08/09/2023, 1:51 PM