Skip to content

Adjustments for re-structured effect specifiers #478

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

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
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
43 changes: 25 additions & 18 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
arrangeAttributeList(node.attributes)

if let asyncKeyword = node.asyncKeyword {
if node.throwsKeyword != nil {
if let asyncKeyword = node.effectSpecifiers?.asyncSpecifier {
if node.effectSpecifiers?.throwsSpecifier != nil {
before(asyncKeyword, tokens: .break, .open)
} else {
before(asyncKeyword, tokens: .break)
}
}

if let throwsKeyword = node.throwsKeyword {
before(node.throwsKeyword, tokens: .break)
if node.asyncKeyword != nil {
if let throwsKeyword = node.effectSpecifiers?.throwsSpecifier {
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
if node.effectSpecifiers?.asyncSpecifier != nil {
after(throwsKeyword, tokens: .close)
}
}
Expand Down Expand Up @@ -1132,9 +1132,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
}

before(node.asyncKeyword, tokens: .break)
before(node.throwsTok, tokens: .break)
if let asyncKeyword = node.asyncKeyword, let throwsTok = node.throwsTok {
before(node.effectSpecifiers?.asyncSpecifier, tokens: .break)
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
if let asyncKeyword = node.effectSpecifiers?.asyncSpecifier, let throwsTok = node.effectSpecifiers?.throwsSpecifier {
before(asyncKeyword, tokens: .open)
after(throwsTok, tokens: .close)
}
Expand Down Expand Up @@ -1256,7 +1256,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: ReturnClauseSyntax) -> SyntaxVisitorContinueKind {
after(node.arrow, tokens: .space)
if node.parent?.is(FunctionTypeSyntax.self) ?? false {
// `FunctionTypeSyntax` used to not use `ReturnClauseSyntax` and had
// slightly different formatting behavior than the normal
// `ReturnClauseSyntax`. To maintain the previous formatting behavior,
// add a special case.
Comment on lines +1260 to +1263
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allevato I leave it up to you to decide whether you want to get rid of this special case in a follow-up PR.

before(node.arrow, tokens: .break)
before(node.returnType.firstToken, tokens: .break)
} else {
after(node.arrow, tokens: .space)
}

// Member type identifier is used when the return type is a member of another type. Add a group
// here so that the base, dot, and member type are kept together when they fit.
Expand Down Expand Up @@ -1500,10 +1509,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
override func visit(_ node: FunctionTypeSyntax) -> SyntaxVisitorContinueKind {
after(node.leftParen, tokens: .break(.open, size: 0), .open)
before(node.rightParen, tokens: .break(.close, size: 0), .close)
before(node.asyncKeyword, tokens: .break)
before(node.throwsOrRethrowsKeyword, tokens: .break)
before(node.arrow, tokens: .break)
before(node.returnType.firstToken, tokens: .break)
before(node.effectSpecifiers?.asyncSpecifier, tokens: .break)
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
return .visitChildren
}

Expand Down Expand Up @@ -1723,10 +1730,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: FunctionSignatureSyntax) -> SyntaxVisitorContinueKind {
before(node.asyncOrReasyncKeyword, tokens: .break)
before(node.throwsOrRethrowsKeyword, tokens: .break)
if let asyncOrReasyncKeyword = node.asyncOrReasyncKeyword,
let throwsOrRethrowsKeyword = node.throwsOrRethrowsKeyword
before(node.effectSpecifiers?.asyncSpecifier, tokens: .break)
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
if let asyncOrReasyncKeyword = node.effectSpecifiers?.asyncSpecifier,
let throwsOrRethrowsKeyword = node.effectSpecifiers?.throwsSpecifier
{
before(asyncOrReasyncKeyword, tokens: .open)
after(throwsOrRethrowsKeyword, tokens: .close)
Expand Down Expand Up @@ -1868,7 +1875,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
override func visit(_ node: ArrowExprSyntax) -> SyntaxVisitorContinueKind {
// The break before the `throws` keyword is inserted at the `InfixOperatorExpr` level so that it
// is placed in the correct relative position to the group surrounding the "operator".
after(node.throwsToken, tokens: .break)
after(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
return .visitChildren
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SwiftSyntax
/// Format: `-> ()` is replaced with `-> Void`
public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
public override func visit(_ node: FunctionTypeSyntax) -> TypeSyntax {
guard let returnType = node.returnType.as(TupleTypeSyntax.self),
guard let returnType = node.output.returnType.as(TupleTypeSyntax.self),
returnType.elements.count == 0
else {
return super.visit(node)
Expand All @@ -43,7 +43,10 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
// `(Int -> ()) -> ()` should become `(Int -> Void) -> Void`).
let arguments = visit(node.arguments)
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
return TypeSyntax(node.withArguments(arguments).withReturnType(TypeSyntax(voidKeyword)))
var rewrittenNode = node
rewrittenNode.arguments = arguments
rewrittenNode.output.returnType = TypeSyntax(voidKeyword)
return TypeSyntax(rewrittenNode)
}

public override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax {
Expand Down
13 changes: 5 additions & 8 deletions Sources/SwiftFormatRules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,9 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
leftParen: functionType.leftParen,
argumentTypes: functionType.arguments,
rightParen: functionType.rightParen,
asyncKeyword: functionType.asyncKeyword,
throwsOrRethrowsKeyword: functionType.throwsOrRethrowsKeyword,
arrow: functionType.arrow,
returnType: functionType.returnType
effectSpecifiers: functionType.effectSpecifiers,
arrow: functionType.output.arrow,
returnType: functionType.output.returnType
)
return ExprSyntax(result)

Expand Down Expand Up @@ -461,8 +460,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
leftParen: TokenSyntax,
argumentTypes: TupleTypeElementListSyntax,
rightParen: TokenSyntax,
asyncKeyword: TokenSyntax?,
throwsOrRethrowsKeyword: TokenSyntax?,
effectSpecifiers: TypeEffectSpecifiersSyntax?,
arrow: TokenSyntax,
returnType: TypeSyntax
) -> SequenceExprSyntax? {
Expand All @@ -478,8 +476,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
elementList: argumentTypeExprs,
rightParen: rightParen)
let arrowExpr = ArrowExprSyntax(
asyncKeyword: asyncKeyword,
throwsToken: throwsOrRethrowsKeyword,
effectSpecifiers: effectSpecifiers,
arrowToken: arrow)

return SequenceExprSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
acc.accessorKind.tokenKind == .keyword(.get),
acc.attributes == nil,
acc.modifier == nil,
acc.asyncKeyword == nil,
acc.throwsKeyword == nil
acc.effectSpecifiers == nil
else { return node }

diagnose(.removeExtraneousGetBlock, on: acc)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/UseSynthesizedInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
// Collect any possible redundant initializers into a list
} else if let initDecl = member.as(InitializerDeclSyntax.self) {
guard initDecl.optionalMark == nil else { continue }
guard initDecl.signature.throwsOrRethrowsKeyword == nil else { continue }
guard initDecl.signature.effectSpecifiers?.throwsSpecifier == nil else { continue }
initializers.append(initDecl)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
}

validateThrows(
signature.throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
signature.effectSpecifiers?.throwsSpecifier, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
validateReturn(
returnClause, name: name, returnDesc: commentInfo.returnsDescription, node: node)
let funcParameters = funcParametersIdentifiers(in: signature.input.parameterList)
Expand Down