Skip to content

Commit 5383050

Browse files
caldanicklockwood
authored andcommitted
Fix issue parseType helper didn't handle qualified types like Foo.Bar
1 parent 4a072af commit 5383050

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Sources/ParsingHelpers.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ extension Formatter {
12911291
/// - `some ...`
12921292
/// - `borrowing ...`
12931293
/// - `consuming ...`
1294+
/// - `(type).(type)`
12941295
func parseType(at startOfTypeIndex: Int) -> (name: String, range: ClosedRange<Int>)? {
12951296
guard let baseType = parseNonOptionalType(at: startOfTypeIndex) else { return nil }
12961297

@@ -1302,6 +1303,16 @@ extension Formatter {
13021303
return (name: tokens[typeRange].string, range: typeRange)
13031304
}
13041305

1306+
// Any type can be followed by a `.` which can then continue the type
1307+
if let nextTokenIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: baseType.range.upperBound),
1308+
tokens[nextTokenIndex] == .operator(".", .infix),
1309+
let followingToken = index(of: .nonSpaceOrCommentOrLinebreak, after: nextTokenIndex),
1310+
let followingType = parseType(at: followingToken)
1311+
{
1312+
let typeRange = startOfTypeIndex ... followingType.range.upperBound
1313+
return (name: tokens[typeRange].string, range: typeRange)
1314+
}
1315+
13051316
return baseType
13061317
}
13071318

Tests/ParsingHelpersTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,27 @@ class ParsingHelpersTests: XCTestCase {
17461746
XCTAssertEqual(formatter.parseType(at: 5)?.name, "any Foo")
17471747
}
17481748

1749+
func testParseCompoundType() {
1750+
let formatter = Formatter(tokenize("""
1751+
let foo: Foo.Bar.Baaz
1752+
"""))
1753+
XCTAssertEqual(formatter.parseType(at: 5)?.name, "Foo.Bar.Baaz")
1754+
}
1755+
1756+
func testParseCompoundGenericType() {
1757+
let formatter = Formatter(tokenize("""
1758+
let foo: Foo<Bar>.Bar.Baaz<Quux.V2>
1759+
"""))
1760+
XCTAssertEqual(formatter.parseType(at: 5)?.name, "Foo<Bar>.Bar.Baaz<Quux.V2>")
1761+
}
1762+
1763+
func testParseExistentialTypeWithSubtype() {
1764+
let formatter = Formatter(tokenize("""
1765+
let foo: (any Foo).Bar.Baaz
1766+
"""))
1767+
XCTAssertEqual(formatter.parseType(at: 5)?.name, "(any Foo).Bar.Baaz")
1768+
}
1769+
17491770
func testParseOpaqueReturnType() {
17501771
let formatter = Formatter(tokenize("""
17511772
var body: some View { EmptyView() }

Tests/RulesTests+Wrapping.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,20 @@ class WrappingTests: RulesTests {
48434843
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
48444844
}
48454845

4846+
func testAttributeOnComputedProperty() {
4847+
let input = """
4848+
extension SectionContainer: ContentProviding where Section: ContentProviding {
4849+
@_disfavoredOverload
4850+
public var content: Section.Content {
4851+
section.content
4852+
}
4853+
}
4854+
"""
4855+
4856+
let options = FormatOptions(storedVarAttributes: .sameLine, computedVarAttributes: .prevLine)
4857+
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
4858+
}
4859+
48464860
// MARK: wrapEnumCases
48474861

48484862
func testMultilineEnumCases() {

0 commit comments

Comments
 (0)