mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
52 lines
1.4 KiB
Swift
52 lines
1.4 KiB
Swift
//
|
|
// DuplicateImports.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 2/7/18.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Remove duplicate import statements
|
|
static let duplicateImports = FormatRule(
|
|
help: "Remove duplicate import statements."
|
|
) { formatter in
|
|
for var importRanges in formatter.parseImports().reversed() {
|
|
for i in importRanges.indices.reversed() {
|
|
let range = importRanges.remove(at: i)
|
|
guard let j = importRanges.firstIndex(where: { $0.module == range.module }) else {
|
|
continue
|
|
}
|
|
let range2 = importRanges[j]
|
|
if Set(range.attributes).isSubset(of: range2.attributes) {
|
|
formatter.removeTokens(in: range.range)
|
|
continue
|
|
}
|
|
if j >= i {
|
|
formatter.removeTokens(in: range2.range)
|
|
importRanges.remove(at: j)
|
|
}
|
|
importRanges.append(range)
|
|
}
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
import Foo
|
|
import Bar
|
|
- import Foo
|
|
```
|
|
|
|
```diff
|
|
import B
|
|
#if os(iOS)
|
|
import A
|
|
- import B
|
|
#endif
|
|
```
|
|
"""
|
|
}
|
|
}
|