Skip to content

Commit c6a4b5d

Browse files
committed
Fixup UseShorthandTypeNames to handle migration from the old tuple
node types to the new ones.
1 parent 2d8e612 commit c6a4b5d

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ import Foundation
1414
import SwiftFormatCore
1515
import SwiftSyntax
1616

17+
// FIXME: Remove this once we've completely moved up to a version of SwiftSyntax that has
18+
// switched over to TupleExprElement nodes.
19+
#if !HAS_CONSOLIDATED_TUPLE_AND_FUNCTION_CALL_SYNTAX
20+
fileprivate typealias TupleExprElementListSyntax = TupleElementListSyntax
21+
fileprivate typealias TupleExprElementSyntax = TupleElementSyntax
22+
23+
extension SyntaxFactory {
24+
fileprivate static func makeTupleExprElementList(_ elements: [TupleExprElementSyntax])
25+
-> TupleExprElementListSyntax
26+
{
27+
return makeTupleElementList(elements)
28+
}
29+
30+
fileprivate static func makeTupleExprElement(
31+
label: TokenSyntax?, colon: TokenSyntax?, expression: ExprSyntax, trailingComma: TokenSyntax?
32+
) -> TupleExprElementSyntax {
33+
return makeTupleElement(
34+
label: label, colon: colon, expression: expression, trailingComma: trailingComma)
35+
}
36+
}
37+
#endif
38+
1739
/// Shorthand type forms must be used wherever possible.
1840
///
1941
/// Lint: Using a non-shorthand form (e.g. `Array<Element>`) yields a lint error unless the long
@@ -236,10 +258,10 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
236258
// leading trivia to the left-paren that we're adding in this case.
237259
let tupleTypeElement =
238260
SyntaxFactory.makeTupleTypeElement(type: functionType, trailingComma: nil)
239-
let tupleElementList = SyntaxFactory.makeTupleTypeElementList([tupleTypeElement])
261+
let tupleTypeElementList = SyntaxFactory.makeTupleTypeElementList([tupleTypeElement])
240262
wrappedType = SyntaxFactory.makeTupleType(
241263
leftParen: SyntaxFactory.makeLeftParenToken(leadingTrivia: leadingTrivia),
242-
elements: tupleElementList,
264+
elements: tupleTypeElementList,
243265
rightParen: SyntaxFactory.makeRightParenToken())
244266
} else {
245267
// Otherwise, the argument type can safely become an optional by simply appending a "?", but
@@ -318,13 +340,13 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
318340
// Function types must be wrapped as a tuple before using shorthand optional syntax,
319341
// otherwise the "?" applies to the return type instead of the function type. Attach the
320342
// leading trivia to the left-paren that we're adding in this case.
321-
let tupleElement =
322-
SyntaxFactory.makeTupleElement(
343+
let tupleExprElement =
344+
SyntaxFactory.makeTupleExprElement(
323345
label: nil, colon: nil, expression: wrappedTypeExpr, trailingComma: nil)
324-
let tupleElementList = SyntaxFactory.makeTupleElementList([tupleElement])
346+
let tupleExprElementList = SyntaxFactory.makeTupleExprElementList([tupleExprElement])
325347
wrappedTypeExpr = SyntaxFactory.makeTupleExpr(
326348
leftParen: SyntaxFactory.makeLeftParenToken(leadingTrivia: leadingTrivia ?? []),
327-
elementList: tupleElementList,
349+
elementList: tupleExprElementList,
328350
rightParen: SyntaxFactory.makeRightParenToken())
329351
} else if let leadingTrivia = leadingTrivia {
330352
// Otherwise, the argument type can safely become an optional by simply appending a "?". If
@@ -421,21 +443,21 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
421443
}
422444

423445
private func expressionRepresentation(of tupleTypeElements: TupleTypeElementListSyntax)
424-
-> TupleElementListSyntax?
446+
-> TupleExprElementListSyntax?
425447
{
426448
guard !tupleTypeElements.isEmpty else { return nil }
427449

428-
var elementExprs = [TupleElementSyntax]()
450+
var exprElements = [TupleExprElementSyntax]()
429451
for typeElement in tupleTypeElements {
430452
guard let elementExpr = expressionRepresentation(of: typeElement.type) else { return nil }
431-
elementExprs.append(
432-
SyntaxFactory.makeTupleElement(
453+
exprElements.append(
454+
SyntaxFactory.makeTupleExprElement(
433455
label: typeElement.name,
434456
colon: typeElement.colon,
435457
expression: elementExpr,
436458
trailingComma: typeElement.trailingComma))
437459
}
438-
return SyntaxFactory.makeTupleElementList(elementExprs)
460+
return SyntaxFactory.makeTupleExprElementList(exprElements)
439461
}
440462

441463
private func makeFunctionTypeExpression(

0 commit comments

Comments
 (0)