KTIJ-26423 [AA] Remove KtImportOptimizerResult.unusedImports
This information is computed on the `intellij` side, and no there is no code left in `kotlin` which still requires it
This commit is contained in:
+1
-44
@@ -59,52 +59,9 @@ internal class KtFirImportOptimizer(
|
|||||||
if (existingImports.isEmpty()) return KtImportOptimizerResult()
|
if (existingImports.isEmpty()) return KtImportOptimizerResult()
|
||||||
|
|
||||||
val firFile = file.getOrBuildFirFile(firResolveSession).apply { lazyResolveToPhaseRecursively(FirResolvePhase.BODY_RESOLVE) }
|
val firFile = file.getOrBuildFirFile(firResolveSession).apply { lazyResolveToPhaseRecursively(FirResolvePhase.BODY_RESOLVE) }
|
||||||
|
|
||||||
val explicitlyImportedFqNames = existingImports
|
|
||||||
.asSequence()
|
|
||||||
.mapNotNull { it.importPath }
|
|
||||||
.filter { !it.isAllUnder && !it.hasAlias() }
|
|
||||||
.map { it.fqName }
|
|
||||||
.toSet()
|
|
||||||
|
|
||||||
val (usedDeclarations, unresolvedNames) = collectReferencedEntities(firFile)
|
val (usedDeclarations, unresolvedNames) = collectReferencedEntities(firFile)
|
||||||
|
|
||||||
// TODO remove unused imports computing code completely
|
return KtImportOptimizerResult(usedDeclarations, unresolvedNames)
|
||||||
val referencesEntities = usedDeclarations
|
|
||||||
.filterNot { (fqName, referencedByNames) ->
|
|
||||||
val fromCurrentPackage = fqName.parentOrNull() == file.packageFqName
|
|
||||||
val noAliasedImports = referencedByNames.singleOrNull() == fqName.shortName()
|
|
||||||
|
|
||||||
fromCurrentPackage && noAliasedImports
|
|
||||||
}
|
|
||||||
|
|
||||||
val requiredStarImports = referencesEntities.keys
|
|
||||||
.asSequence()
|
|
||||||
.filterNot { it in explicitlyImportedFqNames }
|
|
||||||
.mapNotNull { it.parentOrNull() }
|
|
||||||
.filterNot { it.isRoot }
|
|
||||||
.toSet()
|
|
||||||
|
|
||||||
val unusedImports = mutableSetOf<KtImportDirective>()
|
|
||||||
val alreadySeenImports = mutableSetOf<ImportPath>()
|
|
||||||
|
|
||||||
for (import in existingImports) {
|
|
||||||
val importPath = import.importPath ?: continue
|
|
||||||
|
|
||||||
val isUsed = when {
|
|
||||||
importPath.importedName in unresolvedNames -> true
|
|
||||||
!alreadySeenImports.add(importPath) -> false
|
|
||||||
importPath.isAllUnder -> unresolvedNames.isNotEmpty() || importPath.fqName in requiredStarImports
|
|
||||||
importPath.fqName in referencesEntities -> importPath.importedName in referencesEntities.getValue(importPath.fqName)
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isUsed) {
|
|
||||||
unusedImports += import
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return KtImportOptimizerResult(unusedImports, usedDeclarations, unresolvedNames)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class ReferencedEntitiesResult(
|
private data class ReferencedEntitiesResult(
|
||||||
|
|||||||
-3
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
|||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.KtImportDirective
|
|
||||||
|
|
||||||
public abstract class KtImportOptimizer : KtLifetimeOwner {
|
public abstract class KtImportOptimizer : KtLifetimeOwner {
|
||||||
public abstract fun analyseImports(file: KtFile): KtImportOptimizerResult
|
public abstract fun analyseImports(file: KtFile): KtImportOptimizerResult
|
||||||
@@ -30,8 +29,6 @@ public interface KtImportOptimizerMixIn : KtAnalysisSessionMixIn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class KtImportOptimizerResult(
|
public class KtImportOptimizerResult(
|
||||||
@Deprecated("Compute unused import on the IntelliJ side instead, see org.jetbrains.kotlin.idea.base.analysis.api.utils.computeUnusedImports")
|
|
||||||
public val unusedImports: Set<KtImportDirective> = emptySet(),
|
|
||||||
public val usedDeclarations: Map<FqName, Set<Name>> = emptyMap(),
|
public val usedDeclarations: Map<FqName, Set<Name>> = emptyMap(),
|
||||||
public val unresolvedNames: Set<Name> = emptySet(),
|
public val unresolvedNames: Set<Name> = emptySet(),
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user