Refactor sealed subclass to object: add tests, fix type parameter case
This commit is contained in:
@@ -38,6 +38,7 @@ class CanSealedSubClassBeObjectInspection : AbstractKotlinInspection() {
|
||||
klass.getSubclasses()
|
||||
.withEmptyConstructors()
|
||||
.thatAreFinal()
|
||||
.thatHasNoTypeParameters()
|
||||
.thatHasNoInnerClasses()
|
||||
.thatHasNoCompanionObjects()
|
||||
.forEach { reportPossibleObject(it) }
|
||||
@@ -74,6 +75,10 @@ class CanSealedSubClassBeObjectInspection : AbstractKotlinInspection() {
|
||||
return filter { klass -> klass.getModalityFromDescriptor() == KtTokens.FINAL_KEYWORD }
|
||||
}
|
||||
|
||||
private fun List<KtClass>.thatHasNoTypeParameters(): List<KtClass> {
|
||||
return filter { klass -> klass.typeParameters.isEmpty() }
|
||||
}
|
||||
|
||||
private fun List<KtClass>.thatHasNoInnerClasses(): List<KtClass> {
|
||||
return filter { klass -> klass.hasNoInnerClass() }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
sealed class Sealed<T>
|
||||
|
||||
<caret>class SubSealed<T> : Sealed<T>()
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
sealed class Sealed(val y: Int)
|
||||
|
||||
<caret>class SubSealed(x: Int) : Sealed(x)
|
||||
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
sealed class Sealed
|
||||
|
||||
open <caret>class SubSealed : Sealed()
|
||||
@@ -0,0 +1,9 @@
|
||||
// PROBLEM: none
|
||||
|
||||
sealed class Sealed
|
||||
|
||||
<caret>class SubSealed : Sealed() {
|
||||
companion object {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// PROBLEM: none
|
||||
|
||||
sealed class Sealed
|
||||
|
||||
<caret>class SubSealed : Sealed() {
|
||||
val x: Int = 42
|
||||
|
||||
inner class Inner {
|
||||
fun foo() = x
|
||||
}
|
||||
}
|
||||
+30
@@ -1259,6 +1259,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithoutParentheses.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("generic.kt")
|
||||
public void testGeneric() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nonEmptyConstructor.kt")
|
||||
public void testNonEmptyConstructor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("openSubclass.kt")
|
||||
public void testOpenSubclass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withCompanion.kt")
|
||||
public void testWithCompanion() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withInner.kt")
|
||||
public void testWithInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/copyWithoutNamedArguments")
|
||||
|
||||
Reference in New Issue
Block a user