[IDE] Update SuperClassNotInitialized to relaxed restrictions on sealed classes

This commit is contained in:
Dmitriy Novozhilov
2021-01-14 09:57:26 +03:00
parent 16e0c0b820
commit a9ef8bb32f
6 changed files with 26 additions and 19 deletions
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
@@ -52,10 +53,10 @@ object SuperClassNotInitialized : KotlinIntentionActionsFactory() {
val superClass = (type.constructor.declarationDescriptor as? ClassDescriptor) ?: return emptyList()
val classDescriptor = classOrObjectDeclaration.resolveToDescriptorIfAny(BodyResolveMode.FULL) ?: return emptyList()
val containingPackage = superClass.containingDeclaration as? PackageFragmentDescriptor
val inSameFile = containingPackage == classDescriptor.containingDeclaration
val containingPackage = superClass.classId?.packageFqName
val inSamePackage = containingPackage != null && containingPackage == classDescriptor.classId?.packageFqName
val constructors = superClass.constructors.filter {
it.isVisible(classDescriptor) || (superClass.modality == Modality.SEALED && inSameFile)
it.isVisible(classDescriptor) && (superClass.modality != Modality.SEALED || inSamePackage && classDescriptor.visibility != DescriptorVisibilities.LOCAL)
}
if (constructors.isEmpty() && (!superClass.isExpect || superClass.kind != ClassKind.CLASS)) {
return emptyList() // no accessible constructor
@@ -6,4 +6,4 @@ sealed class A
fun test() {
class B : A<caret>
}
}
@@ -1,10 +0,0 @@
// "Change to constructor invocation" "false"
// ACTION: Introduce import alias
// ERROR: This type has a constructor, and thus must be initialized here
// ERROR: This type is sealed, so it can be inherited by only its own nested classes or objects
class My {
sealed class A
class B : A<caret>
}
@@ -0,0 +1,8 @@
// "Change to constructor invocation" "true"
// ACTION: Introduce import alias
class My {
sealed class A
class B : A<caret>
}
@@ -0,0 +1,8 @@
// "Change to constructor invocation" "true"
// ACTION: Introduce import alias
class My {
sealed class A
class B : A<caret>()
}
@@ -12509,11 +12509,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/supertypeInitialization/addParenthesisForInvalidSealedClass.kt");
}
@TestMetadata("addParenthesisForInvalidSealedClass2.kt")
public void testAddParenthesisForInvalidSealedClass2() throws Exception {
runTest("idea/testData/quickfix/supertypeInitialization/addParenthesisForInvalidSealedClass2.kt");
}
@TestMetadata("addParenthesisForLocalClass.kt")
public void testAddParenthesisForLocalClass() throws Exception {
runTest("idea/testData/quickfix/supertypeInitialization/addParenthesisForLocalClass.kt");
@@ -12529,6 +12524,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/supertypeInitialization/addParenthesisForSealedClass.kt");
}
@TestMetadata("addParenthesisForSealedClass2.kt")
public void testAddParenthesisForSealedClass2() throws Exception {
runTest("idea/testData/quickfix/supertypeInitialization/addParenthesisForSealedClass2.kt");
}
public void testAllFilesPresentInSupertypeInitialization() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/supertypeInitialization"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}