Refactor sealed subclass to object: add tests, fix type parameter case

This commit is contained in:
Mikhail Glukhikh
2018-04-11 14:11:06 +03:00
parent df7968678a
commit e4275cc027
7 changed files with 70 additions and 0 deletions
@@ -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>()
@@ -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
}
}
@@ -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")