Fix conversion of destructuring declarations

This commit is contained in:
Dmitry Jemerov
2017-09-13 12:58:58 +02:00
parent 40daeb13d1
commit b638febc41
5 changed files with 38 additions and 4 deletions
@@ -285,13 +285,14 @@ internal object KotlinConverter {
}
}
is KtDestructuringDeclaration -> expr<UDeclarationsExpression> {
KotlinUDeclarationsExpression(givenParent).apply {
val tempAssignment = KotlinULocalVariable(UastKotlinPsiVariable.create(expression, uastParent!!), givenParent)
val declarationsExpression = KotlinUDeclarationsExpression(givenParent)
declarationsExpression.apply {
val tempAssignment = KotlinULocalVariable(UastKotlinPsiVariable.create(expression, declarationsExpression), declarationsExpression)
val destructuringAssignments = expression.entries.mapIndexed { i, entry ->
val psiFactory = KtPsiFactory(expression.project)
val initializer = psiFactory.createAnalyzableExpression("${tempAssignment.name}.component${i + 1}()",
expression.containingFile)
KotlinULocalVariable(UastKotlinPsiVariable.create(entry, tempAssignment.psi, uastParent!!, initializer), givenParent)
KotlinULocalVariable(UastKotlinPsiVariable.create(entry, tempAssignment.psi, declarationsExpression, initializer), declarationsExpression)
}
declarations = listOf(tempAssignment) + destructuringAssignments
}
@@ -0,0 +1,3 @@
fun foo() {
val (a, b) = "foo" to 1
}
@@ -0,0 +1,21 @@
UFile (package = )
UClass (name = DestructuringDeclarationKt)
UAnnotationMethod (name = foo)
UBlockExpression
UDeclarationsExpression
ULocalVariable (name = var268d4034)
UBinaryExpression (operator = <other>)
ULiteralExpression (value = "foo")
ULiteralExpression (value = 1)
ULocalVariable (name = a)
UQualifiedReferenceExpression
USimpleNameReferenceExpression (identifier = var268d4034)
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
UIdentifier (Identifier (component1))
USimpleNameReferenceExpression (identifier = <anonymous class>)
ULocalVariable (name = b)
UQualifiedReferenceExpression
USimpleNameReferenceExpression (identifier = var268d4034)
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
UIdentifier (Identifier (component2))
USimpleNameReferenceExpression (identifier = <anonymous class>)
@@ -0,0 +1,7 @@
public final class DestructuringDeclarationKt {
public static final fun foo() : void {
var var268d4034: kotlin.Pair<? extends java.lang.String,? extends java.lang.Integer> = "foo" <other> 1
var a: <ErrorType> = var268d4034.<anonymous class>()
var b: <ErrorType> = var268d4034.<anonymous class>()
}
}
@@ -37,7 +37,9 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
@Test fun testSimpleScript() = doTest("SimpleScript")
@Test fun testDestructuringDeclaration() = doTest("DestructuringDeclaration")
@Test fun testParameterPropertyWithAnnotation() = doTest("ParameterPropertyWithAnnotation")
@Test fun testParametersWithDefaultValues() = doTest("ParametersWithDefaultValues")
}
}