Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type validation to Assertions. #543

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 334 additions & 8 deletions src/main/java/org/openrewrite/kotlin/Assertions.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOu
p.append("val");
}

if (m.getType() == J.Modifier.Type.LanguageExtension && m.getKeyword().equals("typealias")) {
if (m.getType() == J.Modifier.Type.LanguageExtension && m.getKeyword() != null && m.getKeyword().equals("typealias")) {
isTypeAlias = true;
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/openrewrite/kotlin/search/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.kotlin.search;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -947,6 +948,7 @@ class A : RemoteStub()
@Test
void unknownIdentifier() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
kotlin(
//language=none
"class A : RemoteStub",
Expand Down
28 changes: 16 additions & 12 deletions src/test/java/org/openrewrite/kotlin/RemoveImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.Recipe;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.kotlin.Assertions.kotlin;
import static org.openrewrite.test.RewriteTest.toRecipe;
Expand All @@ -34,9 +35,9 @@ void jvmStaticMember() {
"""
import java.lang.Integer
import java.lang.Long

import java.lang.Integer.MAX_VALUE

class A
""",
"""
Expand All @@ -56,7 +57,7 @@ void removeStarFoldPackage() {
kotlin(
"""
import java.io.*

class A {
val f = File("foo")
}
Expand All @@ -79,7 +80,7 @@ void keepStarFoldPackage() {
kotlin(
"""
import java.io.*

class A {
val c : Closeable? = null
val f : File? = null
Expand All @@ -99,7 +100,7 @@ void removeStarFoldTypeMembers() {
kotlin(
"""
import java.util.regex.Pattern.*

class A {
val i = CASE_INSENSITIVE
val x = COMMENTS
Expand All @@ -108,7 +109,7 @@ class A {
"""
import java.util.regex.Pattern.CASE_INSENSITIVE
import java.util.regex.Pattern.COMMENTS

class A {
val i = CASE_INSENSITIVE
val x = COMMENTS
Expand All @@ -125,7 +126,7 @@ void keepStarFoldTypeMembers() {
kotlin(
"""
import java.util.regex.Pattern.*

class A {
val i = CASE_INSENSITIVE
val x = COMMENTS
Expand All @@ -139,12 +140,13 @@ class A {
@Test
void keepImportAlias() {
rewriteRun(
spec -> spec.recipe(removeMemberImportRecipe("java.util.regex.Pattern", "COMMENTS")),
spec -> spec.typeValidationOptions(TypeValidation.none())
.recipe(removeMemberImportRecipe("java.util.regex.Pattern", "COMMENTS")),
kotlin(
"""
import java.util.regex.Pattern.CASE_INSENSITIVE as i
import java.util.regex.Pattern.COMMENTS as x

class A {
val f = arrayOf(i, x)
}
Expand All @@ -157,19 +159,21 @@ class A {
void removeImportAlias() {
// TODO check if this is really what we want to happen
rewriteRun(
spec -> spec.recipe(removeMemberImportRecipe("java.util.regex.Pattern", "COMMENTS")),
// Type validation is disabled until https://github.com/openrewrite/rewrite-kotlin/issues/545 is implemented.
spec -> spec.typeValidationOptions(TypeValidation.none())
.recipe(removeMemberImportRecipe("java.util.regex.Pattern", "COMMENTS")),
kotlin(
"""
import java.util.regex.Pattern.CASE_INSENSITIVE as i
import java.util.regex.Pattern.COMMENTS as x

class A {
val f = arrayOf(i)
}
""",
"""
import java.util.regex.Pattern.CASE_INSENSITIVE as i

class A {
val f = arrayOf(i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.style.NamedStyles;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -2223,8 +2224,11 @@ fun test0() {
@Test
void trailingLambdaCall() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
kotlin(
"""
import kotlin.reflect.full.memberProperties

inline fun <reified T : Any> T.destruct(): Map<String, Any?> {
return T::class.memberProperties.map {
it.name to it.get(this)
Expand Down
Loading