https://kotlinlang.org logo
#detekt
Title
# detekt
b

bbaldino

01/21/2020, 5:45 AM
Is there a way in a detekt spec (for a rule I'm working on) to use
${...}
in the compiled test code? If I do:
Copy code
val code = """
    fun foo() {
        println("The list is ${listOf(1, 2, 3)}"
    }
""".trimIndent()
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(0)
then the compiled
ktFile
has the list expanded, but if I do:
Copy code
val code = """
    fun foo() {
        println("The list is \$\{listOf(1, 2, 3)\}"
    }
""".trimIndent()
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(0)
then the compiled
ktFile
maintains the escape characters, so the code isn't actually doing the expansion
What I want is for the code to be analyzed to actually contain
println("The list is ${listOf(1, 2, 3)}"
s

schalkms

01/21/2020, 7:51 PM
Nice to hear that you are working on a detekt rule. 🙂 The mentioned problem actually relates to the Kotlin multi-line string behavior. This has nothing to do with the
compileAndLint
functionality.
b

bbaldino

01/21/2020, 7:52 PM
Yeah, I was wondering if perhaps a case like this had been hit before and
compileAndLint
(or some other helper function) existed to work around it in detekt
I did look through the other specs, but didn't see any cases of something like this...but was hoping I'd missed something.
s

schalkms

01/23/2020, 6:27 PM
Nope there isn't, sorry. Please submit a PR, if you develop a helper function for that case.
b

bbaldino

01/23/2020, 6:27 PM
Ok, thanks for clarifying
a

Artur Bosch

01/27/2020, 8:35 AM
We might need test files containing the code to test interpolation code.
b

bbaldino

01/27/2020, 6:00 PM
Is there a way to do that currently? i.e. if I put a
.kt
file in resources or something, is there a form of
compileAndLintWithContext
that takes in a file?
c

Czar

01/28/2020, 2:29 PM
have you tried
${'$'}
instead of an escape?
Copy code
val code = """
    fun foo() {
        println("The list is ${'$'}{listOf(1, 2, 3)}"
    }
	""".trimIndent()
b

bbaldino

01/28/2020, 5:39 PM
I haven't, but looks like that did it! Thanks @Czar!
s

schalkms

01/28/2020, 6:11 PM
Thanks for sharing this!
c

Czar

01/28/2020, 6:41 PM
You're welcome, funny thing is someone was just asking how to escape
$
without resorting to
${'$'}
several messages above 😄
3 Views