Remove carriage returns from source file text

The Kotlin compiler removes carriage returns from the text of the file
when calculating the startOffset and endOffset properties for an
IrExpression. This causes the source snippet extraction to not work for
files with Windows-style line-separators.

Before pull substrings out of the file source string, remove all
carriages returns so the IrExpression offsets match the compiler.
This commit is contained in:
Brian Norman
2020-09-14 20:01:01 -05:00
parent 22a1e16324
commit 6de1f2ac1f
2 changed files with 19 additions and 2 deletions
@@ -84,7 +84,7 @@ class PowerAssertCallTransformer(
override fun lower(irFile: IrFile) {
file = irFile
fileSource = File(irFile.path).readText()
fileSource = File(irFile.path).readText().replace("\r\n", "\n")
irFile.transformChildrenVoid()
}
@@ -382,6 +382,23 @@ check(1 == 2) { "the world is broken" }
PowerAssertComponentRegistrar(setOf(FqName("kotlin.check")))
)
}
@Test
fun carriageReturnRemoval() {
assertMessage(
"""
fun main() {
val a = 0
assert(a == 42)
}""".replace("\n", "\r\n"),
"""
Assertion failed
assert(a == 42)
| |
| false
0
""".trimIndent())
}
}
fun assertMessage(
@@ -390,7 +407,7 @@ fun assertMessage(
vararg plugins: ComponentRegistrar = arrayOf(PowerAssertComponentRegistrar(setOf(FqName("kotlin.assert"))))
) {
val result = KotlinCompilation().apply {
sources = listOf(SourceFile.kotlin("main.kt", source))
sources = listOf(SourceFile.kotlin("main.kt", source, trimIndent = false))
useIR = true
messageOutputStream = System.out
compilerPlugins = plugins.toList()