Skip to content

Commit

Permalink
Fixed Triggering of "Unused import" when element is referenced in KDo…
Browse files Browse the repository at this point in the history
…c(s) only (#46)
  • Loading branch information
shyiko committed May 29, 2017
1 parent fbede69 commit 357a956
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.github.shyiko.ktlint.ruleset.standard
import com.github.shyiko.ktlint.core.Rule
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.psiUtil.startOffset
Expand Down Expand Up @@ -39,9 +41,14 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
if (node.elementType == KtStubElementTypes.FILE) {
node.visit { node ->
val psi = node.psi
val type = node.elementType
if (type == KDocTokens.MARKDOWN_LINK && psi is KDocLink) {
val linkText = psi.getLinkText().replace("`", "")
ref.add(linkText.split('.').first())
} else
if ((type == KtNodeTypes.REFERENCE_EXPRESSION || type == KtNodeTypes.OPERATION_REFERENCE) &&
!node.psi.isPartOf(KtImportDirective::class)) {
!psi.isPartOf(KtImportDirective::class)) {
ref.add(node.text.trim('`'))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ class NoUnusedImportsRuleTest {
)).isEmpty()
}

@Test
fun testLintKDocLinkImport() {
assertThat(NoUnusedImportsRule().lint(
"""
package kdoc
import DRef
import p.PDRef
import DRef2
import p.PDRef2
import p.DRef3
import p.PDRef3
import p.PDRef4
import p.PDRef5
import p.O
/**
* [DRef] DRef2
* [O.method]
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
* [] text
*/
fun main() {}
""".trimIndent()
)).isEqualTo(listOf(
LintError(4, 1, "no-unused-imports", "Unused import"),
LintError(5, 1, "no-unused-imports", "Unused import"),
LintError(6, 1, "no-unused-imports", "Unused import"),
LintError(7, 1, "no-unused-imports", "Unused import"),
LintError(8, 1, "no-unused-imports", "Unused import"),
LintError(9, 1, "no-unused-imports", "Unused import"),
LintError(10, 1, "no-unused-imports", "Unused import")
))
}

@Test
fun testSamePackageImport() {
assertThat(NoUnusedImportsRule().lint(
Expand Down Expand Up @@ -123,4 +159,42 @@ class NoUnusedImportsRuleTest {
)
}

@Test
fun testFormatKDocLinkImport() {
assertThat(NoUnusedImportsRule().format(
"""
package kdoc
import DRef
import p.PDRef
import DRef2
import p.PDRef2
import p.DRef3
import p.PDRef3
import p.PDRef4
import p.PDRef5
/**
* [DRef] DRef2
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
*/
fun main() {}
""".trimIndent()
)).isEqualTo(
"""
package kdoc
import DRef
/**
* [DRef] DRef2
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
*/
fun main() {}
""".trimIndent()
)
}

}

0 comments on commit 357a956

Please # to comment.