Fix and add some required type checks
This commit is contained in:
@@ -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,29 +425,40 @@ 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? {
|
||||||
leftOperand = KotlinStringUSimpleReferenceExpression("it", this)
|
return with(requiredType) {
|
||||||
operator = when {
|
when (condition) {
|
||||||
condition.isNegated -> KotlinBinaryOperators.NOT_IN
|
is KtWhenConditionInRange -> expr<UBinaryExpression> {
|
||||||
else -> KotlinBinaryOperators.IN
|
KotlinCustomUBinaryExpression(condition, givenParent).apply {
|
||||||
|
leftOperand = KotlinStringUSimpleReferenceExpression("it", this)
|
||||||
|
operator = when {
|
||||||
|
condition.isNegated -> KotlinBinaryOperators.NOT_IN
|
||||||
|
else -> KotlinBinaryOperators.IN
|
||||||
|
}
|
||||||
|
rightOperand = KotlinConverter.convertOrEmpty(condition.rangeExpression, this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rightOperand = KotlinConverter.convertOrEmpty(condition.rangeExpression, this)
|
is KtWhenConditionIsPattern -> expr<UBinaryExpression> {
|
||||||
|
KotlinCustomUBinaryExpressionWithType(condition, givenParent).apply {
|
||||||
|
operand = KotlinStringUSimpleReferenceExpression("it", this)
|
||||||
|
operationKind = when {
|
||||||
|
condition.isNegated -> KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK
|
||||||
|
else -> UastBinaryExpressionWithTypeKind.INSTANCE_CHECK
|
||||||
|
}
|
||||||
|
val typeRef = condition.typeReference
|
||||||
|
typeReference = typeRef?.let {
|
||||||
|
LazyKotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is KtWhenConditionWithExpression ->
|
||||||
|
condition.expression?.let { KotlinConverter.convertExpression(it, givenParent, requiredType) }
|
||||||
|
|
||||||
|
else -> expr<UExpression> { UastEmptyExpression }
|
||||||
}
|
}
|
||||||
is KtWhenConditionIsPattern -> KotlinCustomUBinaryExpressionWithType(condition, givenParent).apply {
|
|
||||||
operand = KotlinStringUSimpleReferenceExpression("it", this)
|
|
||||||
operationKind = when {
|
|
||||||
condition.isNegated -> KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK
|
|
||||||
else -> UastBinaryExpressionWithTypeKind.INSTANCE_CHECK
|
|
||||||
}
|
|
||||||
val typeRef = condition.typeReference
|
|
||||||
typeReference = typeRef?.let {
|
|
||||||
LazyKotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is KtWhenConditionWithExpression -> KotlinConverter.convertOrEmpty(condition.expression, givenParent)
|
|
||||||
else -> UastEmptyExpression
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user