@@ -14,6 +14,28 @@ import Foundation
14
14
import SwiftFormatCore
15
15
import SwiftSyntax
16
16
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
+
17
39
/// Shorthand type forms must be used wherever possible.
18
40
///
19
41
/// 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 {
236
258
// leading trivia to the left-paren that we're adding in this case.
237
259
let tupleTypeElement =
238
260
SyntaxFactory . makeTupleTypeElement ( type: functionType, trailingComma: nil )
239
- let tupleElementList = SyntaxFactory . makeTupleTypeElementList ( [ tupleTypeElement] )
261
+ let tupleTypeElementList = SyntaxFactory . makeTupleTypeElementList ( [ tupleTypeElement] )
240
262
wrappedType = SyntaxFactory . makeTupleType (
241
263
leftParen: SyntaxFactory . makeLeftParenToken ( leadingTrivia: leadingTrivia) ,
242
- elements: tupleElementList ,
264
+ elements: tupleTypeElementList ,
243
265
rightParen: SyntaxFactory . makeRightParenToken ( ) )
244
266
} else {
245
267
// Otherwise, the argument type can safely become an optional by simply appending a "?", but
@@ -318,13 +340,13 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
318
340
// Function types must be wrapped as a tuple before using shorthand optional syntax,
319
341
// otherwise the "?" applies to the return type instead of the function type. Attach the
320
342
// 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 (
323
345
label: nil , colon: nil , expression: wrappedTypeExpr, trailingComma: nil )
324
- let tupleElementList = SyntaxFactory . makeTupleElementList ( [ tupleElement ] )
346
+ let tupleExprElementList = SyntaxFactory . makeTupleExprElementList ( [ tupleExprElement ] )
325
347
wrappedTypeExpr = SyntaxFactory . makeTupleExpr (
326
348
leftParen: SyntaxFactory . makeLeftParenToken ( leadingTrivia: leadingTrivia ?? [ ] ) ,
327
- elementList: tupleElementList ,
349
+ elementList: tupleExprElementList ,
328
350
rightParen: SyntaxFactory . makeRightParenToken ( ) )
329
351
} else if let leadingTrivia = leadingTrivia {
330
352
// Otherwise, the argument type can safely become an optional by simply appending a "?". If
@@ -421,21 +443,21 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
421
443
}
422
444
423
445
private func expressionRepresentation( of tupleTypeElements: TupleTypeElementListSyntax )
424
- -> TupleElementListSyntax ?
446
+ -> TupleExprElementListSyntax ?
425
447
{
426
448
guard !tupleTypeElements. isEmpty else { return nil }
427
449
428
- var elementExprs = [ TupleElementSyntax ] ( )
450
+ var exprElements = [ TupleExprElementSyntax ] ( )
429
451
for typeElement in tupleTypeElements {
430
452
guard let elementExpr = expressionRepresentation ( of: typeElement. type) else { return nil }
431
- elementExprs . append (
432
- SyntaxFactory . makeTupleElement (
453
+ exprElements . append (
454
+ SyntaxFactory . makeTupleExprElement (
433
455
label: typeElement. name,
434
456
colon: typeElement. colon,
435
457
expression: elementExpr,
436
458
trailingComma: typeElement. trailingComma) )
437
459
}
438
- return SyntaxFactory . makeTupleElementList ( elementExprs )
460
+ return SyntaxFactory . makeTupleExprElementList ( exprElements )
439
461
}
440
462
441
463
private func makeFunctionTypeExpression(
0 commit comments