diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index d6e8b0fe..4ce397f4 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -136,7 +136,7 @@ public J visitForExpression(KtForExpression expression, ExecutionContext data) { randomId(), prefix(expression.getLeftParenthesis()), Markers.EMPTY, - padRight((J.VariableDeclarations) requireNonNull(expression.getLoopParameter()).accept(this, data), suffix(expression.getLoopParameter())), + padRight((TypedTree) requireNonNull(expression.getLoopParameter()).accept(this, data), suffix(expression.getLoopParameter())), padRight(requireNonNull(expression.getLoopRange()).accept(this, data) .withPrefix(prefix(expression.getLoopRange().getParent())), suffix(expression.getLoopRange().getParent())) ), @@ -886,6 +886,10 @@ public J visitNullableType(KtNullableType nullableType, ExecutionContext data) { @Override public J visitParameter(KtParameter parameter, ExecutionContext data) { + if (parameter.getDestructuringDeclaration() != null) { + return parameter.getDestructuringDeclaration().accept(this, data).withPrefix(prefix(parameter)); + } + Markers markers = Markers.EMPTY; List leadingAnnotations = new ArrayList<>(); List lastAnnotations = new ArrayList<>(); @@ -916,11 +920,6 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) { } } - if (parameter.getDestructuringDeclaration() != null) { - return mapDestructuringDeclaration(parameter.getDestructuringDeclaration(), data) - .withPrefix(prefix(parameter)); - } - JavaType.Variable vt = variableType(parameter, owner(parameter)); J.Identifier name = createIdentifier(requireNonNull(parameter.getNameIdentifier()), vt, consumedSpaces); @@ -2338,21 +2337,21 @@ public J visitClassBody(KtClassBody classBody, ExecutionContext data) { @Override public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclaration, ExecutionContext data) { - List modifiers = new ArrayList<>(); List leadingAnnotations = new ArrayList<>(); List> destructVars = new ArrayList<>(); JLeftPadded paddedInitializer = null; - J.Modifier modifier = new J.Modifier( - Tree.randomId(), - prefix(multiDeclaration.getValOrVarKeyword(), preConsumedInfix(multiDeclaration)), - Markers.EMPTY, - multiDeclaration.isVar() ? "var" : null, - multiDeclaration.isVar() ? J.Modifier.Type.LanguageExtension : J.Modifier.Type.Final, - Collections.emptyList() - ); - modifiers.add(modifier); + List modifiers = multiDeclaration.getValOrVarKeyword() != null ? + singletonList( + new J.Modifier( + Tree.randomId(), + prefix(multiDeclaration.getValOrVarKeyword(), preConsumedInfix(multiDeclaration)), + Markers.EMPTY, + multiDeclaration.isVar() ? "var" : null, + multiDeclaration.isVar() ? J.Modifier.Type.LanguageExtension : J.Modifier.Type.Final, + Collections.emptyList() + )) : emptyList(); if (multiDeclaration.getInitializer() != null) { paddedInitializer = padLeft(suffix(multiDeclaration.getRPar()), @@ -2445,7 +2444,8 @@ public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclarati Markers.EMPTY, variableDeclarations, null, - JContainer.build(prefix(multiDeclaration.getLPar()), destructVars, Markers.EMPTY) + JContainer.build(prefix(multiDeclaration.getLPar()), destructVars, Markers.EMPTY), + type(multiDeclaration) ); } @@ -3980,49 +3980,6 @@ private List mapAnnotations(List ktAnnotationEn .collect(Collectors.toList()); } - private J mapDestructuringDeclaration(KtDestructuringDeclaration ktDestructuringDeclaration, ExecutionContext data) { - List entries = ktDestructuringDeclaration.getEntries(); - List> variables = new ArrayList<>(entries.size()); - - for (KtDestructuringDeclarationEntry ktDestructuringDeclarationEntry : entries) { - J.Identifier name = (J.Identifier) ktDestructuringDeclarationEntry.accept(this, data); - - J.VariableDeclarations.NamedVariable namedVariable = new J.VariableDeclarations.NamedVariable( - randomId(), - Space.EMPTY, - Markers.EMPTY, - name, - emptyList(), - null, - variableType(ktDestructuringDeclarationEntry, owner(ktDestructuringDeclarationEntry)) - ); - variables.add(padRight(namedVariable, suffix(ktDestructuringDeclarationEntry))); - } - - J j = new J.VariableDeclarations( - randomId(), - prefix(ktDestructuringDeclaration), - Markers.EMPTY.addIfAbsent(new OmitEquals(randomId())), - emptyList(), - emptyList(), - null, - null, - emptyList(), - variables - ); - - if (entries.size() == 1) { - // Handle potential redundant parentheses - List allChildren = getAllChildren(ktDestructuringDeclaration); - int l = findFirstLPAR(allChildren, 0); - int r = findLastRPAR(allChildren, allChildren.size() - 1); - if (l >= 0 && l < r) { - j = new J.Parentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, padRight(j, Space.EMPTY)); - } - } - return j; - } - private J.Modifier mapModifier(PsiElement modifier, List annotations, @Nullable Set consumedSpaces) { Space prefix = prefix(modifier, consumedSpaces); J.Modifier.Type type; diff --git a/src/main/java/org/openrewrite/kotlin/tree/K.java b/src/main/java/org/openrewrite/kotlin/tree/K.java index 494b4dc2..2835bb0c 100644 --- a/src/main/java/org/openrewrite/kotlin/tree/K.java +++ b/src/main/java/org/openrewrite/kotlin/tree/K.java @@ -910,7 +910,7 @@ public String toString() { @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) - final class DestructuringDeclaration implements K, Statement { + final class DestructuringDeclaration implements K, Statement, TypedTree { @Nullable @NonFinal @@ -939,6 +939,11 @@ final class DestructuringDeclaration implements K, Statement { JContainer destructAssignments; + @With + @Getter + @Nullable + JavaType type; + @Deprecated @Nullable public List getAssignments() { @@ -1004,7 +1009,7 @@ public JContainer getDestructAssignments() { } public DestructuringDeclaration withDestructAssignments(JContainer assignments) { - return t.destructAssignments == assignments ? t : new DestructuringDeclaration(t.id, t.prefix, t.markers, t.initializer, null, assignments); + return t.destructAssignments == assignments ? t : new DestructuringDeclaration(t.id, t.prefix, t.markers, t.initializer, null, assignments, t.type); } } diff --git a/src/test/java/org/openrewrite/kotlin/tree/ForLoopTest.java b/src/test/java/org/openrewrite/kotlin/tree/ForLoopTest.java index 4c34ad04..43b3ca82 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ForLoopTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ForLoopTest.java @@ -120,6 +120,20 @@ fun method ( array : Array < Int > ) { ); } + @Test + void destructuring() { + rewriteRun( + kotlin( + """ + fun method (array: Array>) { + for ( (s:String) in array ) { + } + } + """ + ) + ); + } + @Test void downToWithStep() { rewriteRun(