Skip to content

Commit c40a978

Browse files
Merge pull request #3110 from swiftlang/jepa
Enable some nice-to-have code formatting rules
2 parents 58fdc3b + 74a8c07 commit c40a978

File tree

6 files changed

+89
-13
lines changed

6 files changed

+89
-13
lines changed

.swift-format

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"NoBlockComments": false,
1414
"OrderedImports": true,
1515
"UseLetInEveryBoundCaseVariable": false,
16-
"UseSynthesizedInitializer": false
16+
"UseSynthesizedInitializer": false,
17+
"ReturnVoidInsteadOfEmptyTuple": true,
18+
"NoVoidReturnOnFunctionSignature": true,
1719
}
1820
}

Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public struct AddAsyncMacro: PeerMacro {
4242
}
4343

4444
// This only makes sense void functions
45-
if funcDecl.signature.returnClause?.type.as(IdentifierTypeSyntax.self)?.name.text != "Void" {
45+
if let returnClause = funcDecl.signature.returnClause,
46+
returnClause.type.as(IdentifierTypeSyntax.self)?.name.text != "Void"
47+
{
4648
throw CustomError.message(
4749
"@addAsync requires an function that returns void"
4850
)

Examples/Sources/MacroExamples/Playground/PeerMacrosPlayground.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ func runPeerMacrosPlayground() {
1717

1818
struct MyStruct {
1919
@AddAsync
20-
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
20+
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) {
2121
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
2222
}
2323

2424
@AddAsync
25-
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
25+
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) {
2626
completionBlock(true)
2727
}
2828
}

Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ final class AddAsyncMacroTests: XCTestCase {
7979
)
8080
}
8181

82+
func testImplicitVoidResult() {
83+
assertMacroExpansion(
84+
"""
85+
@AddAsync
86+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) {
87+
}
88+
""",
89+
expandedSource: """
90+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) {
91+
}
92+
93+
func d(a: Int) async -> Bool {
94+
await withCheckedContinuation { continuation in
95+
d(a: a) { returnValue in
96+
97+
continuation.resume(returning: returnValue)
98+
}
99+
}
100+
101+
}
102+
""",
103+
macros: macros,
104+
indentationWidth: .spaces(2)
105+
)
106+
}
107+
82108
func testExpansionOnStoredPropertyEmitsError() {
83109
assertMacroExpansion(
84110
"""
@@ -105,18 +131,18 @@ final class AddAsyncMacroTests: XCTestCase {
105131
)
106132
}
107133

108-
func testExpansionOnAsyncFunctionEmitsError() {
134+
func testNonVoidResult() {
109135
assertMacroExpansion(
110136
"""
111137
struct Test {
112138
@AddAsync
113-
async func sayHello() {
139+
func sayHello() -> Int {
114140
}
115141
}
116142
""",
117143
expandedSource: """
118144
struct Test {
119-
async func sayHello() {
145+
func sayHello() -> Int {
120146
}
121147
}
122148
""",
@@ -132,4 +158,50 @@ final class AddAsyncMacroTests: XCTestCase {
132158
indentationWidth: .spaces(2)
133159
)
134160
}
161+
162+
func testAlreadyAsync() {
163+
assertMacroExpansion(
164+
"""
165+
@AddAsync
166+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) async {
167+
}
168+
""",
169+
expandedSource: """
170+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) async {
171+
}
172+
""",
173+
diagnostics: [
174+
DiagnosticSpec(
175+
message: "@addAsync requires an non async function",
176+
line: 1,
177+
column: 1,
178+
severity: .error
179+
)
180+
],
181+
macros: macros,
182+
indentationWidth: .spaces(2)
183+
)
184+
}
185+
186+
func testNoCompletionHandler() {
187+
assertMacroExpansion(
188+
"""
189+
@AddAsync
190+
func sayHello(x: Int) {}
191+
""",
192+
expandedSource: """
193+
func sayHello(x: Int) {}
194+
""",
195+
diagnostics: [
196+
DiagnosticSpec(
197+
message: "@addAsync requires an function that has a completion handler as last parameter",
198+
line: 1,
199+
column: 1,
200+
severity: .error
201+
)
202+
],
203+
macros: macros,
204+
indentationWidth: .spaces(2)
205+
)
206+
}
135207
}

Sources/SwiftParser/IncrementalParseTransition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Parser {
4747
///
4848
/// This is also used for testing purposes to ensure incremental reparsing
4949
/// worked as expected.
50-
public typealias ReusedNodeCallback = (_ node: Syntax) -> ()
50+
public typealias ReusedNodeCallback = (_ node: Syntax) -> Void
5151

5252
/// Keeps track of a previously parsed syntax tree and the source edits that
5353
/// occurred since it was created.

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ fileprivate extension SyntaxText {
701701
/// - Returns: The position at the end of the walk.
702702
func forEachEndOfLine(
703703
position: AbsolutePosition,
704-
body: (AbsolutePosition) -> ()
704+
body: (AbsolutePosition) -> Void
705705
) -> AbsolutePosition {
706706
guard let startPtr = buffer.baseAddress else {
707707
return position
@@ -745,7 +745,7 @@ fileprivate extension RawTriviaPiece {
745745
/// - Returns: The position at the end of the walk.
746746
func forEachEndOfLine(
747747
position: AbsolutePosition,
748-
body: (AbsolutePosition) -> ()
748+
body: (AbsolutePosition) -> Void
749749
) -> AbsolutePosition {
750750
var position = position
751751
switch self {
@@ -788,7 +788,7 @@ fileprivate extension RawTriviaPieceBuffer {
788788
/// - Returns: The position at the end of the walk.
789789
func forEachEndOfLine(
790790
position: AbsolutePosition,
791-
body: (AbsolutePosition) -> ()
791+
body: (AbsolutePosition) -> Void
792792
) -> AbsolutePosition {
793793
var position = position
794794
for piece in self {
@@ -810,8 +810,8 @@ fileprivate extension RawSyntax {
810810
/// - Returns: The position at the end of the walk.
811811
func forEachEndOfLine(
812812
position: AbsolutePosition,
813-
body: (AbsolutePosition) -> (),
814-
handleSourceLocationDirective: (_ position: AbsolutePosition, _ rawSyntax: RawSyntax) -> ()
813+
body: (AbsolutePosition) -> Void,
814+
handleSourceLocationDirective: (_ position: AbsolutePosition, _ rawSyntax: RawSyntax) -> Void
815815
) -> AbsolutePosition {
816816
var position = position
817817
switch self.rawData.payload {

0 commit comments

Comments
 (0)