New J2K: Fix ClassToObjectPromotionConversion when empty constructor

This commit is contained in:
Ilya Kirillov
2018-11-08 16:36:35 +03:00
committed by Ilya Kirillov
parent 44185c1246
commit 70def791c0
@@ -14,29 +14,36 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
class ClassToObjectPromotionConversion : RecursiveApplicableConversionBase() {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element is JKClass && element.classKind == JKClass.ClassKind.CLASS) {
val constructor = element.declarationList.firstIsInstanceOrNull<JKKtPrimaryConstructor>()
val companion = element.declarationList.firstOrNull { it is JKClass && it.classKind == JKClass.ClassKind.COMPANION }
if (companion != null && (constructor == null || constructor.parameters.isEmpty())) {
val declarations = element.declarationList - companion - listOfNotNull(constructor)
if (declarations.isEmpty()) {
companion as JKClass
companion.invalidate()
element.invalidate()
return recurse(
JKClassImpl(
element.modifierList,
element.name,
element.inheritance,
JKClass.ClassKind.OBJECT,
element.typeParameterList
)
.apply {
declarationList = companion.declarationList
}
)
val companion =
element.declarationList.firstIsInstanceOrNull<JKClass>()
?.takeIf { it.classKind == JKClass.ClassKind.COMPANION }
?: return recurse(element)
val allDeclarationsMatches = element.declarationList.all {
when (it) {
is JKKtPrimaryConstructor -> it.parameters.isEmpty() && it.block.statements.isEmpty()
is JKClass -> it.classKind == JKClass.ClassKind.COMPANION
else -> false
}
}
if (allDeclarationsMatches) {
companion.invalidate()
element.invalidate()
return recurse(
JKClassImpl(
element.modifierList,
element.name,
element.inheritance,
JKClass.ClassKind.OBJECT,
element.typeParameterList
).apply {
declarationList = companion.declarationList
}
)
}
}
return recurse(element)
}
}