Skip to content

Commit

Permalink
NoUnusedImportsRule: fix false positive when import directive has bac…
Browse files Browse the repository at this point in the history
…kticks and alias (#911)
  • Loading branch information
t-kameyama authored Sep 17, 2020
1 parent e3d7a60 commit 6589349
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.visit
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
Expand Down Expand Up @@ -93,14 +92,14 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
parentExpressions.add(text.substringBeforeLast("("))
}
if (type == KDocTokens.MARKDOWN_LINK && psi is KDocLink) {
val linkText = psi.getLinkText().replace("`", "")
val linkText = psi.getLinkText().removeBackticks()
ref.add(Reference(linkText.split('.').first(), false))
} else if ((type == REFERENCE_EXPRESSION || type == OPERATION_REFERENCE) &&
!vnode.isPartOf(IMPORT_DIRECTIVE)
) {
ref.add(Reference(text.trim('`'), psi.parentDotQualifiedExpression() != null))
ref.add(Reference(text.removeBackticks(), psi.parentDotQualifiedExpression() != null))
} else if (type == IMPORT_DIRECTIVE) {
buildNonRedundantImports(vnode.psi as KtImportDirective)
imports += (vnode.psi as KtImportDirective).importPath!!.pathStr.removeBackticks().trim()
}
}
val directCalls = ref.filter { !it.inDotQualifiedExpression }.map { it.text }
Expand All @@ -114,8 +113,8 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
packageName = packageDirective.qualifiedName
} else if (node.elementType == IMPORT_DIRECTIVE) {
val importDirective = node.psi as KtImportDirective
val name = importDirective.importPath?.importedName?.asString()
val importPath = importDirective.importPath?.pathStr!!
val name = importDirective.importPath?.importedName?.asString()?.removeBackticks()
val importPath = importDirective.importPath?.pathStr?.removeBackticks()!!
if (importDirective.aliasName == null &&
(packageName.isEmpty() || importPath.startsWith("$packageName.")) &&
importPath.substring(packageName.length + 1).indexOf('.') == -1
Expand All @@ -139,16 +138,6 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
}
}

// Builds a list of imports. Takes care of having aliases in case it is assigned to imports
private fun buildNonRedundantImports(import: KtImportDirective) {
if (import.alias != null) {
imports += import.importPath!!.pathStr.replace("`", "").trim()
imports += import.aliasName!!.trim()
} else {
imports += import.importPath!!.pathStr.trim()
}
}

// Checks if any static method call is present in code with the parent import present in imports list
private fun checkIfExpressionHasParentImport(text: String, type: IElementType): Boolean {
val containsMethodCall = text.split(".").last().contains("(")
Expand Down Expand Up @@ -187,4 +176,6 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
val callOrThis = (parent as? KtCallExpression)?.takeIf { it.calleeExpression == this } ?: this
return (callOrThis.parent as? KtDotQualifiedExpression)?.takeIf { it.selectorExpression == callOrThis }
}

private fun String.removeBackticks() = replace("`", "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,19 @@ class NoUnusedImportsRuleTest {
)
).isEmpty()
}

@Test
fun `import directive has backticks and alias`() {
assertThat(
NoUnusedImportsRule().lint(
"""
import com.test.`object`.Producer as Producer1
class Consumer(producer1: Producer1<String>) {
val x = 1
}
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit 6589349

Please sign in to comment.