Skip to content

Commit

Permalink
Fix missing import-alias type, and pass all tests of RenameTypeAliasTest
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Nov 7, 2023
1 parent 8bd266b commit ac73c01
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public J.Identifier visitIdentifier(J.Identifier i, ExecutionContext executionCo
if (!i.getSimpleName().equals(aliasName) || !TypeUtils.isOfClassType(i.getType(), fullyQualifiedAliasedType)) {
return i;
}
if (!isVariableName(getCursor().getParentTreeCursor(), i)) {
if (!isVariableName(getCursor().getParentTreeCursor(), i) ||
isAliasImport(getCursor().getParentTreeCursor(), i) ) {
i = i.withSimpleName(newName);
}
return i;
Expand Down Expand Up @@ -95,4 +96,13 @@ private boolean isVariableName(Cursor cursor, J.Identifier ident) {
}
return true;
}

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

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,7 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex
Markers.EMPTY,
new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY),
padLeft(Space.EMPTY, (J.Identifier) reference),
null
type(importDirective)
);
}

Expand All @@ -2395,7 +2395,7 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex
rpStatic,
(J.FieldAccess) reference,
// TODO: fix NPE.
alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), null)) : null
alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), type(alias))) : null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class PsiTreePrinter {
private static KotlinIrTypeMapping irTypeMapping = new KotlinIrTypeMapping(new JavaTypeCache());

// Set to true to print types and verify, otherwise just verify the parse to print idempotent.
private final static boolean printTypes = false;
private final static boolean printTypes = true;

private final List<StringBuilder> outputLines;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass
import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaTypeParameter
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.Variance
import org.openrewrite.java.JavaTypeSignatureBuilder
import org.openrewrite.java.tree.JavaType
Expand Down Expand Up @@ -167,6 +168,14 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val
javaElement(type)
}

is FirResolvedImport -> {
signature(type.importedFqName)
}

is FqName -> {
type.asString()
}

else -> "{undefined}"
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import static org.openrewrite.kotlin.Assertions.kotlin;

public class RenameTypeAliasTest implements RewriteTest {
class RenameTypeAliasTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RenameTypeAlias("OldAlias", "NewAlias", "Test"));
Expand Down Expand Up @@ -71,10 +71,10 @@ class Test
);
}

@ExpectedToFail("FirImport does not contain type attribution.")
@Test
void _import() {
void aliasImport() {
rewriteRun(
spec -> spec.recipe(new RenameTypeAlias("OldAlias", "NewAlias", "foo.Test")),
kotlin(
"""
package foo
Expand All @@ -84,13 +84,13 @@ class Test
kotlin(
"""
import foo.Test as OldAlias
val a : OldAlias = OldAlias()
""",
"""
import foo.Test as NewAlias
val a : NewAlias = Test()
val a : NewAlias = NewAlias()
"""
)
);
Expand Down

0 comments on commit ac73c01

Please sign in to comment.