mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Rename sortedImports to sortImports
This commit is contained in:
@@ -55,8 +55,8 @@
|
||||
* [redundantVoidReturnType](#redundantVoidReturnType)
|
||||
* [semicolons](#semicolons)
|
||||
* [sortDeclarations](#sortDeclarations)
|
||||
* [sortImports](#sortImports)
|
||||
* [sortTypealiases](#sortTypealiases)
|
||||
* [sortedImports](#sortedImports)
|
||||
* [spaceAroundBraces](#spaceAroundBraces)
|
||||
* [spaceAroundBrackets](#spaceAroundBrackets)
|
||||
* [spaceAroundComments](#spaceAroundComments)
|
||||
@@ -102,6 +102,7 @@
|
||||
|
||||
# Deprecated Rules (do not use)
|
||||
|
||||
* [sortedImports](#sortedImports)
|
||||
* [sortedSwitchCases](#sortedSwitchCases)
|
||||
* [specifiers](#specifiers)
|
||||
|
||||
@@ -1836,34 +1837,7 @@ and declarations between // swiftformat:sort:begin and
|
||||
</details>
|
||||
<br/>
|
||||
|
||||
## sortSwitchCases
|
||||
|
||||
Sort switch cases alphabetically.
|
||||
|
||||
## sortTypealiases
|
||||
|
||||
Sort protocol composition typealiases alphabetically.
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```diff
|
||||
- typealias Placeholders = Foo & Bar & Baaz & Quux
|
||||
+ typealias Placeholders = Baaz & Bar & Foo & Quux
|
||||
|
||||
typealias Dependencies
|
||||
- = FooProviding
|
||||
+ = BaazProviding
|
||||
& BarProviding
|
||||
- & BaazProviding
|
||||
+ & FooProviding
|
||||
& QuuxProviding
|
||||
```
|
||||
|
||||
</details>
|
||||
<br/>
|
||||
|
||||
## sortedImports
|
||||
## sortImports
|
||||
|
||||
Sort import statements alphabetically.
|
||||
|
||||
@@ -1899,6 +1873,39 @@ Option | Description
|
||||
</details>
|
||||
<br/>
|
||||
|
||||
## sortSwitchCases
|
||||
|
||||
Sort switch cases alphabetically.
|
||||
|
||||
## sortTypealiases
|
||||
|
||||
Sort protocol composition typealiases alphabetically.
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```diff
|
||||
- typealias Placeholders = Foo & Bar & Baaz & Quux
|
||||
+ typealias Placeholders = Baaz & Bar & Foo & Quux
|
||||
|
||||
typealias Dependencies
|
||||
- = FooProviding
|
||||
+ = BaazProviding
|
||||
& BarProviding
|
||||
- & BaazProviding
|
||||
+ & FooProviding
|
||||
& QuuxProviding
|
||||
```
|
||||
|
||||
</details>
|
||||
<br/>
|
||||
|
||||
## sortedImports
|
||||
|
||||
Sort import statements alphabetically.
|
||||
|
||||
*Note: sortedImports rule is deprecated. Use sortImports instead.*
|
||||
|
||||
## sortedSwitchCases
|
||||
|
||||
Sort switch cases alphabetically.
|
||||
|
||||
@@ -721,7 +721,7 @@ private struct Examples {
|
||||
```
|
||||
"""
|
||||
|
||||
let sortedImports = """
|
||||
let sortImports = """
|
||||
```diff
|
||||
- import Foo
|
||||
- import Bar
|
||||
|
||||
+13
-1
@@ -5216,8 +5216,20 @@ public struct _FormatRules {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sort import statements
|
||||
/// Deprecated
|
||||
public let sortedImports = FormatRule(
|
||||
help: "Sort import statements alphabetically.",
|
||||
deprecationMessage: "Use sortImports instead.",
|
||||
options: ["importgrouping"],
|
||||
sharedOptions: ["linebreaks"]
|
||||
) { formatter in
|
||||
_ = formatter.options.importGrouping
|
||||
_ = formatter.options.linebreak
|
||||
FormatRules.sortImports.apply(with: formatter)
|
||||
}
|
||||
|
||||
/// Sort import statements
|
||||
public let sortImports = FormatRule(
|
||||
help: "Sort import statements alphabetically.",
|
||||
options: ["importgrouping"],
|
||||
sharedOptions: ["linebreaks"]
|
||||
|
||||
@@ -755,7 +755,7 @@ class ArgumentsTests: XCTestCase {
|
||||
|
||||
func testParseOptionAsRuleThrows() {
|
||||
XCTAssertThrowsError(try parseRules("importgrouping")) { error in
|
||||
XCTAssert("\(error)".contains("'sortedImports'"))
|
||||
XCTAssert("\(error)".contains("'sortImports'"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -918,7 +918,7 @@ class OrganizationTests: RulesTests {
|
||||
|
||||
testFormatting(
|
||||
for: input, output, rule: FormatRules.organizeDeclarations,
|
||||
exclude: ["blankLinesAtStartOfScope", "sortedImports"]
|
||||
exclude: ["blankLinesAtStartOfScope", "sortImports"]
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2574,131 +2574,131 @@ class OrganizationTests: RulesTests {
|
||||
testFormatting(for: input, output, rule: FormatRules.markTypes)
|
||||
}
|
||||
|
||||
// MARK: - sortedImports
|
||||
// MARK: - sortImports
|
||||
|
||||
func testSortedImportsSimpleCase() {
|
||||
func testSortImportsSimpleCase() {
|
||||
let input = "import Foo\nimport Bar"
|
||||
let output = "import Bar\nimport Foo"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testSortedImportsKeepsPreviousCommentWithImport() {
|
||||
func testSortImportsKeepsPreviousCommentWithImport() {
|
||||
let input = "import Foo\n// important comment\n// (very important)\nimport Bar"
|
||||
let output = "// important comment\n// (very important)\nimport Bar\nimport Foo"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
func testSortedImportsKeepsPreviousCommentWithImport2() {
|
||||
func testSortImportsKeepsPreviousCommentWithImport2() {
|
||||
let input = "// important comment\n// (very important)\nimport Foo\nimport Bar"
|
||||
let output = "import Bar\n// important comment\n// (very important)\nimport Foo"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
func testSortedImportsDoesntMoveHeaderComment() {
|
||||
func testSortImportsDoesntMoveHeaderComment() {
|
||||
let input = "// header comment\n\nimport Foo\nimport Bar"
|
||||
let output = "// header comment\n\nimport Bar\nimport Foo"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testSortedImportsDoesntMoveHeaderCommentFollowedByImportComment() {
|
||||
func testSortImportsDoesntMoveHeaderCommentFollowedByImportComment() {
|
||||
let input = "// header comment\n\n// important comment\nimport Foo\nimport Bar"
|
||||
let output = "// header comment\n\nimport Bar\n// important comment\nimport Foo"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
func testSortedImportsOnSameLine() {
|
||||
func testSortImportsOnSameLine() {
|
||||
let input = "import Foo; import Bar\nimport Baz"
|
||||
let output = "import Baz\nimport Foo; import Bar"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testSortedImportsWithSemicolonAndCommentOnSameLine() {
|
||||
func testSortImportsWithSemicolonAndCommentOnSameLine() {
|
||||
let input = "import Foo; // foobar\nimport Bar\nimport Baz"
|
||||
let output = "import Bar\nimport Baz\nimport Foo; // foobar"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports, exclude: ["semicolons"])
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports, exclude: ["semicolons"])
|
||||
}
|
||||
|
||||
func testSortedImportEnum() {
|
||||
func testSortImportEnum() {
|
||||
let input = "import enum Foo.baz\nimport Foo.bar"
|
||||
let output = "import Foo.bar\nimport enum Foo.baz"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testSortedImportFunc() {
|
||||
func testSortImportFunc() {
|
||||
let input = "import func Foo.baz\nimport Foo.bar"
|
||||
let output = "import Foo.bar\nimport func Foo.baz"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testAlreadySortedImportsDoesNothing() {
|
||||
func testAlreadySortImportsDoesNothing() {
|
||||
let input = "import Bar\nimport Foo"
|
||||
testFormatting(for: input, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testPreprocessorSortedImports() {
|
||||
func testPreprocessorSortImports() {
|
||||
let input = "#if os(iOS)\n import Foo2\n import Bar2\n#else\n import Foo1\n import Bar1\n#endif\nimport Foo3\nimport Bar3"
|
||||
let output = "#if os(iOS)\n import Bar2\n import Foo2\n#else\n import Bar1\n import Foo1\n#endif\nimport Bar3\nimport Foo3"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testTestableSortedImports() {
|
||||
func testTestableSortImports() {
|
||||
let input = "@testable import Foo3\nimport Bar3"
|
||||
let output = "import Bar3\n@testable import Foo3"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testLengthSortedImports() {
|
||||
func testLengthSortImports() {
|
||||
let input = "import Foo\nimport Module\nimport Bar3"
|
||||
let output = "import Foo\nimport Bar3\nimport Module"
|
||||
let options = FormatOptions(importGrouping: .length)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports, options: options)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports, options: options)
|
||||
}
|
||||
|
||||
func testTestableImportsWithTestableOnPreviousLine() {
|
||||
let input = "@testable\nimport Foo3\nimport Bar3"
|
||||
let output = "import Bar3\n@testable\nimport Foo3"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testTestableImportsWithGroupingTestableBottom() {
|
||||
let input = "@testable import Bar\nimport Foo\n@testable import UIKit"
|
||||
let output = "import Foo\n@testable import Bar\n@testable import UIKit"
|
||||
let options = FormatOptions(importGrouping: .testableLast)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports, options: options)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports, options: options)
|
||||
}
|
||||
|
||||
func testTestableImportsWithGroupingTestableTop() {
|
||||
let input = "@testable import Bar\nimport Foo\n@testable import UIKit"
|
||||
let output = "@testable import Bar\n@testable import UIKit\nimport Foo"
|
||||
let options = FormatOptions(importGrouping: .testableFirst)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports, options: options)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports, options: options)
|
||||
}
|
||||
|
||||
func testCaseInsensitiveSortedImports() {
|
||||
func testCaseInsensitiveSortImports() {
|
||||
let input = "import Zlib\nimport lib"
|
||||
let output = "import lib\nimport Zlib"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testCaseInsensitiveCaseDifferingSortedImports() {
|
||||
func testCaseInsensitiveCaseDifferingSortImports() {
|
||||
let input = "import c\nimport B\nimport A.a\nimport A.A"
|
||||
let output = "import A.A\nimport A.a\nimport B\nimport c"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testNoDeleteCodeBetweenImports() {
|
||||
let input = "import Foo\nfunc bar() {}\nimport Bar"
|
||||
testFormatting(for: input, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
func testNoDeleteCodeBetweenImports2() {
|
||||
let input = "import Foo\nimport Bar\nfoo = bar\nimport Bar"
|
||||
let output = "import Bar\nimport Foo\nfoo = bar\nimport Bar"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
@@ -2714,13 +2714,13 @@ class OrganizationTests: RulesTests {
|
||||
|
||||
import A
|
||||
"""
|
||||
testFormatting(for: input, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testSortContiguousImports() {
|
||||
let input = "import Foo\nimport Bar\nfunc bar() {}\nimport Quux\nimport Baz"
|
||||
let output = "import Bar\nimport Foo\nfunc bar() {}\nimport Baz\nimport Quux"
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports,
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports,
|
||||
exclude: ["blankLineAfterImports"])
|
||||
}
|
||||
|
||||
@@ -2747,7 +2747,7 @@ class OrganizationTests: RulesTests {
|
||||
#endif
|
||||
#endif
|
||||
"""
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
func testNoMangleFileHeaderNotFollowedByLinebreak() {
|
||||
@@ -2775,7 +2775,7 @@ class OrganizationTests: RulesTests {
|
||||
import AModuleUI
|
||||
import SomeOtherModule
|
||||
"""
|
||||
testFormatting(for: input, output, rule: FormatRules.sortedImports)
|
||||
testFormatting(for: input, output, rule: FormatRules.sortImports)
|
||||
}
|
||||
|
||||
// MARK: - sortSwitchCases
|
||||
|
||||
Reference in New Issue
Block a user