Skip to content

Commit d931f86

Browse files
committed
Discard newlines when sorting imports.
When the author inserts a newline between the `import` token and the imported path, that newline shouldn't be considered when sorting the paths.
1 parent 6207f97 commit d931f86

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

Sources/SwiftFormatRules/OrderedImports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ fileprivate class Line {
503503
else {
504504
return ""
505505
}
506-
return importDecl.path.description.trimmingCharacters(in: .whitespaces)
506+
return importDecl.path.description.trimmingCharacters(in: .whitespacesAndNewlines)
507507
}
508508

509509
/// Returns the first `TokenSyntax` in the code block(s) from this Line, or nil when this Line

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ extension DifferentiationAttributeTests {
207207
// `swift test --generate-linuxmain`
208208
// to regenerate.
209209
static let __allTests__DifferentiationAttributeTests = [
210-
("testDerivative", testDerivative),
211210
("testDifferentiable", testDifferentiable),
212-
("testTranspose", testTranspose),
213211
]
214212
}
215213

Tests/SwiftFormatRulesTests/OrderedImportsTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
396396
)
397397
}
398398

399+
func testImportsContainingNewlines() {
400+
let input =
401+
"""
402+
import
403+
zeta
404+
import Zeta
405+
import
406+
Alpha
407+
import Beta
408+
"""
409+
410+
let expected =
411+
"""
412+
import
413+
Alpha
414+
import Beta
415+
import Zeta
416+
import
417+
zeta
418+
"""
419+
420+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
421+
}
422+
399423
func testRemovesDuplicateImports() {
400424
let input =
401425
"""

Tests/SwiftFormatRulesTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ extension OrderedImportsTests {
272272
("testDuplicateIgnoredImports", testDuplicateIgnoredImports),
273273
("testEmptyFile", testEmptyFile),
274274
("testIgnoredConditionalImports", testIgnoredConditionalImports),
275+
("testImportsContainingNewlines", testImportsContainingNewlines),
275276
("testImportsOrderWithDocComment", testImportsOrderWithDocComment),
276277
("testImportsOrderWithoutModuleType", testImportsOrderWithoutModuleType),
277278
("testInvalidImportsOrder", testInvalidImportsOrder),

0 commit comments

Comments
 (0)