Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for integer generics with new generic argument node #866

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Changes from 1 commit
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
37 changes: 23 additions & 14 deletions Sources/SwiftFormat/Rules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,28 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {

switch node.name.text {
case "Array":
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard let argument = genericArgumentList.firstAndOnly,
case .type(let typeArgument) = argument else {
Azoy marked this conversation as resolved.
Show resolved Hide resolved
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) = arguments.0.argument,
caes .type(let type1Argument) = arguments.1.argument else {
Azoy marked this conversation as resolved.
Show resolved Hide resolved
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 +78,13 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
newNode = nil
break
}
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard let argument = genericArgumentList.firstAndOnly,
case .type(let typeArgument) = argument else {
newNode = nil
break
}
newNode = shorthandOptionalType(
wrapping: typeArgument.argument,
wrapping: typeArgument,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia
)
Expand Down Expand Up @@ -137,38 +142,42 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {

switch expression.baseName.text {
case "Array":
guard let typeArgument = genericArgumentList.firstAndOnly else {
guard let argument = genericArgumentList.firstAndOnly,
case .type(let typeArgument) = 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) = arguments.0.argument,
case .type(let type1Argument) = 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 let argument = genericArgumentList.firstAndOnly,
case .type(let typeArgument) = argument else {
newNode = nil
break
}
let optionalTypeExpr = makeOptionalTypeExpression(
wrapping: typeArgument.argument,
wrapping: typeArgument,
leadingTrivia: leadingTrivia,
questionMark: TokenSyntax.postfixQuestionMarkToken(trailingTrivia: trailingTrivia)
)
Expand Down
Loading