Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not remove import which is used as markdown link in KDoc only #1292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Fix NoSuchElementException for property accessor (`trailing-comma`) ([#1280](https://github.com/pinterest/ktlint/issues/1280))
- Fix ClassCastException using ktlintFormat on class with KDoc (`no-trailing-spaces`) ([#1270](https://github.com/pinterest/ktlint/issues/1270))
- Do not remove trailing comma in annotation ([#1297](https://github.com/pinterest/ktlint/issues/1297))
- Do not remove import which is used as markdown link in KDoc only (`no-unused-imports`) ([#1282](https://github.com/pinterest/ktlint/issues/1282))

### Changed
- Update Kotlin version to `1.6.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
if (type == KDocTokens.MARKDOWN_LINK && psi is KDocLink) {
val linkText = psi.getLinkText().removeBackticksAndWildcards()
ref.add(Reference(linkText.split('.').first(), false))
ref.add(Reference(linkText.split('.').last(), false))
} else if ((type == REFERENCE_EXPRESSION || type == OPERATION_REFERENCE) &&
!vnode.isPartOf(IMPORT_DIRECTIVE)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ class NoUnusedImportsRuleTest {
* [DRef] DRef2
* [O.method]
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
* [PDRef3](p.DRef3) p.PDRef4 PDRef5 Note that [PDRef3](p.DRef3) is not recognized as markdown link because of "](" and
* of such "p.DRef3" is marked as unused import
* [] 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"),
Expand Down Expand Up @@ -411,7 +411,8 @@ class NoUnusedImportsRuleTest {
/**
* [DRef] DRef2
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
* [PDRef3](p.DRef3) p.PDRef4 PDRef5 Note that [PDRef3](p.DRef3) is not recognized as markdown link because of "](" and
* of such "p.DRef3" is marked as unused import
*/
fun main() {}
""".trimIndent()
Expand All @@ -421,11 +422,13 @@ class NoUnusedImportsRuleTest {
package kdoc

import DRef
import p.PDRef

/**
* [DRef] DRef2
* [p.PDRef] p.PDRef2
* [PDRef3](p.DRef3) p.PDRef4 PDRef5
* [PDRef3](p.DRef3) p.PDRef4 PDRef5 Note that [PDRef3](p.DRef3) is not recognized as markdown link because of "](" and
* of such "p.DRef3" is marked as unused import
*/
fun main() {}
""".trimIndent()
Expand Down Expand Up @@ -860,4 +863,21 @@ class NoUnusedImportsRuleTest {
)
)
}

@Test
fun `Issue 1282 - do not remove import when used in kdoc only`() {
val rule = NoUnusedImportsRule()
assertThat(
rule.lint(
"""
import some.pkg.returnSelf

/**
* Do not forget that you can also return string via [String.returnSelf]
*/
fun test() {}
""".trimIndent()
)
).isEmpty()
}
}