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

Fix unused import correction #2562

Merged
merged 1 commit into from
Jan 16, 2019
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

#### Experimental

* None.
* Fix `unused_import` correction deleting unrelated ranges when there are
multiple violations in a single file.
[JP Simard](https://github.com/jpsim)
[#2561](https://github.com/realm/SwiftLint/issues/2561)

#### Enhancements

Expand Down
12 changes: 11 additions & 1 deletion Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -21804,11 +21804,21 @@ class A {}
```swift
↓import Dispatch
struct A {
static func dispatchMain() {}
static func dispatchMain() {}
}
A.dispatchMain()
```

```swift
↓import Foundation
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
↓import Dispatch

```

```swift
↓import Foundation
dispatchMain()
Expand Down
53 changes: 43 additions & 10 deletions Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ public struct UnusedImportRule: CorrectableRule, ConfigurationProviderRule, Anal
triggeringExamples: [
"""
↓import Dispatch
struct A {\n static func dispatchMain() {}\n}
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
""",
"""
↓import Foundation
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
↓import Dispatch

""",
"""
↓import Foundation
Expand All @@ -45,12 +56,32 @@ public struct UnusedImportRule: CorrectableRule, ConfigurationProviderRule, Anal
corrections: [
"""
↓import Dispatch
struct A {\n static func dispatchMain() {}\n}
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
""":
"""
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
""",
"""
↓import Foundation
struct A {
static func dispatchMain() {}
}
A.dispatchMain()
↓import Dispatch

""":
"""
struct A {\n static func dispatchMain() {}\n}
struct A {
static func dispatchMain() {}
}
A.dispatchMain()

""",
"""
↓import Foundation
Expand Down Expand Up @@ -166,14 +197,16 @@ private extension File {
if unusedImports.contains("Foundation") && containsAttributesRequiringFoundation() {
unusedImports.remove("Foundation")
}
return unusedImports.map { module in
let testableImportRange = contentsNSString.range(of: "@testable import \(module)\n")
if testableImportRange.location != NSNotFound {
return (module, testableImportRange)
}
return unusedImports
.map { module in
let testableImportRange = contentsNSString.range(of: "@testable import \(module)\n")
if testableImportRange.location != NSNotFound {
return (module, testableImportRange)
}

return (module, contentsNSString.range(of: "import \(module)\n"))
}
return (module, contentsNSString.range(of: "import \(module)\n"))
}
.sorted(by: { $0.1.location < $1.1.location })
}

private func containsAttributesRequiringFoundation() -> Bool {
Expand Down