Skip to content

Commit

Permalink
Fixed type on import aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Nov 10, 2023
1 parent 4fb2f02 commit 64a226f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
13 changes: 4 additions & 9 deletions src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public J.Identifier visitIdentifier(J.Identifier i, ExecutionContext executionCo
return i;
}
if (!isVariableName(getCursor().getParentTreeCursor(), i) ||
isAliasImport(getCursor().getParentTreeCursor(), i) ) {
isAliasImport(getCursor().getParentTreeCursor(), i)) {
i = i.withSimpleName(newName);
}
return i;
Expand All @@ -86,20 +86,15 @@ private boolean isVariableName(Cursor cursor, J.Identifier ident) {
Object maybeVd = cursor.getParentTreeCursor().getValue();
if (maybeVd instanceof J.VariableDeclarations) {
J.VariableDeclarations vd = (J.VariableDeclarations) maybeVd;
if (vd.getLeadingAnnotations().stream().anyMatch(it -> "typealias".equals(it.getSimpleName()))) {
return false;
}
return vd.getLeadingAnnotations().stream().noneMatch(it -> "typealias".equals(it.getSimpleName()));
}
return true;
} else if (value instanceof J.ParameterizedType) {
return false;
}
return true;
} else return !(value instanceof J.ParameterizedType);
}

private boolean isAliasImport(Cursor cursor, J.Identifier id) {
if (cursor.getValue() instanceof J.Import) {
J.Import ji = (J.Import) cursor.getValue();
J.Import ji = cursor.getValue();
return ji.getAlias() == id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2411,8 +2411,8 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex
Markers.EMPTY,
rpStatic,
(J.FieldAccess) reference,
// TODO: fix NPE.
alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), type(alias))) : null
// Aliases contain Kotlin `Name` and do not resolve to a type. The aliases type is the import directive, so we set the type to match the import.
alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), type(importDirective))) : null
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -72,7 +70,6 @@ class Test
);
}

@Disabled("Require rewrite release to pass")
@Test
void aliasImport() {
rewriteRun(
Expand All @@ -84,11 +81,13 @@ class Test
"""
),
kotlin(
//language=none
"""
import foo.Test as OldAlias
val a : OldAlias = OldAlias()
""",
//language=none
"""
import foo.Test as NewAlias
Expand Down Expand Up @@ -121,12 +120,14 @@ void functionParameter() {
rewriteRun(
kotlin(
"""
@file:Suppress("UNUSED_PARAMETER")
class Test
typealias OldAlias = Test
fun method(a: OldAlias) {
}
""",
"""
@file:Suppress("UNUSED_PARAMETER")
class Test
typealias NewAlias = Test
fun method(a: NewAlias) {
Expand Down

0 comments on commit 64a226f

Please sign in to comment.