Skip to content
Merged
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
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
Loading