Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where parseType helper didn't handle qualified types like Foo.Bar #1597

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/ParsingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,7 @@ extension Formatter {
/// - `some ...`
/// - `borrowing ...`
/// - `consuming ...`
/// - `(type).(type)`
func parseType(at startOfTypeIndex: Int) -> (name: String, range: ClosedRange<Int>)? {
guard let baseType = parseNonOptionalType(at: startOfTypeIndex) else { return nil }

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

// Any type can be followed by a `.` which can then continue the type
if let nextTokenIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: baseType.range.upperBound),
tokens[nextTokenIndex] == .operator(".", .infix),
let followingToken = index(of: .nonSpaceOrCommentOrLinebreak, after: nextTokenIndex),
let followingType = parseType(at: followingToken)
{
let typeRange = startOfTypeIndex ... followingType.range.upperBound
return (name: tokens[typeRange].string, range: typeRange)
}

return baseType
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/ParsingHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,27 @@ class ParsingHelpersTests: XCTestCase {
XCTAssertEqual(formatter.parseType(at: 5)?.name, "any Foo")
}

func testParseCompoundType() {
let formatter = Formatter(tokenize("""
let foo: Foo.Bar.Baaz
"""))
XCTAssertEqual(formatter.parseType(at: 5)?.name, "Foo.Bar.Baaz")
}

func testParseCompoundGenericType() {
let formatter = Formatter(tokenize("""
let foo: Foo<Bar>.Bar.Baaz<Quux.V2>
"""))
XCTAssertEqual(formatter.parseType(at: 5)?.name, "Foo<Bar>.Bar.Baaz<Quux.V2>")
}

func testParseExistentialTypeWithSubtype() {
let formatter = Formatter(tokenize("""
let foo: (any Foo).Bar.Baaz
"""))
XCTAssertEqual(formatter.parseType(at: 5)?.name, "(any Foo).Bar.Baaz")
}

func testParseOpaqueReturnType() {
let formatter = Formatter(tokenize("""
var body: some View { EmptyView() }
Expand Down
14 changes: 14 additions & 0 deletions Tests/RulesTests+Wrapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,20 @@ class WrappingTests: RulesTests {
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

func testAttributeOnComputedProperty() {
let input = """
extension SectionContainer: ContentProviding where Section: ContentProviding {
@_disfavoredOverload
public var content: Section.Content {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the fix in this PR this example was incorrectly handled as a stored property, causing it to be updated incorrectly to @_disfavoredOverload public var content: Section.Content { ... }.

section.content
}
}
"""

let options = FormatOptions(storedVarAttributes: .sameLine, computedVarAttributes: .prevLine)
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

// MARK: wrapEnumCases

func testMultilineEnumCases() {
Expand Down