Replace deprecated symbol usage: do not include 'Companion' in import directives when importing companion object

#KT-34078 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-10-15 13:59:33 +09:00
committed by Dmitry Gridin
parent 5250421002
commit 48968c0fe2
8 changed files with 81 additions and 1 deletions
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver
@@ -160,7 +161,14 @@ class CodeToInlineBuilder(
//TODO: other types of references ('[]' etc)
if (expression.canBeResolvedViaImport(target, bindingContext)) {
codeToInline.fqNamesToImport.add(target.importableFqName!!)
val importableFqName = if (target.isCompanionObject()) {
target.containingDeclaration?.importableFqName
} else {
target.importableFqName
}
if (importableFqName != null) {
codeToInline.fqNamesToImport.add(importableFqName)
}
}
if (expression.getReceiverExpression() == null) {
@@ -0,0 +1,13 @@
// "Replace with 'A.bar(x)'" "true"
package test
import a.A
@Deprecated("bla", ReplaceWith("A.bar(x)", "a.A"))
fun foo(x: Any) {
}
fun test() {
A.bar(1)
}
@@ -0,0 +1,7 @@
package a
class A {
companion object {
fun bar(x: Any) {}
}
}
@@ -0,0 +1,11 @@
// "Replace with 'A.bar(x)'" "true"
package test
@Deprecated("bla", ReplaceWith("A.bar(x)", "a.A"))
fun foo(x: Any) {
}
fun test() {
<caret>foo(1)
}
@@ -0,0 +1,13 @@
// "Replace with 'A.bar(x)'" "true"
package test
import a.A
@Deprecated("bla", ReplaceWith("A.bar(x)", "a.A"))
fun foo(x: Any) {
}
fun test() {
A.bar(1)
}
@@ -0,0 +1,7 @@
package a
class A {
companion object F {
fun bar(x: Any) {}
}
}
@@ -0,0 +1,11 @@
// "Replace with 'A.bar(x)'" "true"
package test
@Deprecated("bla", ReplaceWith("A.bar(x)", "a.A"))
fun foo(x: Any) {
}
fun test() {
<caret>foo(1)
}
@@ -2555,6 +2555,16 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("addImportForCompanionObject.before.Main.kt")
public void testAddImportForCompanionObject() throws Exception {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/imports/addImportForCompanionObject.before.Main.kt");
}
@TestMetadata("addImportForCompanionObject2.before.Main.kt")
public void testAddImportForCompanionObject2() throws Exception {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/imports/addImportForCompanionObject2.before.Main.kt");
}
@TestMetadata("addImportForOperator.before.Main.kt")
public void testAddImportForOperator() throws Exception {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/imports/addImportForOperator.before.Main.kt");