Skip to content

Commit

Permalink
Remove unnecessary wildcard imports (#1258)
Browse files Browse the repository at this point in the history
Closes #1256

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
  • Loading branch information
paul-dingemans and Paul Dingemans committed Oct 27, 2021
1 parent 27f90a4 commit 3063316
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Do not check for `.idea` folder presence when using `applyToIDEA` globally ([#1186](https://github.com/pinterest/ktlint/issues/1186))
- Remove spaces before primary constructor (`paren-spacing`) ([#1207](https://github.com/pinterest/ktlint/issues/1207))
- Fix false positive for delegated properties with a lambda argument (`indent`) ([#1210](https://github.com/pinterest/ktlint/issues/1210))

- Remove unnecessary wildcard imports (`no-unused-imports`) ([#1256](https://github.com/pinterest/ktlint/issues/1256))
### Changed
- Support absolute paths for globs ([#1131](https://github.com/pinterest/ktlint/issues/1131))
- Fix regression from 0.41 with argument list wrapping after dot qualified expression (`argument-list-wrapping`)([#1159](https://github.com/pinterest/ktlint/issues/1159))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.util.removeSuffixIfPresent

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

Expand Down Expand Up @@ -95,12 +96,12 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
parentExpressions.add(text.substringBeforeLast("("))
}
if (type == KDocTokens.MARKDOWN_LINK && psi is KDocLink) {
val linkText = psi.getLinkText().removeBackticks()
val linkText = psi.getLinkText().removeBackticksAndWildcards()
ref.add(Reference(linkText.split('.').first(), false))
} else if ((type == REFERENCE_EXPRESSION || type == OPERATION_REFERENCE) &&
!vnode.isPartOf(IMPORT_DIRECTIVE)
) {
ref.add(Reference(text.removeBackticks(), psi.parentDotQualifiedExpression() != null))
ref.add(Reference(text.removeBackticksAndWildcards(), psi.parentDotQualifiedExpression() != null))
} else if (type == IMPORT_DIRECTIVE) {
val importPath = (vnode.psi as KtImportDirective).importPath!!
if (!usedImportPaths.add(importPath)) {
Expand All @@ -109,7 +110,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
vnode.psi.delete()
}
} else {
imports += importPath.pathStr.removeBackticks().trim()
imports += importPath.pathStr.removeBackticksAndWildcards().trim()
}
}
}
Expand All @@ -124,8 +125,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()?.removeBackticks()
val importPath = importDirective.importPath?.pathStr?.removeBackticks()!!
val name = importDirective.importPath?.importedName?.asString()?.removeBackticksAndWildcards()
val importPath = importDirective.importPath?.pathStr?.removeBackticksAndWildcards()!!
if (importDirective.aliasName == null &&
(packageName.isEmpty() || importPath.startsWith("$packageName.")) &&
importPath.substring(packageName.length + 1).indexOf('.') == -1
Expand Down Expand Up @@ -188,5 +189,5 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
return (callOrThis.parent as? KtDotQualifiedExpression)?.takeIf { it.selectorExpression == callOrThis }
}

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

@Test
fun `Issue 1256 - remove wildcard import when not used`() {
val rule = NoUnusedImportsRule()
assertThat(
rule.lint(
"""
import abc.*
import def.*
import def.Something
fun foo() = def.Something()
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(1, 1, "no-unused-imports", "Unnecessary import"),
LintError(2, 1, "no-unused-imports", "Unnecessary import")
)
)
}
}

0 comments on commit 3063316

Please sign in to comment.