Skip to content

Commit

Permalink
Revert "Fix unused import when import is duplicated (pinterest#1129)"
Browse files Browse the repository at this point in the history
This reverts commit 3a43dbb.
  • Loading branch information
shashachu authored and romtsn committed Aug 8, 2021
1 parent a828740 commit 66ddc1c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.resolve.ImportPath

class NoUnusedImportsRule : Rule("no-unused-imports") {

Expand Down Expand Up @@ -71,7 +70,6 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
private val ref = mutableSetOf<Reference>()
private val parentExpressions = mutableSetOf<String>()
private val imports = mutableSetOf<String>()
private val usedImportPaths = mutableSetOf<ImportPath>()
private var packageName = ""
private var rootNode: ASTNode? = null

Expand Down Expand Up @@ -101,11 +99,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
) {
ref.add(Reference(text.removeBackticks(), psi.parentDotQualifiedExpression() != null))
} else if (type == IMPORT_DIRECTIVE) {
val importPath = (vnode.psi as KtImportDirective).importPath!!
if (!usedImportPaths.add(importPath)) {
emit(vnode.startOffset, "Unused import", false)
}
imports += importPath.pathStr.removeBackticks().trim()
imports += (vnode.psi as KtImportDirective).importPath!!.pathStr.removeBackticks().trim()
}
}
val directCalls = ref.filter { !it.inDotQualifiedExpression }.map { it.text }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,88 +192,6 @@ class NoUnusedImportsRuleTest {
)
}

@Test
fun testRepeatedImport() {
assertThat(
NoUnusedImportsRule().lint(
"""
import org.repository.RepositoryPolicy
import org.repository.any
import org.repository.all
import org.repository.any
fun main() {
RepositoryPolicy(
any(false), all("trial")
)
}
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(6, 1, "no-unused-imports", "Unused import")
)
)
}

@Test
fun testRepeatedImportTwice() {
assertThat(
NoUnusedImportsRule().lint(
"""
import org.repository.RepositoryPolicy
import org.repository.any
import org.repository.all
import org.repository.any
import org.repository.any
fun main() {
RepositoryPolicy(
any(false), all("trial")
)
}
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(6, 1, "no-unused-imports", "Unused import"),
LintError(7, 1, "no-unused-imports", "Unused import")

)
)
}

@Test
fun testRepeatedImportsWithDistinctIncidences() {
assertThat(
NoUnusedImportsRule().lint(
"""
import org.repository.RepositoryPolicy
import org.repository.any
import org.repository.all
import org.repository.many
import org.repository.any
import org.repository.none
import org.repository.all
fun main() {
RepositoryPolicy(
any(false), all("trial"), many("hello"), none("goodbye")
)
}
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(7, 1, "no-unused-imports", "Unused import"),
LintError(9, 1, "no-unused-imports", "Unused import")
)
)
}

@Test
fun testFormat() {
assertThat(
Expand Down

0 comments on commit 66ddc1c

Please sign in to comment.