Skip to content

Commit 34f7266

Browse files
authored
Merge pull request #478 from ahoppen/ahoppen/effect-specifiers
Adjustments for re-structured effect specifiers
2 parents 7272f19 + 42a8904 commit 34f7266

File tree

6 files changed

+38
-32
lines changed

6 files changed

+38
-32
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
450450
override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
451451
arrangeAttributeList(node.attributes)
452452

453-
if let asyncKeyword = node.asyncKeyword {
454-
if node.throwsKeyword != nil {
453+
if let asyncKeyword = node.effectSpecifiers?.asyncSpecifier {
454+
if node.effectSpecifiers?.throwsSpecifier != nil {
455455
before(asyncKeyword, tokens: .break, .open)
456456
} else {
457457
before(asyncKeyword, tokens: .break)
458458
}
459459
}
460460

461-
if let throwsKeyword = node.throwsKeyword {
462-
before(node.throwsKeyword, tokens: .break)
463-
if node.asyncKeyword != nil {
461+
if let throwsKeyword = node.effectSpecifiers?.throwsSpecifier {
462+
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
463+
if node.effectSpecifiers?.asyncSpecifier != nil {
464464
after(throwsKeyword, tokens: .close)
465465
}
466466
}
@@ -1132,9 +1132,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
11321132
}
11331133
}
11341134

1135-
before(node.asyncKeyword, tokens: .break)
1136-
before(node.throwsTok, tokens: .break)
1137-
if let asyncKeyword = node.asyncKeyword, let throwsTok = node.throwsTok {
1135+
before(node.effectSpecifiers?.asyncSpecifier, tokens: .break)
1136+
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
1137+
if let asyncKeyword = node.effectSpecifiers?.asyncSpecifier, let throwsTok = node.effectSpecifiers?.throwsSpecifier {
11381138
before(asyncKeyword, tokens: .open)
11391139
after(throwsTok, tokens: .close)
11401140
}
@@ -1256,7 +1256,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12561256
}
12571257

12581258
override func visit(_ node: ReturnClauseSyntax) -> SyntaxVisitorContinueKind {
1259-
after(node.arrow, tokens: .space)
1259+
if node.parent?.is(FunctionTypeSyntax.self) ?? false {
1260+
// `FunctionTypeSyntax` used to not use `ReturnClauseSyntax` and had
1261+
// slightly different formatting behavior than the normal
1262+
// `ReturnClauseSyntax`. To maintain the previous formatting behavior,
1263+
// add a special case.
1264+
before(node.arrow, tokens: .break)
1265+
before(node.returnType.firstToken, tokens: .break)
1266+
} else {
1267+
after(node.arrow, tokens: .space)
1268+
}
12601269

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

@@ -1723,10 +1730,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17231730
}
17241731

17251732
override func visit(_ node: FunctionSignatureSyntax) -> SyntaxVisitorContinueKind {
1726-
before(node.asyncOrReasyncKeyword, tokens: .break)
1727-
before(node.throwsOrRethrowsKeyword, tokens: .break)
1728-
if let asyncOrReasyncKeyword = node.asyncOrReasyncKeyword,
1729-
let throwsOrRethrowsKeyword = node.throwsOrRethrowsKeyword
1733+
before(node.effectSpecifiers?.asyncSpecifier, tokens: .break)
1734+
before(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
1735+
if let asyncOrReasyncKeyword = node.effectSpecifiers?.asyncSpecifier,
1736+
let throwsOrRethrowsKeyword = node.effectSpecifiers?.throwsSpecifier
17301737
{
17311738
before(asyncOrReasyncKeyword, tokens: .open)
17321739
after(throwsOrRethrowsKeyword, tokens: .close)
@@ -1868,7 +1875,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
18681875
override func visit(_ node: ArrowExprSyntax) -> SyntaxVisitorContinueKind {
18691876
// The break before the `throws` keyword is inserted at the `InfixOperatorExpr` level so that it
18701877
// is placed in the correct relative position to the group surrounding the "operator".
1871-
after(node.throwsToken, tokens: .break)
1878+
after(node.effectSpecifiers?.throwsSpecifier, tokens: .break)
18721879
return .visitChildren
18731880
}
18741881

Sources/SwiftFormatRules/ReturnVoidInsteadOfEmptyTuple.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftSyntax
2323
/// Format: `-> ()` is replaced with `-> Void`
2424
public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
2525
public override func visit(_ node: FunctionTypeSyntax) -> TypeSyntax {
26-
guard let returnType = node.returnType.as(TupleTypeSyntax.self),
26+
guard let returnType = node.output.returnType.as(TupleTypeSyntax.self),
2727
returnType.elements.count == 0
2828
else {
2929
return super.visit(node)
@@ -43,7 +43,10 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
4343
// `(Int -> ()) -> ()` should become `(Int -> Void) -> Void`).
4444
let arguments = visit(node.arguments)
4545
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
46-
return TypeSyntax(node.withArguments(arguments).withReturnType(TypeSyntax(voidKeyword)))
46+
var rewrittenNode = node
47+
rewrittenNode.arguments = arguments
48+
rewrittenNode.output.returnType = TypeSyntax(voidKeyword)
49+
return TypeSyntax(rewrittenNode)
4750
}
4851

4952
public override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax {

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,9 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
419419
leftParen: functionType.leftParen,
420420
argumentTypes: functionType.arguments,
421421
rightParen: functionType.rightParen,
422-
asyncKeyword: functionType.asyncKeyword,
423-
throwsOrRethrowsKeyword: functionType.throwsOrRethrowsKeyword,
424-
arrow: functionType.arrow,
425-
returnType: functionType.returnType
422+
effectSpecifiers: functionType.effectSpecifiers,
423+
arrow: functionType.output.arrow,
424+
returnType: functionType.output.returnType
426425
)
427426
return ExprSyntax(result)
428427

@@ -461,8 +460,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
461460
leftParen: TokenSyntax,
462461
argumentTypes: TupleTypeElementListSyntax,
463462
rightParen: TokenSyntax,
464-
asyncKeyword: TokenSyntax?,
465-
throwsOrRethrowsKeyword: TokenSyntax?,
463+
effectSpecifiers: TypeEffectSpecifiersSyntax?,
466464
arrow: TokenSyntax,
467465
returnType: TypeSyntax
468466
) -> SequenceExprSyntax? {
@@ -478,8 +476,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
478476
elementList: argumentTypeExprs,
479477
rightParen: rightParen)
480478
let arrowExpr = ArrowExprSyntax(
481-
asyncKeyword: asyncKeyword,
482-
throwsToken: throwsOrRethrowsKeyword,
479+
effectSpecifiers: effectSpecifiers,
483480
arrowToken: arrow)
484481

485482
return SequenceExprSyntax(

Sources/SwiftFormatRules/UseSingleLinePropertyGetter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
2929
acc.accessorKind.tokenKind == .keyword(.get),
3030
acc.attributes == nil,
3131
acc.modifier == nil,
32-
acc.asyncKeyword == nil,
33-
acc.throwsKeyword == nil
32+
acc.effectSpecifiers == nil
3433
else { return node }
3534

3635
diagnose(.removeExtraneousGetBlock, on: acc)

Sources/SwiftFormatRules/UseSynthesizedInitializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
4040
// Collect any possible redundant initializers into a list
4141
} else if let initDecl = member.as(InitializerDeclSyntax.self) {
4242
guard initDecl.optionalMark == nil else { continue }
43-
guard initDecl.signature.throwsOrRethrowsKeyword == nil else { continue }
43+
guard initDecl.signature.effectSpecifiers?.throwsSpecifier == nil else { continue }
4444
initializers.append(initDecl)
4545
}
4646
}

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
6363
}
6464

6565
validateThrows(
66-
signature.throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
66+
signature.effectSpecifiers?.throwsSpecifier, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
6767
validateReturn(
6868
returnClause, name: name, returnDesc: commentInfo.returnsDescription, node: node)
6969
let funcParameters = funcParametersIdentifiers(in: signature.input.parameterList)

0 commit comments

Comments
 (0)