mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Update parseType to support Swift 6.4 :: module selector syntax (#2399)
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
committed by
Cal Stephens
co-authored by
calda
parent
54ab2c763a
commit
e0524731fa
@@ -1490,6 +1490,17 @@ extension Formatter {
|
||||
return TypeName(range: startOfTypeIndex ... followingType.range.upperBound, formatter: self)
|
||||
}
|
||||
|
||||
// `::` can also continue a type (e.g. `Module::Type`), but unlike `.` and `&`,
|
||||
// there must be no newline between `::` and the following identifier.
|
||||
if let nextTokenIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: baseType.range.upperBound),
|
||||
tokens[nextTokenIndex] == .operator("::", .infix),
|
||||
let afterDoubleColonIndex = index(of: .nonSpaceOrComment, after: nextTokenIndex),
|
||||
!tokens[afterDoubleColonIndex].isLinebreak,
|
||||
let followingType = parseType(at: afterDoubleColonIndex, excludeLowercaseIdentifiers: excludeLowercaseIdentifiers)
|
||||
{
|
||||
return TypeName(range: startOfTypeIndex ... followingType.range.upperBound, formatter: self)
|
||||
}
|
||||
|
||||
return baseType
|
||||
}
|
||||
|
||||
|
||||
@@ -2397,6 +2397,36 @@ final class ParsingHelpersTests: XCTestCase {
|
||||
XCTAssertEqual(formatter.parseType(at: 2)?.string, "Foo.Bar.Baaz.Quux.InnerType1.InnerType2")
|
||||
}
|
||||
|
||||
func testParseModuleQualifiedType() {
|
||||
let formatter = Formatter(tokenize("""
|
||||
let foo: Module::Type
|
||||
"""))
|
||||
XCTAssertEqual(formatter.parseType(at: 5)?.string, "Module::Type")
|
||||
}
|
||||
|
||||
func testParseNestedModuleQualifiedType() {
|
||||
let formatter = Formatter(tokenize("""
|
||||
let foo: Module::Type.Module::Subtype
|
||||
"""))
|
||||
XCTAssertEqual(formatter.parseType(at: 5)?.string, "Module::Type.Module::Subtype")
|
||||
}
|
||||
|
||||
func testParseModuleQualifiedTypeWithNewlineBeforeDoubleColon() {
|
||||
let formatter = Formatter(tokenize("""
|
||||
let foo: NationalAeronauticsAndSpaceAdministration
|
||||
::RocketEngine
|
||||
"""))
|
||||
XCTAssertEqual(formatter.parseType(at: 5)?.string, "NationalAeronauticsAndSpaceAdministration::RocketEngine")
|
||||
}
|
||||
|
||||
func testDoesntParseModuleQualifiedTypeWithNewlineAfterDoubleColon() {
|
||||
let formatter = Formatter(tokenize("""
|
||||
let foo: NationalAeronauticsAndSpaceAdministration::
|
||||
RocketEngine
|
||||
"""))
|
||||
XCTAssertEqual(formatter.parseType(at: 5)?.string, "NationalAeronauticsAndSpaceAdministration")
|
||||
}
|
||||
|
||||
func testParseTuples() {
|
||||
let input = """
|
||||
let tuple: (foo: Foo, bar: Bar)
|
||||
|
||||
Reference in New Issue
Block a user