Fix and add some required type checks

This commit is contained in:
Dmitry Jemerov
2017-10-05 12:34:23 +02:00
parent b12f24b41c
commit 31fb34d9af
2 changed files with 41 additions and 27 deletions
@@ -181,14 +181,15 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
val lightMethod = LightClassUtil.getLightClassAccessorMethod(original) ?: return null val lightMethod = LightClassUtil.getLightClassAccessorMethod(original) ?: return null
convertDeclaration(lightMethod, givenParent, requiredType) convertDeclaration(lightMethod, givenParent, requiredType)
} }
is KtProperty -> el<UDeclaration> {
is KtProperty ->
if (original.isLocal) { if (original.isLocal) {
KotlinConverter.convertPsiElement(element, givenParent, requiredType) KotlinConverter.convertPsiElement(element, givenParent, requiredType)
} }
else { else {
convertNonLocalProperty(original, givenParent, requiredType) convertNonLocalProperty(original, givenParent, requiredType)
} }
}
is KtParameter -> el<UParameter> { is KtParameter -> el<UParameter> {
val ownerFunction = original.ownerFunction as? KtFunction ?: return null val ownerFunction = original.ownerFunction as? KtFunction ?: return null
val lightMethod = LightClassUtil.getLightClassMethod(ownerFunction) ?: return null val lightMethod = LightClassUtil.getLightClassMethod(ownerFunction) ?: return null
@@ -240,7 +241,9 @@ private fun convertNonLocalProperty(property: KtProperty,
requiredType: Class<out UElement>?): UElement? { requiredType: Class<out UElement>?): UElement? {
val methods = LightClassUtil.getLightClassPropertyMethods(property) val methods = LightClassUtil.getLightClassPropertyMethods(property)
return methods.backingField?.let { backingField -> return methods.backingField?.let { backingField ->
KotlinUField(backingField, givenParent) with(requiredType) {
el<UField> { KotlinUField(backingField, givenParent) }
}
} ?: methods.getter?.let { getter -> } ?: methods.getter?.let { getter ->
KotlinUastLanguagePlugin().convertDeclaration(getter, givenParent, requiredType) KotlinUastLanguagePlugin().convertDeclaration(getter, givenParent, requiredType)
} }
@@ -301,7 +304,7 @@ internal object KotlinConverter {
is KtLiteralStringTemplateEntry, is KtEscapeStringTemplateEntry -> el<ULiteralExpression>(build(::KotlinStringULiteralExpression)) is KtLiteralStringTemplateEntry, is KtEscapeStringTemplateEntry -> el<ULiteralExpression>(build(::KotlinStringULiteralExpression))
is KtStringTemplateEntry -> element.expression?.let { convertExpression(it, givenParent, requiredType) } ?: expr<UExpression> { UastEmptyExpression } is KtStringTemplateEntry -> element.expression?.let { convertExpression(it, givenParent, requiredType) } ?: expr<UExpression> { UastEmptyExpression }
is KtWhenEntry -> el<USwitchClauseExpressionWithBody>(build(::KotlinUSwitchEntry)) is KtWhenEntry -> el<USwitchClauseExpressionWithBody>(build(::KotlinUSwitchEntry))
is KtWhenCondition -> convertWhenCondition(element, givenParent) is KtWhenCondition -> convertWhenCondition(element, givenParent, requiredType)
is KtTypeReference -> el<UTypeReferenceExpression> { LazyKotlinUTypeReferenceExpression(element, givenParent) } is KtTypeReference -> el<UTypeReferenceExpression> { LazyKotlinUTypeReferenceExpression(element, givenParent) }
else -> { else -> {
@@ -422,9 +425,13 @@ internal object KotlinConverter {
}} }}
} }
internal fun convertWhenCondition(condition: KtWhenCondition, givenParent: UElement?): UExpression { internal fun convertWhenCondition(condition: KtWhenCondition,
return when (condition) { givenParent: UElement?,
is KtWhenConditionInRange -> KotlinCustomUBinaryExpression(condition, givenParent).apply { requiredType: Class<out UElement>? = null): UExpression? {
return with(requiredType) {
when (condition) {
is KtWhenConditionInRange -> expr<UBinaryExpression> {
KotlinCustomUBinaryExpression(condition, givenParent).apply {
leftOperand = KotlinStringUSimpleReferenceExpression("it", this) leftOperand = KotlinStringUSimpleReferenceExpression("it", this)
operator = when { operator = when {
condition.isNegated -> KotlinBinaryOperators.NOT_IN condition.isNegated -> KotlinBinaryOperators.NOT_IN
@@ -432,7 +439,9 @@ internal object KotlinConverter {
} }
rightOperand = KotlinConverter.convertOrEmpty(condition.rangeExpression, this) rightOperand = KotlinConverter.convertOrEmpty(condition.rangeExpression, this)
} }
is KtWhenConditionIsPattern -> KotlinCustomUBinaryExpressionWithType(condition, givenParent).apply { }
is KtWhenConditionIsPattern -> expr<UBinaryExpression> {
KotlinCustomUBinaryExpressionWithType(condition, givenParent).apply {
operand = KotlinStringUSimpleReferenceExpression("it", this) operand = KotlinStringUSimpleReferenceExpression("it", this)
operationKind = when { operationKind = when {
condition.isNegated -> KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK condition.isNegated -> KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK
@@ -443,8 +452,13 @@ internal object KotlinConverter {
LazyKotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) } LazyKotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) }
} }
} }
is KtWhenConditionWithExpression -> KotlinConverter.convertOrEmpty(condition.expression, givenParent) }
else -> UastEmptyExpression
is KtWhenConditionWithExpression ->
condition.expression?.let { KotlinConverter.convertExpression(it, givenParent, requiredType) }
else -> expr<UExpression> { UastEmptyExpression }
}
} }
} }
@@ -53,7 +53,7 @@ class KotlinUSwitchEntry(
givenParent: UElement? givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), USwitchClauseExpressionWithBody { ) : KotlinAbstractUExpression(givenParent), USwitchClauseExpressionWithBody {
override val caseValues by lz { override val caseValues by lz {
psi.conditions.map { KotlinConverter.convertWhenCondition(it, this) } psi.conditions.map { KotlinConverter.convertWhenCondition(it, this) ?: UastEmptyExpression }
} }
override val body: UExpressionList by lz { override val body: UExpressionList by lz {