Skip to content

Commit

Permalink
Merge pull request #866 from Azoy/integer-generics
Browse files Browse the repository at this point in the history
Prepare for integer generics with new generic argument node
  • Loading branch information
ahoppen authored Nov 7, 2024
2 parents c1e7b6e + e6aa9ec commit 637cb85
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions Sources/SwiftFormat/Rules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {

switch node.name.text {
case "Array":
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard case .type(let typeArgument) = genericArgumentList.firstAndOnly?.argument else {
newNode = nil
break
}
newNode = shorthandArrayType(
element: typeArgument.argument,
element: typeArgument,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia
)

case "Dictionary":
guard let typeArguments = exactlyTwoChildren(of: genericArgumentList) else {
guard let arguments = exactlyTwoChildren(of: genericArgumentList),
case (.type(let type0Argument), .type(let type1Argument)) = (arguments.0.argument, arguments.1.argument)
else {
newNode = nil
break
}
newNode = shorthandDictionaryType(
key: typeArguments.0.argument,
value: typeArguments.1.argument,
key: type0Argument,
value: type1Argument,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia
)
Expand All @@ -74,12 +76,12 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
newNode = nil
break
}
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard case .type(let typeArgument) = genericArgumentList.firstAndOnly?.argument else {
newNode = nil
break
}
newNode = shorthandOptionalType(
wrapping: typeArgument.argument,
wrapping: typeArgument,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia
)
Expand Down Expand Up @@ -137,38 +139,40 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {

switch expression.baseName.text {
case "Array":
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard case .type(let typeArgument) = genericArgumentList.firstAndOnly?.argument else {
newNode = nil
break
}
let arrayTypeExpr = makeArrayTypeExpression(
elementType: typeArgument.argument,
elementType: typeArgument,
leftSquare: TokenSyntax.leftSquareToken(leadingTrivia: leadingTrivia),
rightSquare: TokenSyntax.rightSquareToken(trailingTrivia: trailingTrivia)
)
newNode = ExprSyntax(arrayTypeExpr)

case "Dictionary":
guard let typeArguments = exactlyTwoChildren(of: genericArgumentList) else {
guard let arguments = exactlyTwoChildren(of: genericArgumentList),
case (.type(let type0Argument), .type(let type1Argument)) = (arguments.0.argument, arguments.1.argument)
else {
newNode = nil
break
}
let dictTypeExpr = makeDictionaryTypeExpression(
keyType: typeArguments.0.argument,
valueType: typeArguments.1.argument,
keyType: type0Argument,
valueType: type1Argument,
leftSquare: TokenSyntax.leftSquareToken(leadingTrivia: leadingTrivia),
colon: TokenSyntax.colonToken(trailingTrivia: .spaces(1)),
rightSquare: TokenSyntax.rightSquareToken(trailingTrivia: trailingTrivia)
)
newNode = ExprSyntax(dictTypeExpr)

case "Optional":
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard case .type(let typeArgument) = genericArgumentList.firstAndOnly?.argument else {
newNode = nil
break
}
let optionalTypeExpr = makeOptionalTypeExpression(
wrapping: typeArgument.argument,
wrapping: typeArgument,
leadingTrivia: leadingTrivia,
questionMark: TokenSyntax.postfixQuestionMarkToken(trailingTrivia: trailingTrivia)
)
Expand Down

0 comments on commit 637cb85

Please sign in to comment.