diff options
Diffstat (limited to 'ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt')
-rw-r--r-- | ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt index cbb8287d..b46f04df 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt @@ -12,6 +12,8 @@ import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes class NoUnusedImportsRule : Rule("no-unused-imports") { + private val componentNRegex = Regex("^component\\d+$") + private val operatorSet = setOf( // unary "unaryPlus", "unaryMinus", "not", @@ -34,46 +36,45 @@ 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("*") + private val ref = mutableSetOf<String>() private var packageName = "" - override fun visit(node: ASTNode, autoCorrect: Boolean, - emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { + override fun visit( + node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + ) { if (node.elementType == KtStubElementTypes.FILE) { + ref.clear() // rule can potentially be executed more than once (when formatting) + ref.add("*") node.visit { vnode -> val psi = vnode.psi val type = vnode.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) && + } else if ((type == KtNodeTypes.REFERENCE_EXPRESSION || type == KtNodeTypes.OPERATION_REFERENCE) && !psi.isPartOf(KtImportDirective::class)) { ref.add(vnode.text.trim('`')) } } - } else - if (node.elementType == KtStubElementTypes.PACKAGE_DIRECTIVE) { + } else if (node.elementType == KtStubElementTypes.PACKAGE_DIRECTIVE) { val packageDirective = node.psi as KtPackageDirective packageName = packageDirective.qualifiedName - } else - if (node.elementType == KtStubElementTypes.IMPORT_DIRECTIVE) { + } else if (node.elementType == KtStubElementTypes.IMPORT_DIRECTIVE) { val importDirective = node.psi as KtImportDirective val name = importDirective.importPath?.importedName?.asString() val importPath = importDirective.importPath?.pathStr!! if (importDirective.aliasName == null && - importPath.startsWith(packageName) && + (packageName.isEmpty() || importPath.startsWith("$packageName.")) && importPath.substring(packageName.length + 1).indexOf('.') == -1) { emit(importDirective.startOffset, "Unnecessary import", true) if (autoCorrect) { importDirective.delete() } - } else - if (name != null && !ref.contains(name) && !operatorSet.contains(name)) { + } else if (name != null && !ref.contains(name) && !operatorSet.contains(name) && !name.isComponentN()) { emit(importDirective.startOffset, "Unused import", true) if (autoCorrect) { importDirective.delete() @@ -82,8 +83,5 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { } } - private fun ASTNode.visit(cb: (node: ASTNode) -> Unit) { - cb(this) - this.getChildren(null).forEach { it.visit(cb) } - } + private fun String.isComponentN() = componentNRegex.matches(this) } |