Remove redundant let: rename invoke call

#KT-29556 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-01 19:47:47 +09:00
committed by Mikhail Glukhikh
parent df3953f03a
commit e0aeb8f7ec
10 changed files with 98 additions and 1 deletions
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.getReferenceTargets
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -202,7 +203,12 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
parameterDescriptor.destructuringVariables.associate { it.name to it }
else
mapOf(parameterDescriptor.name to parameterDescriptor)
return callExpression.valueArguments.flatMap { arg ->
val callee = (callExpression.calleeExpression as? KtNameReferenceExpression)?.let {
val descriptor = variableDescriptorByName[it.getReferencedNameAsName()]
if (descriptor != null && it.getReferenceTargets(context).singleOrNull() == descriptor) listOf(it) else null
} ?: emptyList()
return callee + callExpression.valueArguments.flatMap { arg ->
arg.collectDescendantsOfType<KtNameReferenceExpression>().filter {
val descriptor = variableDescriptorByName[it.getReferencedNameAsName()]
descriptor != null && it.getResolvedCall(context)?.resultingDescriptor == descriptor
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a.<caret>let { it() }
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a.<caret>let { b -> b() }
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a.<caret>let { b -> b.invoke() }
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
a.invoke()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
class A {
operator fun invoke(a: A, i: Int) {}
}
fun foo(a: A) {
a.<caret>let { it(it, 1) }
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
class A {
operator fun invoke() {}
}
fun foo(a: A) {
(1 to a).<caret>let { (i, b) -> b() }
}
@@ -15204,6 +15204,31 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/inWithRangeMultipleParam.kt");
}
@TestMetadata("invokeCall.kt")
public void testInvokeCall() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/invokeCall.kt");
}
@TestMetadata("invokeCall2.kt")
public void testInvokeCall2() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/invokeCall2.kt");
}
@TestMetadata("invokeCall3.kt")
public void testInvokeCall3() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/invokeCall3.kt");
}
@TestMetadata("invokeCall4.kt")
public void testInvokeCall4() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/invokeCall4.kt");
}
@TestMetadata("invokeCall5.kt")
public void testInvokeCall5() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/invokeCall5.kt");
}
@TestMetadata("lambdaWithBinaryExpression.kt")
public void testLambdaWithBinaryExpression() throws Exception {
runTest("idea/testData/intentions/replaceSingleLineLetIntention/lambdaWithBinaryExpression.kt");