chore: remove unreadable blocks and use comments instead.

This commit is contained in:
Artem Kobzar
2022-03-14 21:01:51 +01:00
committed by teamcity
parent fba4ac12b7
commit c6136619d2
8 changed files with 695 additions and 13 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.isAny
import org.jetbrains.kotlin.ir.util.constructedClassType
import org.jetbrains.kotlin.ir.util.file
@@ -40,14 +41,29 @@ class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer<JsSta
context.newFile(it.owner.file, context.currentFunction, context.localNames)
} ?: context
val block = JsBlock(expression.statements.map { it.accept(this, newContext) })
val statements = expression.statements.map { it.accept(this, newContext) }
if (expression is IrReturnableBlock) {
return if (expression is IrReturnableBlock) {
val label = context.getNameForReturnableBlock(expression)
if (label != null) return JsLabel(label, block)
}
val wrappedStatements = statements.wrapInCommentsInlineFunctionCall(expression)
return block
if (label != null) {
JsLabel(label, JsBlock(wrappedStatements))
} else {
JsVirtualBlock(wrappedStatements)
}
} else {
JsBlock(statements)
}
}
private fun List<JsStatement>.wrapInCommentsInlineFunctionCall(expression: IrReturnableBlock): List<JsStatement> {
val inlineFunctionSymbol = expression.inlineFunctionSymbol ?: return this
val owner = inlineFunctionSymbol.owner
val receiver = (owner.dispatchReceiverParameter ?: owner.extensionReceiverParameter)?.type?.classOrNull?.owner
val receiverName = receiver?.name?.asString()?.plus(".") ?: ""
val funName = "$receiverName${owner.name}"
return listOf(JsSingleLineComment(" Inline function '$funName' call")) + this
}
override fun visitComposite(expression: IrComposite, context: JsGenerationContext): JsStatement {