Skip to content

Commit

Permalink
Respect scattered disable commands in duplicate_imports rule (#5432)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Jan 20, 2024
1 parent 7f960d3 commit 1e9428a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
[leonardosrodrigues0](https://github.com/leonardosrodrigues0)
[#5265](https://github.com/realm/SwiftLint/issues/5265)

* Respect scattered disable commands in auto-correction of `duplicate_imports`
rule.
[SimplyDanny](https://github.com/SimplyDanny)
[#5418](https://github.com/realm/SwiftLint/pull/5418)

* Add new `non_optional_string_data_conversion` rule to enforce
non-failable conversions of UTF-8 `String` <-> `Data`.
[Ben P](https://github.com/ben-p-commits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ private extension DuplicateImportsRule {
override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
let itemsToRemove = node
.enumerated()
.filter { !$1.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) }
.map { ($0, $1.item.positionAfterSkippingLeadingTrivia) }
.filter { _, position in importPositionsToRemove.contains(position) }
.filter { importPositionsToRemove.contains($1) }
.map { (indexInParent: $0, absolutePosition: $1) }

correctionPositions.append(
contentsOf: itemsToRemove.map(\.absolutePosition)
)
if itemsToRemove.isEmpty {
return super.visit(node)
}
correctionPositions.append(contentsOf: itemsToRemove.map(\.absolutePosition))

var copy = node
for indexInParent in itemsToRemove.map(\.indexInParent).reversed() {
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftLintFrameworkTests/DuplicateImportsRuleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testable import SwiftLintBuiltInRules
import XCTest

class DuplicateImportsRuleTests: XCTestCase {
func testDisableCommand() {
let content = """
import InspireAPI
// swiftlint:disable:next duplicate_imports
import class InspireAPI.Response
"""
let file = SwiftLintFile(contents: content)

_ = DuplicateImportsRule().correct(file: file)

XCTAssertEqual(file.contents, content)
}
}

0 comments on commit 1e9428a

Please sign in to comment.