From d7fe31a0a89ed898ec1b3639e533946ed1897f62 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Wed, 16 Jan 2019 13:59:20 -0800 Subject: [PATCH] Fix unused import correction --- CHANGELOG.md | 5 +- Rules.md | 12 ++++- .../Rules/Lint/UnusedImportRule.swift | 53 +++++++++++++++---- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4308c4e682..88abee6522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Rules.md b/Rules.md index c2ba86c1fd..2c2cc61dcf 100644 --- a/Rules.md +++ b/Rules.md @@ -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() diff --git a/Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift b/Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift index 09f96ad881..c5a24731c6 100644 --- a/Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift @@ -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 @@ -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 @@ -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 {