Improve: add more cases for MoveVariableDeclarationIntoWhen

#KT-30499 Fixed
This commit is contained in:
Dmitry Gridin
2019-03-21 16:32:41 +07:00
parent c84ff1d8b1
commit e11072b8c2
11 changed files with 120 additions and 1 deletions
@@ -60,10 +60,26 @@ private fun KtProperty.action(element: KtElement): Action = when (val elementUsa
}
private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
val previousProperty = previousStatement() as? KtProperty ?: return null
val previousProperty = previousStatement() as? KtProperty
?: previousPropertyFromParent()
?: return null
return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text }
}
private tailrec fun KtExpression.previousPropertyFromParent(): KtProperty? {
val parentExpression = parent as? KtExpression ?: return null
if (this != when (parentExpression) {
is KtProperty -> parentExpression.initializer
is KtReturnExpression -> parentExpression.returnedExpression
is KtBinaryExpression -> parentExpression.left
is KtUnaryExpression -> parentExpression.baseExpression
else -> null
}
) return null
return parentExpression.previousStatement() as? KtProperty ?: parentExpression.previousPropertyFromParent()
}
private class VariableDeclarationIntoWhenFix(
private val actionName: String,
private val subjectExpressionPointer: SmartPsiElementPointer<KtExpression>,
@@ -0,0 +1,9 @@
fun test() = true
fun foo(): Int {
val a<caret> = test()
return when (a) {
true -> 42
else -> null
} ?: 55
}
@@ -0,0 +1,8 @@
fun test() = true
fun foo(): Int {
return when (test()) {
true -> 42
else -> null
} ?: 55
}
@@ -0,0 +1,9 @@
fun test() = 42
fun foo() {
val a<caret> = test()
val b = when (a) {
1 -> a
else -> 24
}
}
@@ -0,0 +1,8 @@
fun test() = 42
fun foo() {
val b = when (val a = test()) {
1 -> a
else -> 24
}
}
@@ -0,0 +1,9 @@
fun test() = 42
fun foo(): Int {
val a<caret> = test()
return when (a) {
1 -> a
else -> 24
}
}
@@ -0,0 +1,8 @@
fun test() = 42
fun foo(): Int {
return when (val a = test()) {
1 -> a
else -> 24
}
}
@@ -0,0 +1,9 @@
fun test() = true
fun foo() {
val a<caret> = test()
val b = !when (a) {
true -> true
else -> false
}
}
@@ -0,0 +1,8 @@
fun test() = true
fun foo() {
val b = !when (test()) {
true -> true
else -> false
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun test() = true
fun foo(): Int {
val a<caret> = test()
return null ?: when (a) {
true -> 42
else -> 5
}
}
@@ -4357,6 +4357,31 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("inBinaryExpression.kt")
public void testInBinaryExpression() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt");
}
@TestMetadata("inProperty.kt")
public void testInProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inProperty.kt");
}
@TestMetadata("inReturnExpression.kt")
public void testInReturnExpression() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inReturnExpression.kt");
}
@TestMetadata("inUnaryAndProperty.kt")
public void testInUnaryAndProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt");
}
@TestMetadata("notApplicableInBinaryExpression.kt")
public void testNotApplicableInBinaryExpression() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableInBinaryExpression.kt");
}
@TestMetadata("notApplicableOtherName.kt")
public void testNotApplicableOtherName() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");