Skip to content

Commit

Permalink
Merge pull request #142 from JelloRanger/jelloranger/fix-destructure-…
Browse files Browse the repository at this point in the history
…operator-unused-import

Fix erroneous unused import flagging on custom destructure operators
  • Loading branch information
shyiko committed Jan 28, 2018
2 parents b381170 + 4d13e87 commit 701c0b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes

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

private val destructureOperator = Regex("(?:component)[\\d]+\\b")

private val operatorSet = setOf(
// unary
"unaryPlus", "unaryMinus", "not",
Expand All @@ -34,9 +36,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
// iteration (https://github.com/shyiko/ktlint/issues/40)
"iterator",
// by (https://github.com/shyiko/ktlint/issues/54)
"getValue", "setValue",
// destructuring assignment
"component1", "component2", "component3", "component4", "component5"
"getValue", "setValue"
)
private val ref = mutableSetOf<String>()
private var packageName = ""
Expand Down Expand Up @@ -74,7 +74,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
if (autoCorrect) {
importDirective.delete()
}
} else if (name != null && !ref.contains(name) && !operatorSet.contains(name)) {
} else if (name != null && !ref.contains(name) && !operatorSet.contains(name) && !destructureOperator.matches(name)) {
emit(importDirective.startOffset, "Unused import", true)
if (autoCorrect) {
importDirective.delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ class NoUnusedImportsRuleTest {
)).isEmpty()
}

@Test
fun testDestructureOperatorLint() {
assertThat(NoUnusedImportsRule().lint(
"""
import p.component6
fun main() {
val (one, two, three, four, five, six) = someList
}
""".trimIndent()
)).isEmpty()
assertThat(NoUnusedImportsRule().lint(
"""
import p.component6
import p.component2
import p.component100
import p.component
import p.component12woohoo
fun main() {
val (one, two, three, four, five, six) = someList
}
""".trimIndent()
)).isEqualTo(listOf(
LintError(4, 1, "no-unused-imports", "Unused import"),
LintError(5, 1, "no-unused-imports", "Unused import")
))
}

@Test
fun testLintKDocLinkImport() {
assertThat(NoUnusedImportsRule().lint(
Expand Down

0 comments on commit 701c0b5

Please sign in to comment.