KT-62676 [AA] Collect redundant this qualifiers in reference shortener

^KT-62676 Fixed
This commit is contained in:
Roman Golyshev
2023-10-27 16:30:13 +02:00
committed by teamcity
parent 4ca1f9d492
commit b0f15451fd
31 changed files with 412 additions and 4 deletions
@@ -102,6 +102,7 @@ internal class KtFirReferenceShortener(
//TODO: collect all usages of available symbols in the file and prevent importing symbols that could introduce name clashes, which
// may alter the meaning of existing code.
val collector = ElementsToShortenCollector(
shortenOptions,
context,
towerContext,
selection,
@@ -404,6 +405,7 @@ private class ShortenKDocQualifier(
) : ElementToShorten()
private class ElementsToShortenCollector(
private val shortenOptions: ShortenOptions,
private val shorteningContext: FirShorteningContext,
private val towerContextProvider: FirTowerContextProvider,
private val selection: TextRange,
@@ -1138,11 +1140,13 @@ private class ElementsToShortenCollector(
}
private fun canBePossibleToDropReceiver(qualifiedAccess: FirQualifiedAccessExpression): Boolean {
// we can remove receiver only if it is a qualifier
if (qualifiedAccess.explicitReceiver !is FirResolvedQualifier) return false
return when {
qualifiedAccess.explicitReceiver is FirThisReceiverExpression -> shortenOptions.removeThis
// if there is no extension receiver necessary, then it can be removed
return qualifiedAccess.extensionReceiver == null
qualifiedAccess.explicitReceiver is FirResolvedQualifier -> qualifiedAccess.extensionReceiver == null
else -> false
}
}
private fun findUnambiguousReferencedCallableId(namedReference: FirNamedReference): FirCallableSymbol<*>? {
@@ -72,6 +72,16 @@ public class FirIdeNormalAnalysisScriptSourceModuleReferenceShortenerTestGenerat
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference")
@TestDataPath("$PROJECT_ROOT")
public class ThisReference {
@Test
public void testAllFilesPresentInThisReference() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference"), Pattern.compile("^(.+)\\.kts$"), null, true);
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/typeParameters")
@TestDataPath("$PROJECT_ROOT")
@@ -816,6 +816,94 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference")
@TestDataPath("$PROJECT_ROOT")
public class ThisReference {
@Test
public void testAllFilesPresentInThisReference() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("this_extensionFunction_fromExtensionFunction.kt")
public void testThis_extensionFunction_fromExtensionFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_extensionFunction_fromExtensionFunction.kt");
}
@Test
@TestMetadata("this_extensionFunction_fromExtensionFunction_conflictWithLocal.kt")
public void testThis_extensionFunction_fromExtensionFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_extensionFunction_fromExtensionFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberFunction_fromMemberFunction.kt")
public void testThis_memberFunction_fromMemberFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberFunction_fromMemberFunction.kt");
}
@Test
@TestMetadata("this_memberFunction_fromMemberFunction_conflictWithLocal.kt")
public void testThis_memberFunction_fromMemberFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberFunction_fromMemberFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberProperty_fromInit.kt")
public void testThis_memberProperty_fromInit() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromInit.kt");
}
@Test
@TestMetadata("this_memberProperty_fromInit_conflictWithParameter.kt")
public void testThis_memberProperty_fromInit_conflictWithParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromInit_conflictWithParameter.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction.kt")
public void testThis_memberProperty_fromMemberFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunctionWithContextReceiver.kt")
public void testThis_memberProperty_fromMemberFunctionWithContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunctionWithContextReceiver.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_conflictWithLocal.kt")
public void testThis_memberProperty_fromMemberFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_conflictWithParameter.kt")
public void testThis_memberProperty_fromMemberFunction_conflictWithParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_conflictWithParameter.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_noConflictWithLocal.kt")
public void testThis_memberProperty_fromMemberFunction_noConflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_noConflictWithLocal.kt");
}
@Test
@TestMetadata("this_nullableReceiver.kt")
public void testThis_nullableReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_nullableReceiver.kt");
}
@Test
@TestMetadata("this_safeCall.kt")
public void testThis_safeCall() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_safeCall.kt");
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/typeParameters")
@TestDataPath("$PROJECT_ROOT")
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.analysis.api.impl.base.test.cases.references
import org.jetbrains.kotlin.analysis.api.components.ShortenOptions
import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.references.ShorteningResultsRenderer.renderShorteningResults
import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiBasedSingleModuleTest
@@ -30,6 +31,7 @@ abstract class AbstractReferenceShortenerTest : AbstractAnalysisApiBasedSingleMo
ShortenStrategy.values().map { option ->
val shorteningsForOption = collectPossibleReferenceShorteningsInElement(
element,
shortenOptions = ShortenOptions.ALL_ENABLED,
classShortenStrategy = { option },
callableShortenStrategy = { option }
)
@@ -816,6 +816,94 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference")
@TestDataPath("$PROJECT_ROOT")
public class ThisReference {
@Test
public void testAllFilesPresentInThisReference() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("this_extensionFunction_fromExtensionFunction.kt")
public void testThis_extensionFunction_fromExtensionFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_extensionFunction_fromExtensionFunction.kt");
}
@Test
@TestMetadata("this_extensionFunction_fromExtensionFunction_conflictWithLocal.kt")
public void testThis_extensionFunction_fromExtensionFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_extensionFunction_fromExtensionFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberFunction_fromMemberFunction.kt")
public void testThis_memberFunction_fromMemberFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberFunction_fromMemberFunction.kt");
}
@Test
@TestMetadata("this_memberFunction_fromMemberFunction_conflictWithLocal.kt")
public void testThis_memberFunction_fromMemberFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberFunction_fromMemberFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberProperty_fromInit.kt")
public void testThis_memberProperty_fromInit() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromInit.kt");
}
@Test
@TestMetadata("this_memberProperty_fromInit_conflictWithParameter.kt")
public void testThis_memberProperty_fromInit_conflictWithParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromInit_conflictWithParameter.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction.kt")
public void testThis_memberProperty_fromMemberFunction() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunctionWithContextReceiver.kt")
public void testThis_memberProperty_fromMemberFunctionWithContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunctionWithContextReceiver.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_conflictWithLocal.kt")
public void testThis_memberProperty_fromMemberFunction_conflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_conflictWithLocal.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_conflictWithParameter.kt")
public void testThis_memberProperty_fromMemberFunction_conflictWithParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_conflictWithParameter.kt");
}
@Test
@TestMetadata("this_memberProperty_fromMemberFunction_noConflictWithLocal.kt")
public void testThis_memberProperty_fromMemberFunction_noConflictWithLocal() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_memberProperty_fromMemberFunction_noConflictWithLocal.kt");
}
@Test
@TestMetadata("this_nullableReceiver.kt")
public void testThis_nullableReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_nullableReceiver.kt");
}
@Test
@TestMetadata("this_safeCall.kt")
public void testThis_safeCall() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/thisReference/this_safeCall.kt");
}
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/typeParameters")
@TestDataPath("$PROJECT_ROOT")
@@ -0,0 +1,9 @@
package test
class Foo
fun Foo.one() {}
fun Foo.usage() {
<expr>this.one()</expr>
}
@@ -0,0 +1,8 @@
Before shortening: this.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one()
with SHORTEN_AND_IMPORT:
[qualifier] this.one()
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one()
@@ -0,0 +1,11 @@
package test
class Foo
fun Foo.one() {}
fun Foo.usage() {
fun one() {}
<expr>this.one()</expr>
}
@@ -0,0 +1,5 @@
Before shortening: this.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,9 @@
package test
class Foo {
fun one() {}
fun usage() {
<expr>this.one()</expr>
}
}
@@ -0,0 +1,8 @@
Before shortening: this.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one()
with SHORTEN_AND_IMPORT:
[qualifier] this.one()
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one()
@@ -0,0 +1,11 @@
package test
class Foo {
fun one() {}
fun usage() {
fun one() {}
<expr>this.one()</expr>
}
}
@@ -0,0 +1,5 @@
Before shortening: this.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,9 @@
package test
class Foo {
val one: Int
init {
<expr>this.one</expr> = 10
}
}
@@ -0,0 +1,8 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one
with SHORTEN_AND_IMPORT:
[qualifier] this.one
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one
@@ -0,0 +1,9 @@
package test
class Foo(one: Int) {
val one: Int
init {
<expr>this.one</expr> = one
}
}
@@ -0,0 +1,5 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,9 @@
package test
class Foo {
val one: Int = 10
fun usage() {
<expr>this.one</expr>
}
}
@@ -0,0 +1,8 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one
with SHORTEN_AND_IMPORT:
[qualifier] this.one
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one
@@ -0,0 +1,14 @@
package test
class Foo {
val one: Int = 10
context(Context)
fun usage() {
<expr>this.one</expr>
}
}
class Context {
val one: Int = 20
}
@@ -0,0 +1,8 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one
with SHORTEN_AND_IMPORT:
[qualifier] this.one
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one
@@ -0,0 +1,11 @@
package test
class Foo {
val one: Int = 10
fun usage() {
val one = 20
<expr>this.one</expr>
}
}
@@ -0,0 +1,5 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,9 @@
package test
class Foo {
val one: Int = 10
fun usage(one: Int) {
<expr>this.one</expr>
}
}
@@ -0,0 +1,5 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,11 @@
package test
class Foo {
val one: Int = 10
fun usage() {
<expr>this.one</expr>
val one = 20
}
}
@@ -0,0 +1,8 @@
Before shortening: this.one
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one
with SHORTEN_AND_IMPORT:
[qualifier] this.one
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one
@@ -0,0 +1,9 @@
package test
class Foo
fun Foo?.one() {}
fun Foo?.usage() {
<expr>this.one()</expr>
}
@@ -0,0 +1,8 @@
Before shortening: this.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] this.one()
with SHORTEN_AND_IMPORT:
[qualifier] this.one()
with SHORTEN_AND_STAR_IMPORT:
[qualifier] this.one()
@@ -0,0 +1,9 @@
package test
class Foo
fun Foo.one() {}
fun Foo?.usage() {
<expr>this?.one()</expr>
}
@@ -0,0 +1,5 @@
Before shortening: this?.one()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT: