Skip to content

Commit f46f530

Browse files
authored
Merge pull request #285 from allevato/cherrypick-5.6
[5.6] Async fixes and README update
2 parents 3e924a8 + d4dd9e5 commit f46f530

File tree

6 files changed

+321
-4
lines changed

6 files changed

+321
-4
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ Swift toolchain that is installed and used to build and run the formatter:
2828
| Xcode Release | Swift Version | `swift-format` Branch / Tags |
2929
|:----------------|:-----------------------|:---------------------------------|
3030
|| Swift at `main` | `main` |
31+
| Xcode 13.3 | Swift 5.6 | `release/5.6` / `0.50600.x` |
3132
| Xcode 13.0–13.2 | Swift 5.5 | `swift-5.5-branch` / `0.50500.x` |
3233
| Xcode 12.5 | Swift 5.4 | `swift-5.4-branch` / `0.50400.x` |
3334
| Xcode 12.0–12.4 | Swift 5.3 | `swift-5.3-branch` / `0.50300.x` |
3435
| Xcode 11.4–11.7 | Swift 5.2 | `swift-5.2-branch` / `0.50200.x` |
3536
| Xcode 11.0–11.3 | Swift 5.1 | `swift-5.1-branch` |
3637

37-
For example, if you are using Xcode 13.1 (Swift 5.5), you will need
38-
`swift-format` 0.50500.0.
38+
For example, if you are using Xcode 13.3 (Swift 5.6), you will need
39+
`swift-format` 0.50600.0.
3940

4041
## Getting swift-format
4142

@@ -44,7 +45,7 @@ then once you have identified the version you need, you can check out the
4445
source and build it using the following commands:
4546

4647
```sh
47-
VERSION=0.50500.0 # replace this with the version you need
48+
VERSION=0.50600.0 # replace this with the version you need
4849
git clone https://github.com/apple/swift-format.git
4950
cd swift-format
5051
git checkout "tags/$VERSION"

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+16
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
441441

442442
override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
443443
arrangeAttributeList(node.attributes)
444+
445+
if let asyncKeyword = node.asyncKeyword {
446+
if node.throwsKeyword != nil {
447+
before(asyncKeyword, tokens: .break, .open)
448+
} else {
449+
before(asyncKeyword, tokens: .break)
450+
}
451+
}
452+
453+
if let throwsKeyword = node.throwsKeyword {
454+
before(node.throwsKeyword, tokens: .break)
455+
if node.asyncKeyword != nil {
456+
after(throwsKeyword, tokens: .close)
457+
}
458+
}
459+
444460
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
445461
return .visitChildren
446462
}

Sources/SwiftFormatRules/UseSingleLinePropertyGetter.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
2828
accessorBlock.accessors.count == 1,
2929
acc.accessorKind.tokenKind == .contextualKeyword("get"),
3030
acc.attributes == nil,
31-
acc.modifier == nil
31+
acc.modifier == nil,
32+
acc.asyncKeyword == nil,
33+
acc.throwsKeyword == nil
3234
else { return Syntax(node) }
3335

3436
diagnose(.removeExtraneousGetBlock, on: acc)

Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift

+157
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,161 @@ final class AccessorTests: PrettyPrintTestCase {
295295

296296
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
297297
}
298+
299+
func testPropertyEffectsWithBodyAfter() {
300+
let input =
301+
"""
302+
var x: T {
303+
get async {
304+
foo()
305+
bar()
306+
}
307+
}
308+
var x: T {
309+
get throws {
310+
foo()
311+
bar()
312+
}
313+
}
314+
var x: T {
315+
get async throws {
316+
foo()
317+
bar()
318+
}
319+
}
320+
"""
321+
322+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
323+
324+
let expected16 =
325+
"""
326+
var x: T {
327+
get async {
328+
foo()
329+
bar()
330+
}
331+
}
332+
var x: T {
333+
get throws {
334+
foo()
335+
bar()
336+
}
337+
}
338+
var x: T {
339+
get
340+
async throws
341+
{
342+
foo()
343+
bar()
344+
}
345+
}
346+
347+
"""
348+
349+
assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
350+
351+
let expected10 =
352+
"""
353+
var x: T {
354+
get
355+
async
356+
{
357+
foo()
358+
bar()
359+
}
360+
}
361+
var x: T {
362+
get
363+
throws
364+
{
365+
foo()
366+
bar()
367+
}
368+
}
369+
var x: T {
370+
get
371+
async
372+
throws
373+
{
374+
foo()
375+
bar()
376+
}
377+
}
378+
379+
"""
380+
381+
assertPrettyPrintEqual(input: input, expected: expected10, linelength: 10)
382+
}
383+
384+
func testPropertyEffectsWithNoBodyAfter() {
385+
let input =
386+
"""
387+
protocol P {
388+
var x: T { get async }
389+
var x: T { get throws }
390+
var x: T { get async throws }
391+
}
392+
"""
393+
394+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
395+
396+
let expected20 =
397+
"""
398+
protocol P {
399+
var x: T {
400+
get async
401+
}
402+
var x: T {
403+
get throws
404+
}
405+
var x: T {
406+
get async throws
407+
}
408+
}
409+
410+
"""
411+
412+
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
413+
414+
let expected18 =
415+
"""
416+
protocol P {
417+
var x: T {
418+
get async
419+
}
420+
var x: T {
421+
get throws
422+
}
423+
var x: T {
424+
get
425+
async throws
426+
}
427+
}
428+
429+
"""
430+
431+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
432+
433+
let expected12 =
434+
"""
435+
protocol P {
436+
var x: T {
437+
get
438+
async
439+
}
440+
var x: T {
441+
get
442+
throws
443+
}
444+
var x: T {
445+
get
446+
async
447+
throws
448+
}
449+
}
450+
451+
"""
452+
453+
assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
454+
}
298455
}

Tests/SwiftFormatPrettyPrintTests/SubscriptDeclTests.swift

+111
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,115 @@ final class SubscriptDeclTests: PrettyPrintTestCase {
497497
"""
498498
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 28)
499499
}
500+
501+
func testAccessorEffectsWithBodyAfter() {
502+
let input =
503+
"""
504+
struct X {
505+
subscript(i: Int) -> T {
506+
get async throws {
507+
foo()
508+
bar()
509+
}
510+
}
511+
}
512+
"""
513+
514+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
515+
516+
let expected18 =
517+
"""
518+
struct X {
519+
subscript(
520+
i: Int
521+
) -> T {
522+
get
523+
async throws
524+
{
525+
foo()
526+
bar()
527+
}
528+
}
529+
}
530+
531+
"""
532+
533+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
534+
535+
let expected12 =
536+
"""
537+
struct X {
538+
subscript(
539+
i: Int
540+
) -> T {
541+
get
542+
async
543+
throws
544+
{
545+
foo()
546+
bar()
547+
}
548+
}
549+
}
550+
551+
"""
552+
553+
assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
554+
}
555+
556+
func testAccessorEffectsWithNoBodyAfter() {
557+
let input =
558+
"""
559+
protocol P {
560+
subscript(i: Int) -> T { get async throws }
561+
}
562+
"""
563+
564+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
565+
566+
let expected20 =
567+
"""
568+
protocol P {
569+
subscript(i: Int)
570+
-> T
571+
{
572+
get async throws
573+
}
574+
}
575+
576+
"""
577+
578+
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
579+
580+
let expected18 =
581+
"""
582+
protocol P {
583+
subscript(
584+
i: Int
585+
) -> T {
586+
get
587+
async throws
588+
}
589+
}
590+
591+
"""
592+
593+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
594+
595+
let expected16 =
596+
"""
597+
protocol P {
598+
subscript(
599+
i: Int
600+
) -> T {
601+
get
602+
async
603+
throws
604+
}
605+
}
606+
607+
"""
608+
609+
assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
610+
}
500611
}

Tests/SwiftFormatRulesTests/UseSingleLinePropertyGetterTests.swift

+30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ final class UseSingleLinePropertyGetterTests: LintOrFormatRuleTestCase {
1818
var j: Int {
1919
mutating get { return 0 }
2020
}
21+
var k: Int {
22+
get async {
23+
return 4
24+
}
25+
}
26+
var l: Int {
27+
get throws {
28+
return 4
29+
}
30+
}
31+
var m: Int {
32+
get async throws {
33+
return 4
34+
}
35+
}
2136
""",
2237
expected: """
2338
var g: Int { return 4 }
@@ -31,6 +46,21 @@ final class UseSingleLinePropertyGetterTests: LintOrFormatRuleTestCase {
3146
var j: Int {
3247
mutating get { return 0 }
3348
}
49+
var k: Int {
50+
get async {
51+
return 4
52+
}
53+
}
54+
var l: Int {
55+
get throws {
56+
return 4
57+
}
58+
}
59+
var m: Int {
60+
get async throws {
61+
return 4
62+
}
63+
}
3464
""")
3565
}
3666
}

0 commit comments

Comments
 (0)