From 10cc4959ccc609bd5480b6f943032c1cdf0d8351 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 22 Nov 2018 18:10:56 +0300 Subject: [PATCH] Forbid "move property to constructor" for expect classes #KT-28381 Fixed --- .../idea/intentions/MovePropertyToConstructorIntention.kt | 7 ++++--- .../intentions/movePropertyToConstructor/expectClass.kt | 8 ++++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/movePropertyToConstructor/expectClass.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToConstructorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToConstructorIntention.kt index 0186c483377..bbcc532a182 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToConstructorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToConstructorIntention.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.idea.util.isExpectDeclaration import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens.LATEINIT_KEYWORD import org.jetbrains.kotlin.lexer.KtTokens.VARARG_KEYWORD @@ -54,9 +55,9 @@ class MovePropertyToConstructorIntention : } override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean { - fun KtProperty.isDeclaredInClass(): Boolean { + fun KtProperty.isDeclaredInSupportedClass(): Boolean { val parent = getStrictParentOfType() - return parent is KtClass && !parent.isInterface() + return parent is KtClass && !parent.isInterface() && !parent.isExpectDeclaration() } return !element.isLocal @@ -64,7 +65,7 @@ class MovePropertyToConstructorIntention : && element.getter == null && element.setter == null && !element.hasModifier(LATEINIT_KEYWORD) - && (element.isDeclaredInClass()) + && (element.isDeclaredInSupportedClass()) && (element.initializer?.isValidInConstructor() ?: true) } diff --git a/idea/testData/intentions/movePropertyToConstructor/expectClass.kt b/idea/testData/intentions/movePropertyToConstructor/expectClass.kt new file mode 100644 index 00000000000..e8f4c34d43e --- /dev/null +++ b/idea/testData/intentions/movePropertyToConstructor/expectClass.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false +// ERROR: The feature "multi platform projects" is experimental and should be enabled explicitly + +expect class Outer { + class Nested { + val x: Int + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ca603f1e801..430b5c61e7a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -12020,6 +12020,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/movePropertyToConstructor/delegated.kt"); } + @TestMetadata("expectClass.kt") + public void testExpectClass() throws Exception { + runTest("idea/testData/intentions/movePropertyToConstructor/expectClass.kt"); + } + @TestMetadata("functionReference.kt") public void testFunctionReference() throws Exception { runTest("idea/testData/intentions/movePropertyToConstructor/functionReference.kt");